Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2006 Ola Bini <ola@ologix.com> Copyright (C) 2006 Ola Bini <ola.bini@ki.se> Copyright (C) 2006 Dave Brosius <dbrosius@mebigfatguy.com> Copyright (C) 2006 Peter K Chan <peter@oaktop.com> Copyright (C) 2009 Aurelian Oancea <aurelian@locknet.ro> Copyright (C) 2009 Vladimir Sizikov <vsizikov@gmail.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.zlib;
 
 
 import java.util.List;
 
 
 import org.jruby.Ruby;
 
 
 
 import static org.jruby.runtime.Visibility.*;
 
 import static org.jruby.ext.zlib.Zlib.*;
 
 
 
 import static org.jruby.CompatVersion.*;
 
 @JRubyModule(name="Zlib")
 public class RubyZlib {
     // version
     public final static String ZLIB_VERSION = "1.2.3.3";
     public final static String VERSION = "0.6.0";
 
     private static final Field crc32InternalField = getCRC32InternalField();
 
     private static final Field getCRC32InternalField() {
         try {
             Field field = CRC32.class.getDeclaredField("crc");
             field.setAccessible(true);
             field.setInt(new CRC32(), 1);
             return field;
         } catch(NoSuchFieldException nsfe) {
             return null;
         } catch(IllegalAccessException iae) {
             return null;
         }
     }

    
Create the Zlib module and add it to the Ruby runtime.
 
     public static RubyModule createZlibModule(Ruby runtime) {
         RubyModule mZlib = runtime.defineModule("Zlib");
         mZlib.defineAnnotatedMethods(RubyZlib.class);
 
         RubyClass cStandardError = runtime.getStandardError();
        RubyClass cZlibError = mZlib.defineClassUnder("Error"cStandardErrorcStandardError.getAllocator());
        mZlib.defineClassUnder("StreamEnd"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("StreamError"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("BufError"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("NeedDict"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("MemError"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("VersionError"cZlibErrorcZlibError.getAllocator());
        mZlib.defineClassUnder("DataError"cZlibErrorcZlibError.getAllocator());
        RubyClass cGzFile = mZlib.defineClassUnder("GzipFile"runtime.getObject(), .);
        cGzFile.defineAnnotatedMethods(RubyGzipFile.class);
        cGzFile.defineClassUnder("Error"cZlibErrorcZlibError.getAllocator());
        RubyClass cGzError = cGzFile.defineClassUnder("Error"cZlibErrorcZlibError.getAllocator());
        if (runtime.is1_9()) {
            cGzError.addReadAttribute(runtime.getCurrentContext(), "input");
        }
        cGzError.defineAnnotatedMethods(RubyGzipFile.Error.class);
        cGzFile.defineClassUnder("CRCError"cGzErrorcGzError.getAllocator());
        cGzFile.defineClassUnder("NoFooter"cGzErrorcGzError.getAllocator());
        cGzFile.defineClassUnder("LengthError"cGzErrorcGzError.getAllocator());
        RubyClass cGzReader = mZlib.defineClassUnder("GzipReader"cGzFile.);
        cGzReader.includeModule(runtime.getEnumerable());
        cGzReader.defineAnnotatedMethods(JZlibRubyGzipReader.class);
        RubyClass cGzWriter = mZlib.defineClassUnder("GzipWriter"cGzFile.);
        cGzWriter.defineAnnotatedMethods(JZlibRubyGzipWriter.class);
        mZlib.defineConstant("ZLIB_VERSION"runtime.newString());
        mZlib.defineConstant("VERSION"runtime.newString());
        mZlib.defineConstant("BINARY"runtime.newFixnum());
        mZlib.defineConstant("ASCII"runtime.newFixnum());
        mZlib.defineConstant("UNKNOWN"runtime.newFixnum());
        mZlib.defineConstant("DEF_MEM_LEVEL"runtime.newFixnum(8));
        mZlib.defineConstant("MAX_MEM_LEVEL"runtime.newFixnum(9));
        mZlib.defineConstant("OS_UNIX"runtime.newFixnum());
        mZlib.defineConstant("OS_UNKNOWN"runtime.newFixnum());
        mZlib.defineConstant("OS_CODE"runtime.newFixnum());
        mZlib.defineConstant("OS_ZSYSTEM"runtime.newFixnum());
        mZlib.defineConstant("OS_VMCMS"runtime.newFixnum());
        mZlib.defineConstant("OS_VMS"runtime.newFixnum());
        mZlib.defineConstant("OS_RISCOS"runtime.newFixnum());
        mZlib.defineConstant("OS_MACOS"runtime.newFixnum());
        mZlib.defineConstant("OS_OS2"runtime.newFixnum());
        mZlib.defineConstant("OS_AMIGA"runtime.newFixnum());
        mZlib.defineConstant("OS_QDOS"runtime.newFixnum());
        mZlib.defineConstant("OS_WIN32"runtime.newFixnum());
        mZlib.defineConstant("OS_ATARI"runtime.newFixnum());
        mZlib.defineConstant("OS_MSDOS"runtime.newFixnum());
        mZlib.defineConstant("OS_CPM"runtime.newFixnum());
        mZlib.defineConstant("OS_TOPS20"runtime.newFixnum());
        mZlib.defineConstant("DEFAULT_STRATEGY"runtime.newFixnum(.));
        mZlib.defineConstant("FILTERED"runtime.newFixnum(.));
        mZlib.defineConstant("HUFFMAN_ONLY"runtime.newFixnum(.));
        mZlib.defineConstant("NO_FLUSH"runtime.newFixnum(.));
        mZlib.defineConstant("SYNC_FLUSH"runtime.newFixnum(.));
        mZlib.defineConstant("FULL_FLUSH"runtime.newFixnum(.));
        mZlib.defineConstant("FINISH"runtime.newFixnum(.));
        mZlib.defineConstant("NO_COMPRESSION"runtime.newFixnum(.));
        mZlib.defineConstant("BEST_SPEED"runtime.newFixnum(.));
        mZlib.defineConstant("DEFAULT_COMPRESSION"runtime.newFixnum(.));
        mZlib.defineConstant("BEST_COMPRESSION"runtime.newFixnum(.));
        mZlib.defineConstant("MAX_WBITS"runtime.newFixnum(.));
        // ZStream actually *isn't* allocatable
        RubyClass cZStream = mZlib.defineClassUnder("ZStream"runtime.getObject(), .);
        cZStream.defineAnnotatedMethods(ZStream.class);
        cZStream.undefineMethod("new");
        RubyClass cInflate = mZlib.defineClassUnder("Inflate"cZStream.);
        cInflate.defineAnnotatedMethods(JZlibInflate.class);
        RubyClass cDeflate = mZlib.defineClassUnder("Deflate"cZStream.);
        cDeflate.defineAnnotatedMethods(JZlibDeflate.class);
        runtime.getKernel().callMethod(runtime.getCurrentContext(), "require"runtime.newString("stringio"));
        return mZlib;
    }
    @JRubyClass(name="Zlib::Error", parent="StandardError")
    public static class Error {}
    @JRubyClass(name="Zlib::StreamEnd", parent="Zlib::Error")
    public static class StreamEnd extends Error {}
    @JRubyClass(name="Zlib::StreamError", parent="Zlib::Error")
    public static class StreamError extends Error {}
    @JRubyClass(name="Zlib::BufError", parent="Zlib::Error")
    public static class BufError extends Error {}
    @JRubyClass(name="Zlib::NeedDict", parent="Zlib::Error")
    public static class NeedDict extends Error {}
    @JRubyClass(name="Zlib::MemError", parent="Zlib::Error")
    public static class MemError extends Error {}
    @JRubyClass(name="Zlib::VersionError", parent="Zlib::Error")
    public static class VersionError extends Error {}
    @JRubyClass(name="Zlib::DataError", parent="Zlib::Error")
    public static class DataError extends Error {}
    @JRubyMethod(name = "zlib_version", module = true, visibility = )
    public static IRubyObject zlib_version(IRubyObject recv) {
        RubyBasicObject res = (RubyBasicObject) ((RubyModule)recv).getConstant("ZLIB_VERSION");
        // MRI behavior, enforced by tests
        res.setTaint(true);
        return res;
    }
    @JRubyMethod(name = "crc32", optional = 2, module = true, visibility = )
    public static IRubyObject crc32(IRubyObject recvIRubyObject[] args) {
        args = Arity.scanArgs(recv.getRuntime(),args,0,2);
        int start = 0;
        ByteList bytes = null;
        if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
        if (!args[1].isNil()) start = (int)RubyNumeric.num2long(args[1]);
        CRC32 checksum = new CRC32();
        boolean slow =  == null;
        if (bytes != null) {
            if (start != 0 && !slow) {
                try {
                    .setInt(checksumstart);
                } catch (IllegalAccessException iae) {
                    slow = true;
                }
            }
            checksum.update(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
        }
        long result = checksum.getValue();
        if (start != 0 && slow) {
            result = JZlib.crc32_combine(startresultbytes.length());
        }
        return recv.getRuntime().newFixnum(result);
    }
    @JRubyMethod(name = "adler32", optional = 2, module = true, visibility = )
    public static IRubyObject adler32(IRubyObject recvIRubyObject[] args) {
        args = Arity.scanArgs(recv.getRuntime(),args,0,2);
        int start = 1;
        ByteList bytes = null;
        if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
        if (!args[1].isNil()) start = (int)RubyNumeric.num2long(args[1]);
        Adler32 checksum = new Adler32();
        if (bytes != null) {
            checksum.update(bytes.getUnsafeBytes(), bytes.begin(), bytes.length());
        }
        long result = checksum.getValue();
        if (start != 1) {
            result = JZlib.adler32_combine(startresultbytes.length());
        }
        return recv.getRuntime().newFixnum(result);
    }
    @JRubyMethod(compat = )
    public static IRubyObject inflate(ThreadContext contextIRubyObject recvIRubyObject string) {
        return JZlibInflate.s_inflate(contextrecvstring);
    }
    @JRubyMethod(required = 1, optional = 1, compat = )
    public static IRubyObject deflate(IRubyObject recvIRubyObject[] args) {
        return JZlibDeflate.s_deflate(recvargs);
    }
    @JRubyMethod(name = "crc_table", module = true, visibility = )
    public static IRubyObject crc_table(IRubyObject recv) {
        Ruby runtime = recv.getRuntime();
        int[] table = com.jcraft.jzlib.CRC32.getCRC32Table();
        RubyArray array = runtime.newArray(table.length);
        for (int i = 0; i < table.lengthi++) {
            array.append(runtime.newFixnum(table[i] & 0xffffffffL));
        }
        return array;
    }
    @JRubyMethod(name = "crc32_combine", required = 3, module = true, visibility = )
    public static IRubyObject crc32_combine(IRubyObject recv,
                                            IRubyObject arg0,
                                            IRubyObject arg1,
                                            IRubyObject arg2) {
        long crc1 = RubyNumeric.num2long(arg0);
        long crc2 = RubyNumeric.num2long(arg1);
        long len2 = RubyNumeric.num2long(arg2);
        long crc3 = com.jcraft.jzlib.JZlib.crc32_combine(crc1crc2len2);
        return recv.getRuntime().newFixnum(crc3);
    }
    @JRubyMethod(name = "adler32_combine", required = 3, module = true, visibility = )
    public static IRubyObject adler32_combine(IRubyObject recv,
                                            IRubyObject arg0,
                                            IRubyObject arg1,
                                            IRubyObject arg2) {
        long adler1 = RubyNumeric.num2long(arg0);
        long adler2 = RubyNumeric.num2long(arg1);
        long len2 = RubyNumeric.num2long(arg2);
        long adler3 = com.jcraft.jzlib.JZlib.adler32_combine(adler1adler2len2);
        return recv.getRuntime().newFixnum(adler3);
    }
    static RaiseException newZlibError(Ruby runtimeString message) {
        return newZlibError(runtime"Error"message);
    }
    static RaiseException newBufError(Ruby runtimeString message) {
        return newZlibError(runtime"BufError"message);
    }
    static RaiseException newDictError(Ruby runtimeString message) {
        return newZlibError(runtime"NeedDict"message);
    }
    static RaiseException newStreamError(Ruby runtimeString message) {
        return newZlibError(runtime"StreamError"message);
    }
    static RaiseException newDataError(Ruby runtimeString message) {
        return newZlibError(runtime"DataError"message);
    }
    static RaiseException newZlibError(Ruby runtimeString klassString message) {
        RubyClass errorClass = runtime.getModule("Zlib").getClass(klass);
        return new RaiseException(RubyException.newException(runtimeerrorClassmessage), true);
    }
    static RaiseException newGzipFileError(Ruby runtimeString message) {
        return newGzipFileError(runtime"Error"message);
    }
    static RaiseException newCRCError(Ruby runtimeString message) {
        return newGzipFileError(runtime"CRCError"message);
    }
    static RaiseException newNoFooter(Ruby runtimeString message) {
        return newGzipFileError(runtime"NoFooter"message);
    }
    static RaiseException newLengthError(Ruby runtimeString message) {
        return newGzipFileError(runtime"LengthError"message);
    }
    static RaiseException newGzipFileError(Ruby runtimeString klassString message) {
        RubyClass errorClass = runtime.getModule("Zlib").getClass("GzipFile").getClass(klass);
        RubyException excn = RubyException.newException(runtimeerrorClassmessage);
        if (runtime.is1_9()) {
            // TODO: not yet supported. rewrite GzipReader/Writer with Inflate/Deflate?
            excn.setInstanceVariable("@input"runtime.getNil());
        }
        return new RaiseException(excntrue);
    }
    
    static int FIXNUMARG(IRubyObject objint ifnil) {
        if (obj.isNil()) return ifnil;
        
        return RubyNumeric.fix2int(obj);
    }
New to GrepCode? Check out our FAQ X