From 9543cd3cbd06d3270c0258935aa3f9772521475e Mon Sep 17 00:00:00 2001 From: sherman Date: Fri, 3 Aug 2012 13:40:03 -0700 Subject: [PATCH] 7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native Summary: re-implemented getBytesRead/Writtten() at java level Reviewed-by: andrew, alanb --- make/java/zip/mapfile-vers | 4 - src/share/classes/java/util/zip/Deflater.java | 18 ++-- src/share/classes/java/util/zip/Inflater.java | 15 ++- src/share/native/java/util/zip/Deflater.c | 12 --- src/share/native/java/util/zip/Inflater.c | 12 --- .../java/util/zip/zlib-1.2.5/compress.c | 2 +- .../native/java/util/zip/zlib-1.2.5/inflate.c | 2 +- .../zip/zlib-1.2.5/patches/ChangeLog_java | 9 -- .../native/java/util/zip/zlib-1.2.5/zlib.h | 4 +- test/java/util/zip/TotalInOut.java | 97 +++++++++++++++++++ 10 files changed, 123 insertions(+), 52 deletions(-) create mode 100644 test/java/util/zip/TotalInOut.java diff --git a/make/java/zip/mapfile-vers b/make/java/zip/mapfile-vers index 3f95d470b..367e9058d 100644 --- a/make/java/zip/mapfile-vers +++ b/make/java/zip/mapfile-vers @@ -37,16 +37,12 @@ SUNWprivate_1.1 { 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; diff --git a/src/share/classes/java/util/zip/Deflater.java b/src/share/classes/java/util/zip/Deflater.java index 0f001df32..e5e9b277d 100644 --- a/src/share/classes/java/util/zip/Deflater.java +++ b/src/share/classes/java/util/zip/Deflater.java @@ -79,6 +79,8 @@ class Deflater { 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 @@ class Deflater { 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 @@ class Deflater { public long getBytesRead() { synchronized (zsRef) { ensureOpen(); - return getBytesRead(zsRef.address()); + return bytesRead; } } @@ -488,7 +495,7 @@ class Deflater { public long getBytesWritten() { synchronized (zsRef) { ensureOpen(); - return getBytesWritten(zsRef.address()); + return bytesWritten; } } @@ -503,6 +510,7 @@ class Deflater { finish = false; finished = false; off = len = 0; + bytesRead = bytesWritten = 0; } } @@ -543,8 +551,6 @@ class Deflater { 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); } diff --git a/src/share/classes/java/util/zip/Inflater.java b/src/share/classes/java/util/zip/Inflater.java index 1206be727..9a7976cba 100644 --- a/src/share/classes/java/util/zip/Inflater.java +++ b/src/share/classes/java/util/zip/Inflater.java @@ -78,6 +78,8 @@ class Inflater { 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 @@ class Inflater { } 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 @@ class Inflater { public long getBytesRead() { synchronized (zsRef) { ensureOpen(); - return getBytesRead(zsRef.address()); + return bytesRead; } } @@ -333,7 +339,7 @@ class Inflater { public long getBytesWritten() { synchronized (zsRef) { ensureOpen(); - return getBytesWritten(zsRef.address()); + return bytesWritten; } } @@ -348,6 +354,7 @@ class Inflater { finished = false; needDict = false; off = len = 0; + bytesRead = bytesWritten = 0; } } @@ -395,8 +402,6 @@ class Inflater { 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); } diff --git a/src/share/native/java/util/zip/Deflater.c b/src/share/native/java/util/zip/Deflater.c index 6cad24e48..3977af468 100644 --- a/src/share/native/java/util/zip/Deflater.c +++ b/src/share/native/java/util/zip/Deflater.c @@ -215,18 +215,6 @@ Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr) 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) { diff --git a/src/share/native/java/util/zip/Inflater.c b/src/share/native/java/util/zip/Inflater.c index e5296fcf4..837baef75 100644 --- a/src/share/native/java/util/zip/Inflater.c +++ b/src/share/native/java/util/zip/Inflater.c @@ -174,18 +174,6 @@ Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr) 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) { diff --git a/src/share/native/java/util/zip/zlib-1.2.5/compress.c b/src/share/native/java/util/zip/zlib-1.2.5/compress.c index c05285a77..9d8713f94 100644 --- a/src/share/native/java/util/zip/zlib-1.2.5/compress.c +++ b/src/share/native/java/util/zip/zlib-1.2.5/compress.c @@ -75,7 +75,7 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) deflateEnd(&stream); return err == Z_OK ? Z_BUF_ERROR : err; } - *destLen = (uLong)stream.total_out; + *destLen = stream.total_out; err = deflateEnd(&stream); return err; diff --git a/src/share/native/java/util/zip/zlib-1.2.5/inflate.c b/src/share/native/java/util/zip/zlib-1.2.5/inflate.c index 98e210a8f..6e172217b 100644 --- a/src/share/native/java/util/zip/zlib-1.2.5/inflate.c +++ b/src/share/native/java/util/zip/zlib-1.2.5/inflate.c @@ -1370,7 +1370,7 @@ int ZEXPORT inflateSync(strm) 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; diff --git a/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java b/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java index 8d6957f99..6f013dbb2 100644 --- a/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java +++ b/src/share/native/java/util/zip/zlib-1.2.5/patches/ChangeLog_java @@ -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; diff --git a/src/share/native/java/util/zip/zlib-1.2.5/zlib.h b/src/share/native/java/util/zip/zlib-1.2.5/zlib.h index b847c100b..b3617e58b 100644 --- a/src/share/native/java/util/zip/zlib-1.2.5/zlib.h +++ b/src/share/native/java/util/zip/zlib-1.2.5/zlib.h @@ -109,11 +109,11 @@ struct internal_state; 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 */ diff --git a/test/java/util/zip/TotalInOut.java b/test/java/util/zip/TotalInOut.java new file mode 100644 index 000000000..8e6a1e862 --- /dev/null +++ b/test/java/util/zip/TotalInOut.java @@ -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");} +} -- GitLab