changeset 5863:1468b0af0d06

7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native Summary: re-implemented getBytesRead/Writtten() at java level Reviewed-by: andrew, alanb
author sherman
date Fri, 03 Aug 2012 13:40:03 -0700
parents 8a82e5f9c47f
children 3521fcad4b5f
files make/java/zip/mapfile-vers src/share/classes/java/util/zip/Deflater.java src/share/classes/java/util/zip/Inflater.java src/share/native/java/util/zip/Deflater.c src/share/native/java/util/zip/Inflater.c src/share/native/java/util/zip/zlib-1.2.5/compress.c src/share/native/java/util/zip/zlib-1.2.5/inflate.c src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java src/share/native/java/util/zip/zlib-1.2.5/zlib.h test/java/util/zip/TotalInOut.java
diffstat 10 files changed, 123 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/make/java/zip/mapfile-vers	Thu Aug 02 18:12:18 2012 -0700
+++ b/make/java/zip/mapfile-vers	Fri Aug 03 13:40:03 2012 -0700
@@ -37,16 +37,12 @@
 		Java_java_util_zip_Deflater_deflateBytes;
 		Java_java_util_zip_Deflater_end;
 		Java_java_util_zip_Deflater_getAdler;
-		Java_java_util_zip_Deflater_getBytesRead;
-		Java_java_util_zip_Deflater_getBytesWritten;
 		Java_java_util_zip_Deflater_init;
 		Java_java_util_zip_Deflater_initIDs;
 		Java_java_util_zip_Deflater_reset;
 		Java_java_util_zip_Deflater_setDictionary;
 		Java_java_util_zip_Inflater_end;
 		Java_java_util_zip_Inflater_getAdler;
-		Java_java_util_zip_Inflater_getBytesRead;
-		Java_java_util_zip_Inflater_getBytesWritten;
 		Java_java_util_zip_Inflater_inflateBytes;
 		Java_java_util_zip_Inflater_init;
 		Java_java_util_zip_Inflater_initIDs;
--- a/src/share/classes/java/util/zip/Deflater.java	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/classes/java/util/zip/Deflater.java	Fri Aug 03 13:40:03 2012 -0700
@@ -79,6 +79,8 @@
     private int level, strategy;
     private boolean setParams;
     private boolean finish, finished;
+    private long bytesRead;
+    private long bytesWritten;
 
     /**
      * Compression method for the deflate algorithm (the only one currently
@@ -423,8 +425,13 @@
         synchronized (zsRef) {
             ensureOpen();
             if (flush == NO_FLUSH || flush == SYNC_FLUSH ||
-                flush == FULL_FLUSH)
-                return deflateBytes(zsRef.address(), b, off, len, flush);
+                flush == FULL_FLUSH) {
+                int thisLen = this.len;
+                int n = deflateBytes(zsRef.address(), b, off, len, flush);
+                bytesWritten += n;
+                bytesRead += (thisLen - this.len);
+                return n;
+            }
             throw new IllegalArgumentException();
         }
     }
@@ -462,7 +469,7 @@
     public long getBytesRead() {
         synchronized (zsRef) {
             ensureOpen();
-            return getBytesRead(zsRef.address());
+            return bytesRead;
         }
     }
 
@@ -488,7 +495,7 @@
     public long getBytesWritten() {
         synchronized (zsRef) {
             ensureOpen();
-            return getBytesWritten(zsRef.address());
+            return bytesWritten;
         }
     }
 
@@ -503,6 +510,7 @@
             finish = false;
             finished = false;
             off = len = 0;
+            bytesRead = bytesWritten = 0;
         }
     }
 
@@ -543,8 +551,6 @@
     private native int deflateBytes(long addr, byte[] b, int off, int len,
                                     int flush);
     private native static int getAdler(long addr);
-    private native static long getBytesRead(long addr);
-    private native static long getBytesWritten(long addr);
     private native static void reset(long addr);
     private native static void end(long addr);
 }
--- a/src/share/classes/java/util/zip/Inflater.java	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/classes/java/util/zip/Inflater.java	Fri Aug 03 13:40:03 2012 -0700
@@ -78,6 +78,8 @@
     private int off, len;
     private boolean finished;
     private boolean needDict;
+    private long bytesRead;
+    private long bytesWritten;
 
     private static final byte[] defaultBuf = new byte[0];
 
@@ -253,7 +255,11 @@
         }
         synchronized (zsRef) {
             ensureOpen();
-            return inflateBytes(zsRef.address(), b, off, len);
+            int thisLen = this.len;
+            int n = inflateBytes(zsRef.address(), b, off, len);
+            bytesWritten += n;
+            bytesRead += (thisLen - this.len);
+            return n;
         }
     }
 
@@ -307,7 +313,7 @@
     public long getBytesRead() {
         synchronized (zsRef) {
             ensureOpen();
-            return getBytesRead(zsRef.address());
+            return bytesRead;
         }
     }
 
@@ -333,7 +339,7 @@
     public long getBytesWritten() {
         synchronized (zsRef) {
             ensureOpen();
-            return getBytesWritten(zsRef.address());
+            return bytesWritten;
         }
     }
 
@@ -348,6 +354,7 @@
             finished = false;
             needDict = false;
             off = len = 0;
+            bytesRead = bytesWritten = 0;
         }
     }
 
@@ -395,8 +402,6 @@
     private native int inflateBytes(long addr, byte[] b, int off, int len)
             throws DataFormatException;
     private native static int getAdler(long addr);
-    private native static long getBytesRead(long addr);
-    private native static long getBytesWritten(long addr);
     private native static void reset(long addr);
     private native static void end(long addr);
 }
--- a/src/share/native/java/util/zip/Deflater.c	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/Deflater.c	Fri Aug 03 13:40:03 2012 -0700
@@ -215,18 +215,6 @@
     return ((z_stream *)jlong_to_ptr(addr))->adler;
 }
 
-JNIEXPORT jlong JNICALL
-Java_java_util_zip_Deflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
-{
-    return ((z_stream *)jlong_to_ptr(addr))->total_in;
-}
-
-JNIEXPORT jlong JNICALL
-Java_java_util_zip_Deflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
-{
-    return ((z_stream *)jlong_to_ptr(addr))->total_out;
-}
-
 JNIEXPORT void JNICALL
 Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
 {
--- a/src/share/native/java/util/zip/Inflater.c	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/Inflater.c	Fri Aug 03 13:40:03 2012 -0700
@@ -174,18 +174,6 @@
     return ((z_stream *)jlong_to_ptr(addr))->adler;
 }
 
-JNIEXPORT jlong JNICALL
-Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
-{
-    return ((z_stream *)jlong_to_ptr(addr))->total_in;
-}
-
-JNIEXPORT jlong JNICALL
-Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
-{
-    return ((z_stream *)jlong_to_ptr(addr))->total_out;
-}
-
 JNIEXPORT void JNICALL
 Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
 {
--- a/src/share/native/java/util/zip/zlib-1.2.5/compress.c	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/zlib-1.2.5/compress.c	Fri Aug 03 13:40:03 2012 -0700
@@ -75,7 +75,7 @@
         deflateEnd(&stream);
         return err == Z_OK ? Z_BUF_ERROR : err;
     }
-    *destLen = (uLong)stream.total_out;
+    *destLen = stream.total_out;
 
     err = deflateEnd(&stream);
     return err;
--- a/src/share/native/java/util/zip/zlib-1.2.5/inflate.c	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/zlib-1.2.5/inflate.c	Fri Aug 03 13:40:03 2012 -0700
@@ -1370,7 +1370,7 @@
 z_streamp strm;
 {
     unsigned len;               /* number of bytes to look at or looked at */
-    long long in, out;      /* temporary to save total_in and total_out */
+    unsigned long in, out;      /* temporary to save total_in and total_out */
     unsigned char buf[4];       /* to restore bit buffer to byte string */
     struct inflate_state FAR *state;
 
--- a/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java	Fri Aug 03 13:40:03 2012 -0700
@@ -8,12 +8,3 @@
 
 (3)updated crc32.c/crc32()
    unsigned long      -> uLong
-
-(4)updated zlib.h (to support > 4G zipfile):
-   total_in/out: uLong -> long long
-
-(5)updated inflate.c/inflateSync()
-   unsigned long in, out; -->  long long in, out;
-
-(6)updated compress.c/uncompr.c
-   *destLen = stream.total_out; --> *destLen = (uLong)stream.total_out;
--- a/src/share/native/java/util/zip/zlib-1.2.5/zlib.h	Thu Aug 02 18:12:18 2012 -0700
+++ b/src/share/native/java/util/zip/zlib-1.2.5/zlib.h	Fri Aug 03 13:40:03 2012 -0700
@@ -109,11 +109,11 @@
 typedef struct z_stream_s {
     Bytef    *next_in;  /* next input byte */
     uInt     avail_in;  /* number of bytes available at next_in */
-    long long total_in; /* total nb of input bytes read so far */
+    uLong    total_in;  /* total nb of input bytes read so far */
 
     Bytef    *next_out; /* next output byte should be put there */
     uInt     avail_out; /* remaining free space at next_out */
-    long long total_out;/* total nb of bytes output so far */
+    uLong    total_out; /* total nb of bytes output so far */
 
     char     *msg;      /* last error message, NULL if no error */
     struct internal_state FAR *state; /* not visible by applications */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/util/zip/TotalInOut.java	Fri Aug 03 13:40:03 2012 -0700
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 7188852
+ * @summary Test De/Inflater.getBytesRead/Written()
+ */
+
+import java.io.*;
+import java.util.*;
+import java.util.zip.*;
+
+
+public class TotalInOut {
+     static final int BUF_SIZE= 1 * 1024 * 1024;
+
+     static void realMain (String[] args) throws Throwable {
+         long dataSize = 128L * 1024L * 1024L;      // 128MB
+         if (args.length > 0 && "large".equals(args[0]))
+             dataSize = 5L * 1024L * 1024L * 1024L; //  5GB
+
+         Deflater deflater = new Deflater();
+         Inflater inflater = new Inflater();
+
+         byte[] dataIn = new byte[BUF_SIZE];
+         byte[] dataOut = new byte[BUF_SIZE];
+         byte[] tmp = new byte[BUF_SIZE];
+
+         Random r = new Random();
+         r.nextBytes(dataIn);
+         long bytesReadDef    = 0;
+         long bytesWrittenDef = 0;
+         long bytesReadInf    = 0;
+         long bytesWrittenInf = 0;
+
+         deflater.setInput(dataIn, 0, dataIn.length);
+         while (bytesReadDef < dataSize || bytesWrittenInf < dataSize) {
+             int len = r.nextInt(BUF_SIZE/2) + BUF_SIZE / 2;
+             if (deflater.needsInput()) {
+                 bytesReadDef += dataIn.length;
+                 check(bytesReadDef == deflater.getBytesRead());
+                 deflater.setInput(dataIn, 0, dataIn.length);
+             }
+             int n = deflater.deflate(tmp, 0, len);
+             bytesWrittenDef += n;
+             check(bytesWrittenDef == deflater.getBytesWritten());
+
+             inflater.setInput(tmp, 0, n);
+             bytesReadInf += n;
+             while (!inflater.needsInput()) {
+                 bytesWrittenInf += inflater.inflate(dataOut, 0, dataOut.length);
+                 check(bytesWrittenInf == inflater.getBytesWritten());
+             }
+             check(bytesReadInf == inflater.getBytesRead());
+         }
+     }
+
+     //--------------------- Infrastructure ---------------------------
+     static volatile int passed = 0, failed = 0;
+     static void pass() {passed++;}
+     static void pass(String msg) {System.out.println(msg); passed++;}
+     static void fail() {failed++; Thread.dumpStack();}
+     static void fail(String msg) {System.out.println(msg); fail();}
+     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
+     static void unexpected(Throwable t, String msg) {
+         System.out.println(msg); failed++; t.printStackTrace();}
+     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
+     static void equal(Object x, Object y) {
+          if (x == null ? y == null : x.equals(y)) pass();
+          else fail(x + " not equal to " + y);}
+     public static void main(String[] args) throws Throwable {
+          try {realMain(args);} catch (Throwable t) {unexpected(t);}
+          System.out.println("\nPassed = " + passed + " failed = " + failed);
+          if (failed > 0) throw new AssertionError("Some tests failed");}
+}