提交 9543cd3c 编写于 作者: S sherman

7188852: Move implementation of De/Inflater.getBytesRead/Writtten() to java from native

Summary: re-implemented getBytesRead/Writtten() at java level
Reviewed-by: andrew, alanb
上级 cd762851
......@@ -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;
......
......@@ -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);
}
......@@ -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);
}
......@@ -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)
{
......
......@@ -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)
{
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......@@ -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 */
......
/*
* 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");}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册