commons-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1163637&view=rev
Log:
Implement zlib deflater

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
    commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java

Modified: 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=1163637&r1=1163636&r2=1163637&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibDeflater.java
Wed Aug 31 14:28:30 2011
@@ -71,7 +71,7 @@ public class ZlibDeflater extends Deflat
      */
     public ZlibDeflater(int level, boolean enableCrc32)
     {
-        this(level, ZlibImpl.DEFAULT_WINDOW_SIZE, ZlibImpl.DEFAULT_MEMLEVEL,
+        this(level, ZlibImpl.DEFAULT_WINDOWBITS, ZlibImpl.DEFAULT_MEMLEVEL,
                     ZlibImpl.DEFAULT_STRATEGY, 0, enableCrc32);
     }
 

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=1163637&r1=1163636&r2=1163637&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 14:28:30 2011
@@ -74,6 +74,7 @@ final class ZlibImpl
     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_WINDOWBITS    =  15;
     public  static final  int     DEFAULT_COMPRESSION   = Z_DEFAULT_COMPRESSION;
     public  static final  int     DEFAULT_STRATEGY      = Z_DEFAULT_STRATEGY;
 

Modified: 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=1163637&r1=1163636&r2=1163637&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibInflater.java
Wed Aug 31 14:28:30 2011
@@ -61,7 +61,7 @@ public final class ZlibInflater extends 
      */
     public ZlibInflater()
     {
-        this(ZlibImpl.DEFAULT_WINDOW_SIZE, true);
+        this(ZlibImpl.DEFAULT_WINDOWBITS, true);
     }
 
     /**

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=1163637&r1=1163636&r2=1163637&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 14:28:30 2011
@@ -419,6 +419,140 @@ ACR_ZLIB_EXPORT(void, ZlibInflater, clos
     AcrFree(s);    
 }
 
+ACR_ZLIB_EXPORT(jint, ZlibDeflater, deflate0)(JNI_STDARGS, jlong stream)
+{
+    int rc;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    if (s->ccrc && s->next_size != 0) {
+        /* Calc crc32 for input data */
+        s->crc = crc32(s->crc, s->next_buff, s->next_size);
+        s->next_size = 0;
+    }
+    /* Use internal buffer */
+    s->zs.next_out  = ACR_ZBUFF(s);
+    s->zs.avail_out = s->blen;
+    rc = deflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        return s->blen - s->zs.avail_out;
+    }
+    else if (rc == Z_STREAM_END)
+        s->eos = JNI_TRUE;
+    else if (rc != Z_BUF_ERROR)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+    return 0;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibDeflater, deflate1)(JNI_STDARGS, jlong stream,
+                                              jbyteArray buf, jint off, jint len)
+{
+    int rc;
+    jbyte *next_out;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->eos)
+        return -1;
+    next_out = (*env)->GetByteArrayElements(env, buf, 0);
+    if (next_out == 0) {
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+        return -1;
+    }
+    if (s->ccrc && s->next_size != 0) {
+        /* Calc crc32 for input data */
+        s->crc = crc32(s->crc, s->next_buff, s->next_size);
+        s->next_size = 0;
+    }
+    s->zs.next_out  = (Bytef *)next_out + off;
+    s->zs.avail_out = len;
+    rc = deflate((z_streamp)s, s->flush);
+    (*env)->ReleaseByteArrayElements(env, buf, next_out, 0);
+    if (rc == Z_OK) {
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        return len - s->zs.avail_out;
+    }
+    else if (rc == Z_STREAM_END) {
+        s->eos = JNI_TRUE;
+        return len - s->zs.avail_out;
+    }
+    else if (rc != Z_BUF_ERROR)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+    return 0;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibDeflater, deflate2)(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;
+    }
+    if (s->ccrc && s->next_size != 0) {
+        /* Calc crc32 for input data */
+        s->crc = crc32(s->crc, s->next_buff, s->next_size);
+        s->next_size = 0;
+    }
+
+    s->zs.next_out  = next_out + off;
+    s->zs.avail_out = len;
+    rc = deflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        return len - s->zs.avail_out;
+    }
+    else if (rc == Z_STREAM_END) {
+        s->eos = JNI_TRUE;
+        return len - s->zs.avail_out;
+    }
+    else if (rc != Z_BUF_ERROR)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+    return 0;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibDeflater, deflate3)(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;
+    if (s->ccrc && s->next_size != 0) {
+        /* Calc crc32 for input data */
+        s->crc = crc32(s->crc, s->next_buff, s->next_size);
+        s->next_size = 0;
+    }
+
+    s->zs.next_out  = next_out;
+    s->zs.avail_out = len;
+    rc = deflate((z_streamp)s, s->flush);
+    if (rc == Z_OK) {
+        if (s->flush != Z_FINISH)
+            s->flush = Z_NO_FLUSH;
+        return len - s->zs.avail_out;
+    }
+    else if (rc == Z_STREAM_END) {
+        s->eos = JNI_TRUE;
+        return len - s->zs.avail_out;
+    }
+    else if (rc != Z_BUF_ERROR)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, s->zs.msg);
+    return 0;
+}
+
 ACR_ZLIB_EXPORT(jint, ZlibInflater, inflate0)(JNI_STDARGS, jlong stream)
 {
     int rc;
@@ -430,18 +564,18 @@ ACR_ZLIB_EXPORT(jint, ZlibInflater, infl
     s->zs.next_out  = ACR_ZBUFF(s);
     s->zs.avail_out = s->blen;
     rc = inflate((z_streamp)s, s->flush);
-    if (rc == Z_OK) {
+    if (rc == Z_OK || rc == Z_STREAM_END) {
         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);
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
         return ilen;
     }
     else {
-        if (rc == Z_STREAM_END)
-            s->eos = JNI_TRUE;
-        else if (rc == Z_MEM_ERROR)
+        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);
@@ -466,20 +600,20 @@ ACR_ZLIB_EXPORT(jint, ZlibInflater, infl
     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 (rc == Z_OK || rc == Z_STREAM_END) {
+        uInt ilen = len - 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);
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
         (*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)
+        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);
@@ -505,18 +639,18 @@ ACR_ZLIB_EXPORT(jint, ZlibInflater, infl
     s->zs.next_out  = next_out + off;
     s->zs.avail_out = len;
     rc = inflate((z_streamp)s, s->flush);
-    if (rc == Z_OK) {
+    if (rc == Z_OK || rc == Z_STREAM_END) {
         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);
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
         return ilen;
     }
     else {
-        if (rc == Z_STREAM_END)
-            s->eos = JNI_TRUE;
-        else if (rc == Z_MEM_ERROR)
+        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);
@@ -536,18 +670,18 @@ ACR_ZLIB_EXPORT(jint, ZlibInflater, infl
     s->zs.next_out  = next_out;
     s->zs.avail_out = len;
     rc = inflate((z_streamp)s, s->flush);
-    if (rc == Z_OK) {
+    if (rc == Z_OK || rc == Z_STREAM_END) {
         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);
+        if (rc == Z_STREAM_END)
+            s->eos = JNI_TRUE;
         return ilen;
     }
     else {
-        if (rc == Z_STREAM_END)
-            s->eos = JNI_TRUE;
-        else if (rc == Z_MEM_ERROR)
+        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);

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java?rev=1163637&r1=1163636&r2=1163637&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestZlib.java Wed
Aug 31 14:28:30 2011
@@ -41,11 +41,70 @@ public class TestZlib extends Assert
         byte[] dd = new byte[500];
         byte[] dc = new byte[loremIpsum.length()];
 
-        int sc = Zlib.buffToBuffCompress(cd, 0, dd, 0, cd.length, 9);
+        int sc = Zlib.buffToBuffCompress(cd, 0, dd, 0, cd.length, 6);
         // 270 is pre-computed length.
         assertEquals(sc, 270);
         int sd = Zlib.buffToBuffDecompress(dd, 0, dc, 0, sc);
         assertEquals(sd, loremIpsum.length());
     }
 
+    @Test(groups = { "core" })
+    public void zzDeflater()
+        throws Exception
+    {
+        byte[] cd = loremIpsum.getBytes();
+
+        ZlibDeflater zz = new ZlibDeflater();
+        zz.setInput(cd);
+        zz.finish();
+        byte[] dd = new byte[50];
+        byte[] bd = new byte[500];
+        byte[] dc = new byte[500];
+
+        int i;
+        int p = 0;
+        for (i = 0; i < 10; i++) {
+            int rc = zz.deflate(dd);
+//            System.out.println("Deflate (" + i + ") rc=" + rc);
+            if (rc > 0) {
+                System.arraycopy(dd, 0, bd, p, rc);
+                p += rc;
+            }
+            if (zz.finished()) {
+                // Finished compressing
+                break;
+            }
+        }
+        assertEquals(i, 5);
+        // 270 is pre-computed length.
+        assertEquals(zz.getTotalOut(), 270);
+        assertEquals(zz.getTotalOut(), p);
+        zz.close();
+        int sd = Zlib.buffToBuffDecompress(bd, 0, dc, 0, p);
+        assertEquals(sd, loremIpsum.length());
+    }
+
+    @Test(groups = { "core" })
+    public void zzInflater()
+        throws Exception
+    {
+        byte[] cd = loremIpsum.getBytes();
+        byte[] dd = new byte[500];
+        byte[] dc = new byte[50];
+
+        int sc = Zlib.buffToBuffCompress(cd, 0, dd, 0, cd.length, 6);
+//        System.out.println("Compressed to " + sc);
+        ZlibInflater zz = new ZlibInflater();
+        zz.setInput(dd, 0, sc);        
+        int i = 0;
+        int n;
+        do {
+            if ((n = zz.inflate(dc)) > 0)
+                i += n;
+        } while (n == dc.length);
+        assertTrue(zz.finished());
+        assertEquals(i, loremIpsum.length());
+        zz.close();
+    }
+
 }



Mime
View raw message