commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1163554 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/util/zlib/ native/shared/
Date Wed, 31 Aug 2011 09:33:32 GMT
Author: mturk
Date: Wed Aug 31 09:33:32 2011
New Revision: 1163554

URL: http://svn.apache.org/viewvc?rev=1163554&view=rev
Log:
Add zlib inflater and deflater classes

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
    commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java?rev=1163554&r1=1163553&r2=1163554&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/Zlib.java
Wed Aug 31 09:33:32 2011
@@ -50,6 +50,14 @@ public final class Zlib
     private static final int   Z_BUF_ERROR         = -5;
     private static final int   Z_VERSION_ERROR     = -6;
 
+    /* TODO: Create ZlibStrategy enum out of those
+     */
+    public  static final int   Z_FILTERED          = 1;
+    public  static final int   Z_HUFFMAN_ONLY      = 2;
+    public  static final int   Z_RLE               = 3;
+    public  static final int   Z_FIXED             = 4;
+    public  static final int   Z_DEFAULT_STRATEGY  = 0;
+    
     /* Indicates the stream direction */
     private boolean isCompressing = false;
 

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java?rev=1163554&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
Wed Aug 31 09:33:32 2011
@@ -0,0 +1,271 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.util.zlib;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.InvalidDataException;
+import org.apache.commons.runtime.InvalidRangeException;
+import org.apache.commons.runtime.OperationNotPermittedException;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.util.Deflater;
+
+/**
+ * Zlib compressor.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public class ZlibDeflater extends Deflater
+{
+
+    private long      handle;
+    private long      buffer;
+    private int       bufferSize;
+
+    private static native int     deflate0(long stream)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     deflate1(long stream, byte[] buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     deflate2(long stream, ByteBuffer buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     deflate3(long stream, long buf, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    private static native void    close0(long stream);
+    
+    /**
+     * Creates a new compressor.
+     */
+    public ZlibDeflater()
+    {
+        this(ZlibImpl.Z_DEFAULT_COMPRESSION, true);
+    }
+
+    /**
+     * Creates a new Zlib compressor.
+     *
+     * @param level compression level.
+     */
+    public ZlibDeflater(int level, boolean enableCrc32)
+    {
+        this(level, ZlibImpl.DEFAULT_WINDOW_SIZE, ZlibImpl.DEFAULT_MEMLEVEL,
+                    ZlibImpl.DEFAULT_STRATEGY, 0, enableCrc32);
+    }
+
+    /**
+     * Creates a new Zlib compressor.
+     *
+     * @param level compression level
+     * @param bufferSize internal buffer size.
+     */
+    public ZlibDeflater(int level, int windowBits,
+                        int memLevel, int strategy,
+                        int bufferSize, boolean enableCrc32)
+    {
+        if (strategy < 0 || strategy > 4)
+            throw new InvalidArgumentException();
+        handle = ZlibImpl.newHandle(bufferSize);
+        int rc = ZlibImpl.deflateInit2(handle, level, windowBits, memLevel, strategy, enableCrc32);
+        if (rc != 0) {
+            close0(handle);
+            throw new OutOfMemoryError();
+        }
+        this.bufferSize = bufferSize;
+        if (bufferSize > 0)
+            buffer = handle + ZlibImpl.SIZEOF_Z_STREAM;
+    }
+
+    @Override
+    public synchronized void close()
+        throws IOException
+    {
+        if (handle != 0L) {
+            close0(handle);
+            handle = 0L;
+        }
+    }
+
+    @Override
+    public synchronized boolean needsInput()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.needsInput(handle);
+    }
+
+    @Override
+    public synchronized boolean finished()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.finished(handle);
+    }
+
+    @Override
+    public synchronized void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (off < 0 || off + len > b.length)
+            throw new ArrayIndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput0(handle, b, off, len);
+    }
+
+    @Override
+    public synchronized void setInput(ByteBuffer b)
+        throws InvalidArgumentException
+    {
+        int len = b.remaining();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput1(handle, b, b.position(), len);
+    }
+
+    @Override
+    public synchronized void setInput(long b, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput2(handle, b, len);
+    }
+
+    @Override
+    public synchronized void setInput(Stream s)
+        throws IOException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (ZlibImpl.needsInput(handle)) {
+            int len = s.read(buffer, bufferSize);
+            if (len > 0)
+                ZlibImpl.setInput3(handle, len);
+        }
+    }
+
+    @Override
+    public synchronized long getTotalIn()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.getTotalIn(handle);
+    }
+
+    @Override
+    public synchronized long getTotalOut()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.getTotalOut(handle);
+    }
+
+    @Override
+    public synchronized void finish()
+        throws IllegalStateException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (!ZlibImpl.finish(handle))
+            throw new IllegalStateException();
+    }
+   
+    @Override
+    public synchronized void flush()
+        throws IllegalStateException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (!ZlibImpl.flush(handle))
+            throw new IllegalStateException();
+    }
+        
+    @Override
+    public int deflate(Stream s)
+        throws InvalidDataException,
+               OutOfMemoryError,
+               IOException
+    {
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        int n = deflate0(handle);
+        if (n > 0)
+            n = s.write(buffer, n);
+        return n;
+    }
+
+    @Override
+    public int deflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate1(handle, b, off, len);
+    }
+
+    @Override
+    public int deflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        int len = b.remaining();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate2(handle, b, b.position(), len);
+    }
+
+    @Override
+    public int deflate(long b, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == 0L)
+            throw new NullPointerException();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return deflate3(handle, b, len);
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java?rev=1163554&r1=1163553&r2=1163554&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
Wed Aug 31 09:33:32 2011
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package org.apache.commons.runtime.utilzlib;
+package org.apache.commons.runtime.util.zlib;
 
 import java.io.Closeable;
 import java.io.IOException;
@@ -39,9 +39,9 @@ final class ZlibImpl
     private static native int     init0();
     public  static native long    newHandle(int bufferSize)
         throws OutOfMemoryError;
-    public  static native int     inflateInit2(long handle, int windowBits, int streamSize);
-    public  static native int     deflateInit2(long handle, int level, int method,
-                                               int windowBits, int memLevel, int strategy);
+    public  static native int     inflateInit2(long handle, int windowBits, boolean enableCrc32);
+    public  static native int     deflateInit2(long handle, int level,
+                                               int windowBits, int memLevel, int strategy,
boolean enableCrc32);
 
     public  static native int     setInput0(long handle, byte[] buf, int off, int len);
     public  static native int     setInput1(long handle, ByteBuffer buf, int off, int len);
@@ -53,11 +53,10 @@ final class ZlibImpl
     public  static native boolean needsInput(long handle);
     public  static native long    getTotalIn(long handle);
     public  static native long    getTotalOut(long handle);
+    public  static native boolean flush(long handle);
+    public  static native boolean finish(long handle);
     public  static native boolean finished(long handle);
 
-    public  static native int     deflate(long handle, int flush);
-    public  static native int     inflate(long handle, int flush);
-    
     /* Flush modes */
     public  static final int      Z_NO_FLUSH            =  0;
     public  static final int      Z_PARTIAL_FLUSH       =  1;
@@ -69,12 +68,14 @@ final class ZlibImpl
     public  static final  int     Z_BEST_SPEED          =  1;
     public  static final  int     Z_BEST_COMPRESSION    =  9;
     public  static final  int     Z_DEFAULT_COMPRESSION = -1;
+    public  static final  int     Z_DEFAULT_STRATEGY    =  0;
 
     /* Defaults */
     public  static final  int     DEFAULT_BUFFER_SIZE   =  8096;
     public  static final  int     DEFAULT_MEMLEVEL      =  9;
     public  static final  int     DEFAULT_WINDOW_SIZE   = -15;
     public  static final  int     DEFAULT_COMPRESSION   = Z_DEFAULT_COMPRESSION;
+    public  static final  int     DEFAULT_STRATEGY      = Z_DEFAULT_STRATEGY;
 
     public  static final  int     SIZEOF_Z_STREAM;
     static {

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java?rev=1163554&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
Wed Aug 31 09:33:32 2011
@@ -0,0 +1,257 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.util.zlib;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.commons.runtime.InvalidArgumentException;
+import org.apache.commons.runtime.InvalidDataException;
+import org.apache.commons.runtime.InvalidRangeException;
+import org.apache.commons.runtime.OperationNotPermittedException;
+import org.apache.commons.runtime.OverflowException;
+import org.apache.commons.runtime.Pointer;
+import org.apache.commons.runtime.io.Stream;
+import org.apache.commons.runtime.util.Inflater;
+
+/**
+ * Zlib decompressor.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class ZlibInflater extends Inflater
+{
+
+    private long      handle;
+    private long      buffer;
+    private int       bufferSize;
+    private int       flush;
+    private static native int     inflate0(long stream)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     inflate1(long stream, byte[] buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     inflate2(long stream, ByteBuffer buf, int off, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+    private static native int     inflate3(long stream, long buf, int len)
+        throws InvalidDataException,
+               OutOfMemoryError;
+
+    private static native void    close0(long stream);
+               
+    /**
+     * Creates a new Zlib decompressor.
+     */
+    public ZlibInflater()
+    {
+        this(ZlibImpl.DEFAULT_WINDOW_SIZE, true);
+    }
+
+    /**
+     * Create a new Zlib decompressor
+     *
+     * @param windowBits base two logarithm of the maximum window
+     *          size (the size of the history buffer).
+     * @param enableCrc32 enable claculating crc32 on the decompressed data.
+     */
+    public ZlibInflater(int windowBits, boolean enableCrc32)
+    {
+        this(windowBits, 0, enableCrc32);
+    }
+
+    /**
+     * Create a new Zlib decompressor
+     *
+     * @param windowBits base two logarithm of the maximum window
+     *          size (the size of the history buffer).
+     * @param bufferSize internal buffer size.
+     * @param enableCrc32 enable claculating crc32 on the decompressed data.
+     */
+    public ZlibInflater(int windowBits, int bufferSize, boolean enableCrc32)
+    {
+        if (bufferSize < 0)
+            throw new InvalidArgumentException();
+        handle = ZlibImpl.newHandle(bufferSize);
+        int rc = ZlibImpl.inflateInit2(handle, windowBits, enableCrc32);
+        if (rc != 0) {
+            close0(handle);
+            throw new OutOfMemoryError();
+        }
+        this.bufferSize = bufferSize;
+        if (bufferSize > 0)
+            buffer = handle + ZlibImpl.SIZEOF_Z_STREAM;
+        flush = ZlibImpl.Z_NO_FLUSH;
+    }
+
+    @Override
+    public synchronized void close()
+        throws IOException
+    {
+        if (handle != 0L) {
+            close0(handle);
+            handle = 0L;
+        }
+    }
+
+    @Override
+    public synchronized boolean needsInput()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.needsInput(handle);
+    }
+
+    @Override
+    public synchronized boolean finished()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.finished(handle);
+    }
+
+    @Override
+    public synchronized void setInput(byte[] b, int off, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (off < 0 || off + len > b.length)
+            throw new ArrayIndexOutOfBoundsException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput0(handle, b, off, len);
+    }
+
+    @Override
+    public synchronized void setInput(ByteBuffer b)
+        throws InvalidArgumentException
+    {
+        int len = b.remaining();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput1(handle, b, b.position(), len);
+    }
+
+    @Override
+    public synchronized void setInput(long b, int len)
+        throws InvalidArgumentException
+    {
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (b == 0L)
+            throw new NullPointerException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        ZlibImpl.setInput2(handle, b, len);
+    }
+
+    @Override
+    public void setInput(Stream s)
+        throws IOException
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (ZlibImpl.needsInput(handle)) {
+            int len = s.read(buffer, bufferSize);
+            if (len > 0)
+                ZlibImpl.setInput3(handle, len);
+        }
+    }
+
+    @Override
+    public synchronized long getTotalIn()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.getTotalIn(handle);
+    }
+
+    @Override
+    public synchronized long getTotalOut()
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return ZlibImpl.getTotalOut(handle);
+    }
+
+    @Override
+    public int inflate(Stream s)
+        throws InvalidDataException,
+               OutOfMemoryError,
+               IOException
+    {
+        if (buffer == 0L)
+            throw new NullPointerException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        int n = 0;
+        int i;
+        do {
+            if ((i = inflate0(handle)) > 0)
+                n += s.write(buffer, i);
+        } while (i == bufferSize);
+        return n;
+    }
+
+    @Override
+    public int inflate(byte[] b, int off, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return inflate1(handle, b, off, len);
+    }
+
+    @Override
+    public int inflate(ByteBuffer b)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        int len = b.remaining();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return inflate2(handle, b, b.position(), len);
+    }
+
+    @Override
+    public int inflate(long b, int len)
+        throws InvalidArgumentException,
+               InvalidDataException,
+               OutOfMemoryError
+    {
+        if (b == 0L)
+            throw new NullPointerException();
+        if (len < 1)
+            throw new InvalidArgumentException();
+        if (handle == 0L)
+            throw new IllegalStateException();
+        return inflate3(handle, b, len);
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c?rev=1163554&r1=1163553&r2=1163554&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c Wed Aug 31 09:33:32 2011
@@ -29,11 +29,15 @@ static int ACR_ZSIZE;
 
 typedef struct acr_zstream {
     z_stream            zs;
-    unsigned int        blen;
+    uInt                blen;
     int                 state;
+    int                 flush;
     jboolean            eos;
-    unsigned long       crc;
+    jboolean            ccrc;
+    uLong               crc;
+    uInt                next_size;
     void               *next_array;
+    Bytef              *next_buff;
     Bytef              *next_data;
 } acr_zstream;
 
@@ -226,7 +230,7 @@ ACR_ZLIB_EXPORT(jlong, ZlibImpl, newHand
 ACR_ZLIB_EXPORT(jint, ZlibImpl, inflateInit2)(JNI_STDARGS,
                                               jlong stream,
                                               jint windowBits,
-                                              jint streamSize)
+                                              jboolean crc)
 {
     int rc;
     acr_zstream *s = J2P(stream, acr_zstream *);
@@ -234,23 +238,27 @@ ACR_ZLIB_EXPORT(jint, ZlibImpl, inflateI
     rc =  inflateInit2((z_streamp)s, windowBits);
     s->state = 0;
     s->eos   = JNI_FALSE;
+    s->ccrc  = crc;
+    s->crc   = crc32(0L, Z_NULL, 0);
         
     return rc;
 }
 
 ACR_ZLIB_EXPORT(jlong, ZlibImpl, deflateInit2)(JNI_STDARGS, jlong stream,
                                                jint level,
-                                               jint method,
                                                jint windowBits,
                                                jint memLevel,
-                                               jint strategy)
+                                               jint strategy,
+                                               jboolean crc)
 {
     int rc;
     acr_zstream *s = J2P(stream, acr_zstream *);
 
-    rc = deflateInit2((z_streamp)s, level, method, windowBits, memLevel, strategy);
+    rc = deflateInit2((z_streamp)s, level, Z_DEFLATED, windowBits, memLevel, strategy);
     s->state = 0;
     s->eos   = JNI_FALSE;
+    s->ccrc  = crc;
+    s->crc   = crc32(0L, Z_NULL, 0);
     
     return rc;
 }
@@ -265,8 +273,10 @@ ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput
         return Z_MEM_ERROR;
     s->next_array  = s->next_data;
     (*env)->GetByteArrayRegion(env, src, off, len, s->next_array);
-    s->zs.next_in  = s->next_data + off;
+    s->next_buff   = s->next_data + off;
+    s->zs.next_in  = s->next_buff;
     s->zs.avail_in = len;
+    s->next_size   = len;
     return Z_OK;
 }
 
@@ -279,8 +289,10 @@ ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput
     s->next_data = (*env)->GetDirectBufferAddress(env, src);
     if (s->next_data == 0)
         return Z_BUF_ERROR;
-    s->zs.next_in  = s->next_data + off;
+    s->next_buff   = s->next_data + off;
+    s->zs.next_in  = s->next_buff;
     s->zs.avail_in = len;
+    s->next_size   = len;
     return Z_OK;
 }
 
@@ -293,8 +305,10 @@ ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput
     ACR_MFREE(s->next_array);
     s->next_data  = J2P(src, Bytef *);
     if (s->next_data != 0) {
-        s->zs.next_in  = s->next_data;
+        s->next_buff   = s->next_data;
+        s->zs.next_in  = s->next_buff;
         s->zs.avail_in = len;
+        s->next_size   = len;
         rc = Z_OK;
     }
     return rc;
@@ -307,11 +321,13 @@ ACR_ZLIB_EXPORT(void, ZlibImpl, setInput
 
     ACR_MFREE(s->next_array);
     /* Use internal buffer */
-    s->zs.next_in  = ACR_ZBUFF(s);
+    s->next_buff   = ACR_ZBUFF(s);
+    s->zs.next_in  = s->next_buff;
     if (len < 0)
         s->zs.avail_in = s->blen;
     else
         s->zs.avail_in = len;
+    s->next_size = s->zs.avail_in;
 }
 
 
@@ -358,16 +374,32 @@ ACR_ZLIB_EXPORT(jboolean, ZlibImpl, fini
     return s->eos;
 }
 
-ACR_ZLIB_EXPORT(jint, ZlibImpl, deflate)(JNI_STDARGS, jlong stream, jint flush)
+ACR_ZLIB_EXPORT(jboolean, ZlibImpl, finish)(JNI_STDARGS, jlong stream)
 {
     acr_zstream *s = J2P(stream, acr_zstream *);
-    return deflate((z_streamp)s, flush);
+    if (s->state == 0) {
+        s->flush = Z_FINISH;
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
 }
 
-ACR_ZLIB_EXPORT(jint, ZlibImpl, inflate)(JNI_STDARGS, jlong stream, jint flush)
+ACR_ZLIB_EXPORT(jboolean, ZlibImpl, flush)(JNI_STDARGS, jlong stream)
 {
     acr_zstream *s = J2P(stream, acr_zstream *);
-    return inflate((z_streamp)s, flush);
+    if (s->state == 0) {
+        s->flush = Z_SYNC_FLUSH;
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibImpl, getCrc32)(JNI_STDARGS, jlong stream)
+{
+    acr_zstream *s = J2P(stream, acr_zstream *);
+    return (jint)s->crc;
 }
 
 ACR_ZLIB_EXPORT(void, ZlibDeflater, close0)(JNI_STDARGS, jlong stream)
@@ -386,3 +418,139 @@ ACR_ZLIB_EXPORT(void, ZlibInflater, clos
     AcrFree(s->next_array);
     AcrFree(s);    
 }
+
+ACR_ZLIB_EXPORT(jint, ZlibInflater, inflate0)(JNI_STDARGS, jlong stream)
+{
+    int rc;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    /* Use internal buffer */
+    s->zs.next_out  = ACR_ZBUFF(s);
+    s->zs.avail_out = s->blen;
+    rc = inflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        uInt ilen = s->blen - s->zs.avail_out;
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        if (s->ccrc)
+            s->crc = crc32(s->crc, ACR_ZBUFF(s), ilen);
+        return ilen;
+    }
+    else {
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
+        else if (rc == Z_MEM_ERROR)
+            ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        else if (rc != Z_BUF_ERROR)
+            ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+        return 0;
+    }
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibInflater, inflate1)(JNI_STDARGS, jlong stream,
+                                              jbyteArray buf, jint off, jint len)
+{
+    int rc;
+    Bytef *next_out;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    next_out = (*env)->GetPrimitiveArrayCritical(env, buf, 0);
+    if (next_out == 0) {
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        return -1;
+    }
+    s->zs.next_out  = next_out + off;
+    s->zs.avail_out = len;
+    rc = inflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        uInt ilen = s->blen - s->zs.avail_out;
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        if (s->ccrc)
+            s->crc = crc32(s->crc, next_out + off, ilen);
+        (*env)->ReleasePrimitiveArrayCritical(env, buf, next_out, 0);
+        return ilen;
+    }
+    else {
+        (*env)->ReleasePrimitiveArrayCritical(env, buf, next_out, 0);
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
+        else if (rc == Z_MEM_ERROR)
+            ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        else if (rc != Z_BUF_ERROR)
+            ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+        return 0;
+    }    
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibInflater, inflate2)(JNI_STDARGS, jlong stream,
+                                              jobject buf, jint off, jint len)
+{
+    int rc;
+    Bytef *next_out;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    next_out = (*env)->GetDirectBufferAddress(env, buf);
+    if (next_out == 0) {
+        ACR_THROW(ACR_EX_ENULL, 0);
+        return -1;
+    }
+
+    s->zs.next_out  = next_out + off;
+    s->zs.avail_out = len;
+    rc = inflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        uInt ilen = s->blen - s->zs.avail_out;
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        if (s->ccrc)
+            s->crc = crc32(s->crc, next_out + off, ilen);
+        return ilen;
+    }
+    else {
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
+        else if (rc == Z_MEM_ERROR)
+            ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        else if (rc != Z_BUF_ERROR)
+            ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+        return 0;
+    }
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibInflater, inflate3)(JNI_STDARGS, jlong stream,
+                                              jlong buf, jint len)
+{
+    int rc;
+    Bytef *next_out = J2P(buf, Bytef *);
+    acr_zstream *s  = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    s->zs.next_out  = next_out;
+    s->zs.avail_out = len;
+    rc = inflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        uInt ilen = s->blen - s->zs.avail_out;
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        if (s->ccrc)
+            s->crc = crc32(s->crc, next_out, ilen);
+        return ilen;
+    }
+    else {
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
+        else if (rc == Z_MEM_ERROR)
+            ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        else if (rc != Z_BUF_ERROR)
+            ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+        return 0;
+    }
+}



Mime
View raw message