commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1158327 [2/2] - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/io/ java/org/apache/commons/runtime/net/ java/org/apache/commons/runtime/util/ java/org/apache/commons/runtime/util/bzip2/ native/include/acr/ nati...
Date Tue, 16 Aug 2011 15:40:08 GMT
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/bzip2.c Tue Aug 16 15:40:07 2011
@@ -36,21 +36,17 @@ void _cr_bz_internal_error(int errorcode
 
 typedef struct acr_bzstream {
     bz_stream           bz;
-    int                 compressing;
     unsigned int        blen;
-    jbyteArray          nia;
-    jbyte              *nip;
-    jbyteArray          noa;
-    jbyte              *nop;
+    int                 state;
+    jboolean            eos;
+    void               *next_array;
+    char               *next_data;
 } acr_bzstream;
 
 
 static const char *bz_errmsg(int err)
 {
     switch (err) {
-        case BZ_SEQUENCE_ERROR:
-            return "Out of sequence method call";
-        break;
         case BZ_DATA_ERROR:
             return "Data integrity error";
         break;
@@ -63,15 +59,15 @@ static const char *bz_errmsg(int err)
     return "Unknown Bzip2 error";
 }
 
-ACR_BZIP2_EXPORT(jstring, Bzip2, sver0)(JNI_STDARGS)
+ACR_BZIP2_EXPORT(jstring, Bzip2, version0)(JNI_STDARGS)
 {
     return AcrNewJavaStringU(env, BZ2_bzlibVersion());
 }
 
-ACR_BZIP2_EXPORT(jlong, Bzip2, compress0)(JNI_STDARGS, jbyteArray src, jint spos,
-                                          jbyteArray dst, jint dpos, jint dlen,
-                                          jint len, jint blockSize100k,
-                                          jint workFactor)
+ACR_BZIP2_EXPORT(jlong, Bzip2, b2bdeflate0)(JNI_STDARGS, jbyteArray src, jint spos,
+                                            jbyteArray dst, jint dpos, jint dlen,
+                                            jint len, jint blockSize100k,
+                                            jint workFactor)
 {
     unsigned int dstLen;
     char *scp;
@@ -103,10 +99,10 @@ ACR_BZIP2_EXPORT(jlong, Bzip2, compress0
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, compress1)(JNI_STDARGS, jobject src, jint spos,
-                                         jobject dst, jint dpos, jint dlen,
-                                         jint len, jint blockSize100k,
-                                         jint workFactor)
+ACR_BZIP2_EXPORT(jint, Bzip2, b2bdeflate1)(JNI_STDARGS, jobject src, jint spos,
+                                           jobject dst, jint dpos, jint dlen,
+                                           jint len, jint blockSize100k,
+                                           jint workFactor)
 {
     unsigned int dstLen;
     char *scp;
@@ -135,9 +131,9 @@ ACR_BZIP2_EXPORT(jint, Bzip2, compress1)
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, compress2)(JNI_STDARGS, jlong src, jlong dst,
-                                         jlong dlen, jlong len, jint blockSize100k,
-                                         jint workFactor)
+ACR_BZIP2_EXPORT(jlong, Bzip2, b2bdeflate2)(JNI_STDARGS, jlong src, jlong dst,
+                                            jlong dlen, jlong len, jint blockSize100k,
+                                            jint workFactor)
 {
     unsigned int dstLen;
     char *scp = J2P(src, char *);
@@ -162,7 +158,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2, compress2)
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, decompress0)(JNI_STDARGS, jbyteArray src, jint spos,
+ACR_BZIP2_EXPORT(jint, Bzip2, b2binflate0)(JNI_STDARGS, jbyteArray src, jint spos,
                                            jbyteArray dst, jint dpos, jint dlen,
                                            jint len, jboolean small)
 {
@@ -197,7 +193,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2, decompress
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, decompress1)(JNI_STDARGS, jobject src, jint spos,
+ACR_BZIP2_EXPORT(jint, Bzip2, b2binflate1)(JNI_STDARGS, jobject src, jint spos,
                                            jobject dst, jint dpos, jint dlen,
                                            jint len, jboolean small)
 {
@@ -230,7 +226,7 @@ ACR_BZIP2_EXPORT(jint, Bzip2, decompress
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jlong, Bzip2, decompress2)(JNI_STDARGS, jlong src,
+ACR_BZIP2_EXPORT(jlong, Bzip2, b2binflate2)(JNI_STDARGS, jlong src,
                                             jlong dst, jlong dlen,
                                             jlong len, jboolean small)
 {
@@ -260,179 +256,394 @@ ACR_BZIP2_EXPORT(jlong, Bzip2, decompres
     return 0;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, size0)(JNI_STDARGS)
+ACR_BZIP2_EXPORT(jint, Libbzip2, bzsize0)(JNI_STDARGS)
 {
-    int ssize = ISIZEOF(acr_bzstream);
+    int ssize  = ISIZEOF(acr_bzstream);
     ACR_BZSIZE = ACR_ALIGN_DEFAULT(ssize);
     return ACR_BZSIZE;
 }
 
-ACR_BZIP2_EXPORT(void, Bzip2, init0)(JNI_STDARGS, jbyteArray stream,
-                                     jint bsize)
+ACR_BZIP2_EXPORT(jlong, Libbzip2, newStream)(JNI_STDARGS, jint bsize)
 {
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
-
-    memset(&s->bz, 0, sizeof(bz_stream));
-    s->blen = bsize;
-    RELEASE_CRITICAL(stream, s);
+    acr_bzstream *s;
+    s = ACR_EALLOC(acr_bzstream, bsize);
+    if (s != 0)
+        s->blen = bsize;
+    return P2J(s);
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, bzinit0)(JNI_STDARGS, jbyteArray stream,
-                                       jint blockSize100k,
-                                       jint workFactor)
+ACR_BZIP2_EXPORT(jint, Libbzip2, compressInit)(JNI_STDARGS,
+                                               jlong stream,
+                                               jint blockSize100k,
+                                               jint workFactor)
 {
     int rc;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
     rc = BZ2_bzCompressInit((bz_stream *)s, blockSize100k, 0, workFactor);
-    s->compressing = 1;
-    RELEASE_CRITICAL(stream, s);    
+    s->state = BZ_RUN;
+    s->eos   = JNI_FALSE;
+        
     return rc;
 }
 
-ACR_BZIP2_EXPORT(jlong, Bzip2, bzinit1)(JNI_STDARGS, jbyteArray stream,
-                                        jboolean small)
+ACR_BZIP2_EXPORT(jlong, Libbzip2, decompressInit)(JNI_STDARGS, jlong stream,
+                                                  jboolean small)
 {
     int rc;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
     rc = BZ2_bzDecompressInit((bz_stream *)s, 0, small);
-    s->compressing = 0;
-    RELEASE_CRITICAL(stream, s);
+    s->state = BZ_RUN;
+    s->eos   = JNI_FALSE;
+    
     return rc;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, end0)(JNI_STDARGS, jbyteArray stream)
+
+ACR_BZIP2_EXPORT(jint, Libbzip2, setInput0)(JNI_STDARGS, jlong stream,
+                                           jbyteArray src, jint off, jint len)
 {
-    int rc;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->compressing)
-        rc = BZ2_bzCompressEnd((bz_stream *)s);
-    else
-        rc = BZ2_bzDecompressEnd((bz_stream *)s);
-    if (s->nip != 0 && s->nia != 0)
-        (*env)->ReleaseByteArrayElements(env, s->nia, s->nip, 0);
-    if (s->nop != 0 && s->noa != 0)
-        (*env)->ReleaseByteArrayElements(env, s->noa, s->nop, 0);
-    memset(&s->bz, 0, sizeof(bz_stream));
-    RELEASE_CRITICAL(stream, s);
-    return rc;
+    s->next_data = ACR_REALLOC(char, s->next_array, len);
+    if (s->next_data == 0)
+        return BZ_MEM_ERROR;
+    s->next_array  = s->next_data;
+    (*env)->GetByteArrayRegion(env, src, off, len, s->next_array);
+    s->bz.next_in  = s->next_data + off;
+    s->bz.avail_in = len;
+    return BZ_OK;
+}
+
+ACR_BZIP2_EXPORT(jint, Libbzip2, setInput1)(JNI_STDARGS, jlong stream,
+                                            jobject src, jint off, jint len)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    ACR_MFREE(s->next_array);
+    s->next_data = (*env)->GetDirectBufferAddress(env, src);
+    if (s->next_data == 0)
+        return BZ_PARAM_ERROR;
+    s->bz.next_in  = s->next_data + off;
+    s->bz.avail_in = len;
+    return BZ_OK;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, nextinp0)(JNI_STDARGS, jbyteArray stream,
-                                        jbyteArray src, jint off, jint len)
+ACR_BZIP2_EXPORT(jint, Libbzip2, setInput2)(JNI_STDARGS, jlong stream,
+                                            jlong src, jlong len)
 {
     int rc = BZ_PARAM_ERROR;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nip != 0 && s->nia != 0)
-        (*env)->ReleaseByteArrayElements(env, s->nia, s->nip, 0);
-    s->nia = 0;
-    s->nip = (*env)->GetByteArrayElements(env, src, 0);
-    if (s->nip != 0) {
-        s->nia = src;
-        s->bz.next_in  = (char *)s->nip + off;
-        s->bz.avail_in = len;
+    ACR_MFREE(s->next_array);
+    s->next_data  = J2P(src, char *);
+    if (s->next_data != 0) {
+        s->bz.next_in  = s->next_data;
+        s->bz.avail_in = (unsigned int)len;
         rc = BZ_OK;
     }
     return rc;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, nextinp1)(JNI_STDARGS, jbyteArray stream,
-                                        jobject src, jint off, jint len)
+ACR_BZIP2_EXPORT(void, Libbzip2, setInput3)(JNI_STDARGS, jlong stream)
 {
-    int rc = BZ_PARAM_ERROR;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nip != 0 && s->nia != 0)
-        (*env)->ReleaseByteArrayElements(env, s->nia, s->nip, 0);
-    s->nia = 0;
-    s->nip = (*env)->GetDirectBufferAddress(env, src);
-    if (s->nip != 0) {
-        s->bz.next_in  = (char *)s->nip + off;
-        s->bz.avail_in = len;
-        rc = BZ_OK;
+    ACR_MFREE(s->next_array);
+    /* Use internal buffer */
+    s->bz.next_in  = ACR_BZBUFF(s);
+    s->bz.avail_in = s->blen;    
+}
+
+ACR_BZIP2_EXPORT(jlong, Libbzip2, getTotalIn)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    jlong rv = (jlong)s->bz.total_in_hi32 << 32 | (jlong)s->bz.total_in_lo32;
+    
+    return rv;
+}
+
+ACR_BZIP2_EXPORT(jlong, Libbzip2, getTotalOut)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    return (jlong)s->bz.total_out_hi32 << 32 | (jlong)s->bz.total_out_lo32;
+}
+
+ACR_BZIP2_EXPORT(jint, Libbzip2, getAvailIn)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    return s->bz.avail_in;
+}
+
+ACR_BZIP2_EXPORT(jlong, Libbzip2, getAvailOut)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    return (jlong)s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(jboolean, Libbzip2, needsInput)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    if (s->state == BZ_RUN && s->bz.avail_in == 0)
+        return JNI_TRUE;
+    else
+        return JNI_FALSE;
+}
+
+ACR_BZIP2_EXPORT(jboolean, Libbzip2, flush)(JNI_STDARGS, long stream)
+{
+    jboolean rv = JNI_FALSE;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    if (s->state == BZ_RUN) {
+        s->state = BZ_FLUSH;
+        rv = JNI_TRUE;
     }
-    return rc;
+    return rv;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, nextinp2)(JNI_STDARGS, jbyteArray stream,
-                                        jlong src, jlong off, jlong len)
+ACR_BZIP2_EXPORT(jboolean, Libbzip2, finish)(JNI_STDARGS, long stream)
 {
-    int rc = BZ_PARAM_ERROR;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    jboolean rv = JNI_FALSE;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nip != 0 && s->nia != 0)
-        (*env)->ReleaseByteArrayElements(env, s->nia, s->nip, 0);
-    s->nia = 0;
-    s->nip = J2P(src, jbyte*);
-    if (s->nip != 0) {
-        s->bz.next_in  = (char *)s->nip + (ptrdiff_t)off;
-        s->bz.avail_in = (unsigned int)len;
-        rc = BZ_OK;
+    if (s->state == BZ_RUN) {
+        s->state = BZ_FINISH;
+        rv = JNI_TRUE;
     }
-    return rc;
+    return rv;
+}
+
+ACR_BZIP2_EXPORT(jboolean, Libbzip2, finished)(JNI_STDARGS, long stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    return s->eos;
+}
+
+ACR_BZIP2_EXPORT(void, Bzip2Deflater, close0)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+    BZ2_bzCompressEnd((bz_stream *)s);
+    AcrFree(s->next_array);
+    AcrFree(s);
 }
 
-ACR_BZIP2_EXPORT(void, Bzip2, nextinp3)(JNI_STDARGS, jbyteArray stream)
+ACR_BZIP2_EXPORT(jint, Bzip2Deflater, deflate0)(JNI_STDARGS, jlong stream)
 {
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    int rc;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nip != 0 && s->nia != 0)
-        (*env)->ReleaseByteArrayElements(env, s->nia, s->nip, 0);
-    s->nia = 0;
-    s->nip = 0;
+    if (s->eos)
+        return -1;
     /* Use internal buffer */
-    s->bz.next_in  = ACR_BZBUFF(s);
-    s->bz.avail_in = ACR_BZSIZE;
+    s->bz.next_out  = ACR_BZBUFF(s);
+    s->bz.avail_out = s->blen;
+    rc = BZ2_bzCompress((bz_stream *)s, s->state);
+    if (rc == BZ_SEQUENCE_ERROR || rc == BZ_PARAM_ERROR) {
+        ACR_THROW_MSG(ACR_EX_EILLEGAL, 0);
+        return -1;
+    }
+    else if (rc == BZ_RUN_OK)
+        s->state = BZ_RUN;
+    else if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    
+    return s->blen - s->bz.avail_out;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, nextout1)(JNI_STDARGS, jbyteArray stream,
-                                        jobject src, jint off, jint len)
+ACR_BZIP2_EXPORT(jint, Bzip2Deflater, deflate1)(JNI_STDARGS, jlong stream,
+                                                jbyteArray buf, jint off, jint len)
 {
-    int rc = BZ_PARAM_ERROR;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    int rc;
+    jbyte *next_out;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nop != 0 && s->noa != 0)
-        (*env)->ReleaseByteArrayElements(env, s->noa, s->nop, 0);
-    s->noa = 0;
-    s->nop = (*env)->GetDirectBufferAddress(env, src);
-    if (s->nop != 0) {
-        s->bz.next_out  = (char *)s->nop + off;
-        s->bz.avail_out = len;
-        rc = BZ_OK;
-    }
-    return rc;
+    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;
+    }
+
+    s->bz.next_out  = (char *)next_out + off;
+    s->bz.avail_out = len;
+    rc = BZ2_bzCompress((bz_stream *)s, s->state);
+    (*env)->ReleaseByteArrayElements(env, buf, next_out, 0);
+    if (rc == BZ_SEQUENCE_ERROR || rc == BZ_PARAM_ERROR) {
+        /* Report both errors as illegal */
+        ACR_THROW_MSG(ACR_EX_EILLEGAL, 0);
+        return -1;
+    }
+    else if (rc == BZ_RUN_OK)
+        s->state = BZ_RUN;
+    else if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    
+    return len - s->bz.avail_out;
 }
 
-ACR_BZIP2_EXPORT(jint, Bzip2, nextout2)(JNI_STDARGS, jbyteArray stream,
-                                        jlong src, jlong off, jlong len)
+ACR_BZIP2_EXPORT(jint, Bzip2Deflater, deflate2)(JNI_STDARGS, jlong stream,
+                                                jobject buf, jint off, jint len)
 {
-    int rc = BZ_PARAM_ERROR;
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    int rc;
+    char *next_out;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nop != 0 && s->noa != 0)
-        (*env)->ReleaseByteArrayElements(env, s->noa, s->nop, 0);
-    s->noa = 0;
-    s->nop = J2P(src, jbyte*);
-    if (s->nop != 0) {
-        s->bz.next_out  = (char *)s->nop + (ptrdiff_t)off;
-        s->bz.avail_out = (unsigned int)len;
-        rc = BZ_OK;
+    if (s->eos)
+        return -1;
+    next_out = (*env)->GetDirectBufferAddress(env, buf);
+    if (next_out == 0) {
+        ACR_THROW_MSG(ACR_EX_EINVAL, 0);
+        return -1;
+    }
+
+    s->bz.next_out  = next_out + off;
+    s->bz.avail_out = len;
+    rc = BZ2_bzCompress((bz_stream *)s, s->state);
+    if (rc == BZ_SEQUENCE_ERROR || rc == BZ_PARAM_ERROR) {
+        /* Report both errors as illegal */
+        ACR_THROW_MSG(ACR_EX_EILLEGAL, 0);
+        return -1;
+    }
+    else if (rc == BZ_RUN_OK)
+        s->state = BZ_RUN;
+    else if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+
+    return len - s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(jlong, Bzip2Deflater, deflate3)(JNI_STDARGS, jlong stream,
+                                                 jlong buf, jlong len)
+{
+    int rc;
+    char *next_out  = J2P(buf, char *);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    if (s->eos)
+        return -1;
+
+    s->bz.next_out  = next_out;
+    s->bz.avail_out = (unsigned int)len;
+    rc = BZ2_bzCompress((bz_stream *)s, s->state);
+    if (rc == BZ_SEQUENCE_ERROR || rc == BZ_PARAM_ERROR) {
+        /* Report both errors as illegal */
+        ACR_THROW_MSG(ACR_EX_EILLEGAL, 0);
+        return -1;
     }
-    return rc;
+    else if (rc == BZ_RUN_OK)
+        s->state = BZ_RUN;
+    else if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+
+    return len - s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(void, Bzip2Inflater, close0)(JNI_STDARGS, jlong stream)
+{
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    BZ2_bzDecompressEnd((bz_stream *)s);
+    AcrFree(s->next_array);
+    AcrFree(s);    
 }
 
-ACR_BZIP2_EXPORT(void, Bzip2, nextout3)(JNI_STDARGS, jbyteArray stream)
+ACR_BZIP2_EXPORT(jint, Bzip2Inflater, inflate0)(JNI_STDARGS, jlong stream)
 {
-    acr_bzstream *s = JARRAY_CRITICAL(acr_bzstream, stream);
+    int rc;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
 
-    if (s->nop != 0 && s->noa != 0)
-        (*env)->ReleaseByteArrayElements(env, s->noa, s->nop, 0);
-    s->noa = 0;
-    s->nop = 0;
+    if (s->eos)
+        return -1;
     /* Use internal buffer */
     s->bz.next_out  = ACR_BZBUFF(s);
-    s->bz.avail_out = ACR_BZSIZE;
+    s->bz.avail_out = s->blen;
+    rc = BZ2_bzDecompress((bz_stream *)s);
+    if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    else if (rc == BZ_MEM_ERROR)
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+    else if (rc != BZ_OK)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, bz_errmsg(rc));
+    
+    return s->blen - s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(jint, Bzip2Inflater, inflate1)(JNI_STDARGS, jlong stream,
+                                                jbyteArray buf, jint off, jint len)
+{
+    int rc = BZ_PARAM_ERROR;
+    char *next_out;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    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->bz.next_out  = next_out + off;
+    s->bz.avail_out = len;
+    rc = BZ2_bzDecompress((bz_stream *)s);
+    (*env)->ReleasePrimitiveArrayCritical(env, buf, next_out, 0);
+    if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    else if (rc == BZ_MEM_ERROR)
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+    else if (rc != BZ_OK)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, bz_errmsg(rc));    
+    return len - s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(jint, Bzip2Inflater, inflate2)(JNI_STDARGS, jlong stream,
+                                                jobject buf, jint off, jint len)
+{
+    int rc = BZ_PARAM_ERROR;
+    char *next_out;
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    if (s->eos)
+        return -1;
+    next_out = (*env)->GetDirectBufferAddress(env, buf);
+    if (next_out == 0) {
+        ACR_THROW_MSG(ACR_EX_EINVAL, 0);
+        return -1;
+    }
+
+    s->bz.next_out  = next_out + off;
+    s->bz.avail_out = len;
+    rc = BZ2_bzDecompress((bz_stream *)s);
+    if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    else if (rc == BZ_MEM_ERROR)
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+    else if (rc != BZ_OK)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, bz_errmsg(rc));
+    return len - s->bz.avail_out;
+}
+
+ACR_BZIP2_EXPORT(jlong, Bzip2Inflater, inflate3)(JNI_STDARGS, jlong stream,
+                                                 jlong buf, jlong len)
+{
+    int rc = BZ_PARAM_ERROR;
+    char *next_out  = J2P(buf, char *);
+    acr_bzstream *s = J2P(stream, acr_bzstream *);
+
+    if (s->eos)
+        return -1;
+    s->bz.next_out  = next_out;
+    s->bz.avail_out = (unsigned int)len;
+    rc = BZ2_bzDecompress((bz_stream *)s);
+    if (rc == BZ_STREAM_END)
+        s->eos = JNI_TRUE;
+    else if (rc == BZ_MEM_ERROR)
+        ACR_THROW_MSG(ACR_EX_ENOMEM, 0);
+    else if (rc != BZ_OK)
+        ACR_THROW_MSG(ACR_EX_EILSEQ, bz_errmsg(rc));
+    return len - s->bz.avail_out;
 }

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java?rev=1158327&r1=1158326&r2=1158327&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestBzip2.java
Tue Aug 16 15:40:07 2011
@@ -23,21 +23,57 @@ import org.testng.Assert;
 
 public class TestBzip2 extends Assert
 {
+    private static final String loremIpsum =
+    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
+    "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
+    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris " +
+    "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " +
+    "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla " +
+    "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in " +
+    "culpa qui officia deserunt mollit anim id est laborum.";
 
+    
     @Test(groups = { "core" })
     public void bufToBuffCompressing()
+        throws Exception
     {
-        byte[] cd = new byte[1000];
-        byte[] dd = new byte[1000];
-        byte[] dc = new byte[1000];
+        byte[] cd = loremIpsum.getBytes();
+        byte[] dd = new byte[500];
+        byte[] dc = new byte[loremIpsum.length()];
 
-        for (int i = 0; i < 1000; i++)
-            cd[i] = (byte)(i % 10);
-
-        int sc = Bzip2.buffToBuffCompress(cd, 0, dd, 0, 1000, 9, 30);
-        assertTrue(sc != 0);
+        int sc = Bzip2.buffToBuffCompress(cd, 0, dd, 0, cd.length, 9, 30);
+        // 302 is pre-computed length.
+        assertEquals(sc, 302);
         int sd = Bzip2.buffToBuffDecompress(dd, 0, dc, 0, sc, false);
-        assertEquals(sd, 1000);
+        assertEquals(sd, loremIpsum.length());
+    }
+
+    @Test(groups = { "core" })
+    public void bzDeflater()
+        throws Exception
+    {
+        byte[] cd = loremIpsum.getBytes();
+
+        Bzip2Deflater bz = new Bzip2Deflater();
+        bz.setInput(cd);
+        byte[] dd = new byte[50];
+
+        int i;
+        for (i = 0; i < 10; i++) {
+            int rc = bz.deflate(dd);
+            if (i == 0) {
+                // Set finish mode
+                bz.finish();
+            }
+            if (bz.finished()) {
+                // Finished compressing
+                break;
+            }
+        }
+        assertEquals(i, 7);
+        // 302 is pre-computed length.
+        assertEquals(bz.getTotalOut(), 302);
+        bz.close();
     }
 
 }



Mime
View raw message