Author: mturk
Date: Tue Aug 30 11:07:33 2011
New Revision: 1163171

URL: http://svn.apache.org/viewvc?rev=1163171&view=rev
Log:
Add more to the zlib api wrapper

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

Added: 
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=1163171&view=auto
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
 (added)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.java
 Tue Aug 30 11:07:33 2011
@@ -0,0 +1,79 @@
+/* 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.utilzlib;
+
+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.util.StringManager;
+
+/**
+ * Private libz wrapper.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+final class ZlibImpl
+{
+
+    /* flush modes */
+    public  static final int   Z_PARTIAL_FLUSH     = 1;
+    public  static final int   Z_SYNC_FLUSH        = 2;
+    public  static final int   Z_FULL_FLUSH        = 3;
+
+
+    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     setInput0(long handle, byte[] buf, int off, 
int len);
+    public  static native int     setInput1(long handle, ByteBuffer buf, int 
off, int len);
+    public  static native int     setInput2(long handle, long buf, int len);
+    public  static native void    setInput3(long handle, int len);
+        
+    public  static native int     getAvailIn(long handle);
+    public  static native long    getAvailOut(long handle);
+    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, int mode);
+    public  static native boolean finish(long handle);
+    public  static native boolean finished(long handle);
+
+    public  static final  int     DEFAULT_BUFFER_SIZE = 4096;
+    public  static final  int     DEFAULT_WORK_FACTOR = 30;
+
+    public  static final  int     SIZEOF_Z_STREAM;
+    static {
+        SIZEOF_Z_STREAM = init0();
+    }
+
+    private ZlibImpl()
+    {
+        // No instance
+    }
+}
+

Propchange: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/zlib/ZlibImpl.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=1163171&r1=1163170&r2=1163171&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/zlib.c Tue Aug 30 
11:07:33 2011
@@ -23,17 +23,17 @@
 
 #define ACR_ZLIB_EXPORT(RT, CL, MN)  \
     ACR_JNIEXPORT RT JNICALL 
Java_org_apache_commons_runtime_util_zlib_##CL##_##MN
-#define ACR_ZBUFF(S)   ((char *)(S) + ACR_ZSIZE)
+#define ACR_ZBUFF(S)   ((Bytef *)(S) + ACR_ZSIZE)
 
 static int ACR_ZSIZE;
 
 typedef struct acr_zstream {
     z_stream            zs;
-    unsigned int        zlen;
+    unsigned int        blen;
     int                 state;
     jboolean            eos;
     void               *next_array;
-    char               *next_data;
+    Bytef              *next_data;
 } acr_zstream;
 
 
@@ -218,7 +218,7 @@ ACR_ZLIB_EXPORT(jlong, ZlibImpl, newHand
     acr_zstream *s;
     s = ACR_EALLOC(acr_zstream, zsize);
     if (s != 0)
-        s->zlen = zsize;
+        s->blen = zsize;
     return P2J(s);
 }
 
@@ -254,6 +254,66 @@ ACR_ZLIB_EXPORT(jlong, ZlibImpl, deflate
     return rc;
 }
 
+ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput0)(JNI_STDARGS, jlong stream,
+                                           jbyteArray src, jint off, jint len)
+{
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    s->next_data = ACR_REALLOC(Bytef, s->next_array, len);
+    if (s->next_data == 0)
+        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->zs.avail_in = len;
+    return Z_OK;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput1)(JNI_STDARGS, jlong stream,
+                                             jobject src, jint off, jint len)
+{
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    ACR_MFREE(s->next_array);
+    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->zs.avail_in = len;
+    return Z_OK;
+}
+
+ACR_ZLIB_EXPORT(jint, ZlibImpl, setInput2)(JNI_STDARGS, jlong stream,
+                                             jlong src, jint len)
+{
+    int rc = Z_BUF_ERROR;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    ACR_MFREE(s->next_array);
+    s->next_data  = J2P(src, Bytef *);
+    if (s->next_data != 0) {
+        s->zs.next_in  = s->next_data;
+        s->zs.avail_in = len;
+        rc = Z_OK;
+    }
+    return rc;
+}
+
+ACR_ZLIB_EXPORT(void, ZlibImpl, setInput3)(JNI_STDARGS, jlong stream,
+                                             jint len)
+{
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    ACR_MFREE(s->next_array);
+    /* Use internal buffer */
+    s->zs.next_in  = ACR_ZBUFF(s);
+    if (len < 0)
+        s->zs.avail_in = s->blen;
+    else
+        s->zs.avail_in = len;
+}
+
+
 
 ACR_ZLIB_EXPORT(jlong, ZlibImpl, getTotalIn)(JNI_STDARGS, jlong stream)
 {
@@ -303,6 +363,18 @@ ACR_ZLIB_EXPORT(jboolean, ZlibImpl, fini
     return rv;
 }
 
+ACR_ZLIB_EXPORT(jboolean, ZlibImpl, flush)(JNI_STDARGS, jlong stream, jint 
mode)
+{
+    jboolean rv = JNI_FALSE;
+    acr_zstream *s = J2P(stream, acr_zstream *);
+
+    if (s->state == 0) {
+        s->state = mode;
+        rv = JNI_TRUE;
+    }
+    return rv;
+}
+
 ACR_ZLIB_EXPORT(jboolean, ZlibImpl, finished)(JNI_STDARGS, jlong stream)
 {
     acr_zstream *s = J2P(stream, acr_zstream *);


Reply via email to