提交 da60e031 编写于 作者: M mchung

Merge

/* /*
* Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -72,7 +72,8 @@ package java.util.zip; ...@@ -72,7 +72,8 @@ package java.util.zip;
*/ */
public public
class Deflater { class Deflater {
private long strm;
private final ZStreamRef zsRef;
private byte[] buf = new byte[0]; private byte[] buf = new byte[0];
private int off, len; private int off, len;
private int level, strategy; private int level, strategy;
...@@ -165,7 +166,7 @@ class Deflater { ...@@ -165,7 +166,7 @@ class Deflater {
public Deflater(int level, boolean nowrap) { public Deflater(int level, boolean nowrap) {
this.level = level; this.level = level;
this.strategy = DEFAULT_STRATEGY; this.strategy = DEFAULT_STRATEGY;
strm = init(level, DEFAULT_STRATEGY, nowrap); this.zsRef = new ZStreamRef(init(level, DEFAULT_STRATEGY, nowrap));
} }
/** /**
...@@ -193,16 +194,18 @@ class Deflater { ...@@ -193,16 +194,18 @@ class Deflater {
* @param len the length of the data * @param len the length of the data
* @see Deflater#needsInput * @see Deflater#needsInput
*/ */
public synchronized void setInput(byte[] b, int off, int len) { public void setInput(byte[] b, int off, int len) {
if (b== null) { if (b== null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
this.buf = b; synchronized (zsRef) {
this.off = off; this.buf = b;
this.len = len; this.off = off;
this.len = len;
}
} }
/** /**
...@@ -227,14 +230,17 @@ class Deflater { ...@@ -227,14 +230,17 @@ class Deflater {
* @see Inflater#inflate * @see Inflater#inflate
* @see Inflater#getAdler * @see Inflater#getAdler
*/ */
public synchronized void setDictionary(byte[] b, int off, int len) { public void setDictionary(byte[] b, int off, int len) {
if (strm == 0 || b == null) { if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
setDictionary(strm, b, off, len); synchronized (zsRef) {
ensureOpen();
setDictionary(zsRef.address(), b, off, len);
}
} }
/** /**
...@@ -257,7 +263,7 @@ class Deflater { ...@@ -257,7 +263,7 @@ class Deflater {
* @exception IllegalArgumentException if the compression strategy is * @exception IllegalArgumentException if the compression strategy is
* invalid * invalid
*/ */
public synchronized void setStrategy(int strategy) { public void setStrategy(int strategy) {
switch (strategy) { switch (strategy) {
case DEFAULT_STRATEGY: case DEFAULT_STRATEGY:
case FILTERED: case FILTERED:
...@@ -266,9 +272,11 @@ class Deflater { ...@@ -266,9 +272,11 @@ class Deflater {
default: default:
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
if (this.strategy != strategy) { synchronized (zsRef) {
this.strategy = strategy; if (this.strategy != strategy) {
setParams = true; this.strategy = strategy;
setParams = true;
}
} }
} }
...@@ -277,13 +285,15 @@ class Deflater { ...@@ -277,13 +285,15 @@ class Deflater {
* @param level the new compression level (0-9) * @param level the new compression level (0-9)
* @exception IllegalArgumentException if the compression level is invalid * @exception IllegalArgumentException if the compression level is invalid
*/ */
public synchronized void setLevel(int level) { public void setLevel(int level) {
if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) { if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
throw new IllegalArgumentException("invalid compression level"); throw new IllegalArgumentException("invalid compression level");
} }
if (this.level != level) { synchronized (zsRef) {
this.level = level; if (this.level != level) {
setParams = true; this.level = level;
setParams = true;
}
} }
} }
...@@ -301,8 +311,10 @@ class Deflater { ...@@ -301,8 +311,10 @@ class Deflater {
* When called, indicates that compression should end with the current * When called, indicates that compression should end with the current
* contents of the input buffer. * contents of the input buffer.
*/ */
public synchronized void finish() { public void finish() {
finish = true; synchronized (zsRef) {
finish = true;
}
} }
/** /**
...@@ -311,8 +323,10 @@ class Deflater { ...@@ -311,8 +323,10 @@ class Deflater {
* @return true if the end of the compressed data output stream has * @return true if the end of the compressed data output stream has
* been reached * been reached
*/ */
public synchronized boolean finished() { public boolean finished() {
return finished; synchronized (zsRef) {
return finished;
}
} }
/** /**
...@@ -399,26 +413,31 @@ class Deflater { ...@@ -399,26 +413,31 @@ class Deflater {
* @throws IllegalArgumentException if the flush mode is invalid * @throws IllegalArgumentException if the flush mode is invalid
* @since 1.7 * @since 1.7
*/ */
public synchronized int deflate(byte[] b, int off, int len, int flush) { public int deflate(byte[] b, int off, int len, int flush) {
if (b == null) { if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
if (flush == NO_FLUSH || flush == SYNC_FLUSH || synchronized (zsRef) {
flush == FULL_FLUSH) ensureOpen();
return deflateBytes(b, off, len, flush); if (flush == NO_FLUSH || flush == SYNC_FLUSH ||
throw new IllegalArgumentException(); flush == FULL_FLUSH)
return deflateBytes(zsRef.address(), b, off, len, flush);
throw new IllegalArgumentException();
}
} }
/** /**
* Returns the ADLER-32 value of the uncompressed data. * Returns the ADLER-32 value of the uncompressed data.
* @return the ADLER-32 value of the uncompressed data * @return the ADLER-32 value of the uncompressed data
*/ */
public synchronized int getAdler() { public int getAdler() {
ensureOpen(); synchronized (zsRef) {
return getAdler(strm); ensureOpen();
return getAdler(zsRef.address());
}
} }
/** /**
...@@ -440,9 +459,11 @@ class Deflater { ...@@ -440,9 +459,11 @@ class Deflater {
* @return the total (non-negative) number of uncompressed bytes input so far * @return the total (non-negative) number of uncompressed bytes input so far
* @since 1.5 * @since 1.5
*/ */
public synchronized long getBytesRead() { public long getBytesRead() {
ensureOpen(); synchronized (zsRef) {
return getBytesRead(strm); ensureOpen();
return getBytesRead(zsRef.address());
}
} }
/** /**
...@@ -464,21 +485,25 @@ class Deflater { ...@@ -464,21 +485,25 @@ class Deflater {
* @return the total (non-negative) number of compressed bytes output so far * @return the total (non-negative) number of compressed bytes output so far
* @since 1.5 * @since 1.5
*/ */
public synchronized long getBytesWritten() { public long getBytesWritten() {
ensureOpen(); synchronized (zsRef) {
return getBytesWritten(strm); ensureOpen();
return getBytesWritten(zsRef.address());
}
} }
/** /**
* Resets deflater so that a new set of input data can be processed. * Resets deflater so that a new set of input data can be processed.
* Keeps current compression level and strategy settings. * Keeps current compression level and strategy settings.
*/ */
public synchronized void reset() { public void reset() {
ensureOpen(); synchronized (zsRef) {
reset(strm); ensureOpen();
finish = false; reset(zsRef.address());
finished = false; finish = false;
off = len = 0; finished = false;
off = len = 0;
}
} }
/** /**
...@@ -488,11 +513,14 @@ class Deflater { ...@@ -488,11 +513,14 @@ class Deflater {
* finalize() method. Once this method is called, the behavior * finalize() method. Once this method is called, the behavior
* of the Deflater object is undefined. * of the Deflater object is undefined.
*/ */
public synchronized void end() { public void end() {
if (strm != 0) { synchronized (zsRef) {
end(strm); long addr = zsRef.address();
strm = 0; zsRef.clear();
buf = null; if (addr != 0) {
end(addr);
buf = null;
}
} }
} }
...@@ -504,18 +532,19 @@ class Deflater { ...@@ -504,18 +532,19 @@ class Deflater {
} }
private void ensureOpen() { private void ensureOpen() {
if (strm == 0) assert Thread.holdsLock(zsRef);
throw new NullPointerException(); if (zsRef.address() == 0)
throw new NullPointerException("Deflater has been closed");
} }
private static native void initIDs(); private static native void initIDs();
private native static long init(int level, int strategy, boolean nowrap); private native static long init(int level, int strategy, boolean nowrap);
private native static void setDictionary(long strm, byte[] b, int off, private native static void setDictionary(long addr, byte[] b, int off, int len);
int len); private native int deflateBytes(long addr, byte[] b, int off, int len,
private native int deflateBytes(byte[] b, int off, int len, int flush); int flush);
private native static int getAdler(long strm); private native static int getAdler(long addr);
private native static long getBytesRead(long strm); private native static long getBytesRead(long addr);
private native static long getBytesWritten(long strm); private native static long getBytesWritten(long addr);
private native static void reset(long strm); private native static void reset(long addr);
private native static void end(long strm); private native static void end(long addr);
} }
/* /*
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -72,7 +72,8 @@ package java.util.zip; ...@@ -72,7 +72,8 @@ package java.util.zip;
*/ */
public public
class Inflater { class Inflater {
private long strm;
private final ZStreamRef zsRef;
private byte[] buf = defaultBuf; private byte[] buf = defaultBuf;
private int off, len; private int off, len;
private boolean finished; private boolean finished;
...@@ -97,7 +98,7 @@ class Inflater { ...@@ -97,7 +98,7 @@ class Inflater {
* @param nowrap if true then support GZIP compatible compression * @param nowrap if true then support GZIP compatible compression
*/ */
public Inflater(boolean nowrap) { public Inflater(boolean nowrap) {
strm = init(nowrap); zsRef = new ZStreamRef(init(nowrap));
} }
/** /**
...@@ -116,16 +117,18 @@ class Inflater { ...@@ -116,16 +117,18 @@ class Inflater {
* @param len the length of the input data * @param len the length of the input data
* @see Inflater#needsInput * @see Inflater#needsInput
*/ */
public synchronized void setInput(byte[] b, int off, int len) { public void setInput(byte[] b, int off, int len) {
if (b == null) { if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
this.buf = b; synchronized (zsRef) {
this.off = off; this.buf = b;
this.len = len; this.off = off;
this.len = len;
}
} }
/** /**
...@@ -150,15 +153,18 @@ class Inflater { ...@@ -150,15 +153,18 @@ class Inflater {
* @see Inflater#needsDictionary * @see Inflater#needsDictionary
* @see Inflater#getAdler * @see Inflater#getAdler
*/ */
public synchronized void setDictionary(byte[] b, int off, int len) { public void setDictionary(byte[] b, int off, int len) {
if (strm == 0 || b == null) { if (b == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
setDictionary(strm, b, off, len); synchronized (zsRef) {
needDict = false; ensureOpen();
setDictionary(zsRef.address(), b, off, len);
needDict = false;
}
} }
/** /**
...@@ -180,8 +186,10 @@ class Inflater { ...@@ -180,8 +186,10 @@ class Inflater {
* buffer after decompression has finished. * buffer after decompression has finished.
* @return the total number of bytes remaining in the input buffer * @return the total number of bytes remaining in the input buffer
*/ */
public synchronized int getRemaining() { public int getRemaining() {
return len; synchronized (zsRef) {
return len;
}
} }
/** /**
...@@ -190,8 +198,10 @@ class Inflater { ...@@ -190,8 +198,10 @@ class Inflater {
* to provide more input. * to provide more input.
* @return true if no data remains in the input buffer * @return true if no data remains in the input buffer
*/ */
public synchronized boolean needsInput() { public boolean needsInput() {
return len <= 0; synchronized (zsRef) {
return len <= 0;
}
} }
/** /**
...@@ -199,8 +209,10 @@ class Inflater { ...@@ -199,8 +209,10 @@ class Inflater {
* @return true if a preset dictionary is needed for decompression * @return true if a preset dictionary is needed for decompression
* @see Inflater#setDictionary * @see Inflater#setDictionary
*/ */
public synchronized boolean needsDictionary() { public boolean needsDictionary() {
return needDict; synchronized (zsRef) {
return needDict;
}
} }
/** /**
...@@ -209,8 +221,10 @@ class Inflater { ...@@ -209,8 +221,10 @@ class Inflater {
* @return true if the end of the compressed data stream has been * @return true if the end of the compressed data stream has been
* reached * reached
*/ */
public synchronized boolean finished() { public boolean finished() {
return finished; synchronized (zsRef) {
return finished;
}
} }
/** /**
...@@ -228,7 +242,7 @@ class Inflater { ...@@ -228,7 +242,7 @@ class Inflater {
* @see Inflater#needsInput * @see Inflater#needsInput
* @see Inflater#needsDictionary * @see Inflater#needsDictionary
*/ */
public synchronized int inflate(byte[] b, int off, int len) public int inflate(byte[] b, int off, int len)
throws DataFormatException throws DataFormatException
{ {
if (b == null) { if (b == null) {
...@@ -237,7 +251,10 @@ class Inflater { ...@@ -237,7 +251,10 @@ class Inflater {
if (off < 0 || len < 0 || off > b.length - len) { if (off < 0 || len < 0 || off > b.length - len) {
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException();
} }
return inflateBytes(b, off, len); synchronized (zsRef) {
ensureOpen();
return inflateBytes(zsRef.address(), b, off, len);
}
} }
/** /**
...@@ -261,9 +278,11 @@ class Inflater { ...@@ -261,9 +278,11 @@ class Inflater {
* Returns the ADLER-32 value of the uncompressed data. * Returns the ADLER-32 value of the uncompressed data.
* @return the ADLER-32 value of the uncompressed data * @return the ADLER-32 value of the uncompressed data
*/ */
public synchronized int getAdler() { public int getAdler() {
ensureOpen(); synchronized (zsRef) {
return getAdler(strm); ensureOpen();
return getAdler(zsRef.address());
}
} }
/** /**
...@@ -285,9 +304,11 @@ class Inflater { ...@@ -285,9 +304,11 @@ class Inflater {
* @return the total (non-negative) number of compressed bytes input so far * @return the total (non-negative) number of compressed bytes input so far
* @since 1.5 * @since 1.5
*/ */
public synchronized long getBytesRead() { public long getBytesRead() {
ensureOpen(); synchronized (zsRef) {
return getBytesRead(strm); ensureOpen();
return getBytesRead(zsRef.address());
}
} }
/** /**
...@@ -309,21 +330,25 @@ class Inflater { ...@@ -309,21 +330,25 @@ class Inflater {
* @return the total (non-negative) number of uncompressed bytes output so far * @return the total (non-negative) number of uncompressed bytes output so far
* @since 1.5 * @since 1.5
*/ */
public synchronized long getBytesWritten() { public long getBytesWritten() {
ensureOpen(); synchronized (zsRef) {
return getBytesWritten(strm); ensureOpen();
return getBytesWritten(zsRef.address());
}
} }
/** /**
* Resets inflater so that a new set of input data can be processed. * Resets inflater so that a new set of input data can be processed.
*/ */
public synchronized void reset() { public void reset() {
ensureOpen(); synchronized (zsRef) {
reset(strm); ensureOpen();
buf = defaultBuf; reset(zsRef.address());
finished = false; buf = defaultBuf;
needDict = false; finished = false;
off = len = 0; needDict = false;
off = len = 0;
}
} }
/** /**
...@@ -333,11 +358,14 @@ class Inflater { ...@@ -333,11 +358,14 @@ class Inflater {
* method. Once this method is called, the behavior of the Inflater * method. Once this method is called, the behavior of the Inflater
* object is undefined. * object is undefined.
*/ */
public synchronized void end() { public void end() {
if (strm != 0) { synchronized (zsRef) {
end(strm); long addr = zsRef.address();
strm = 0; zsRef.clear();
buf = null; if (addr != 0) {
end(addr);
buf = null;
}
} }
} }
...@@ -349,19 +377,20 @@ class Inflater { ...@@ -349,19 +377,20 @@ class Inflater {
} }
private void ensureOpen () { private void ensureOpen () {
if (strm == 0) assert Thread.holdsLock(zsRef);
throw new NullPointerException(); if (zsRef.address() == 0)
throw new NullPointerException("Inflater has been closed");
} }
private native static void initIDs(); private native static void initIDs();
private native static long init(boolean nowrap); private native static long init(boolean nowrap);
private native static void setDictionary(long strm, byte[] b, int off, private native static void setDictionary(long addr, byte[] b, int off,
int len); int len);
private native int inflateBytes(byte[] b, int off, int len) private native int inflateBytes(long addr, byte[] b, int off, int len)
throws DataFormatException; throws DataFormatException;
private native static int getAdler(long strm); private native static int getAdler(long addr);
private native static long getBytesRead(long strm); private native static long getBytesRead(long addr);
private native static long getBytesWritten(long strm); private native static long getBytesWritten(long addr);
private native static void reset(long strm); private native static void reset(long addr);
private native static void end(long strm); private native static void end(long addr);
} }
/*
* Copyright 2009 Sun Microsystems, Inc. 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. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package java.util.zip;
/**
* A reference to the native zlib's z_stream structure.
*/
class ZStreamRef {
private long address;
ZStreamRef (long address) {
this.address = address;
}
long address() {
return address;
}
void clear() {
address = 0;
}
}
/* /*
* Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -38,7 +38,6 @@ ...@@ -38,7 +38,6 @@
#define DEF_MEM_LEVEL 8 #define DEF_MEM_LEVEL 8
static jfieldID strmID;
static jfieldID levelID; static jfieldID levelID;
static jfieldID strategyID; static jfieldID strategyID;
static jfieldID setParamsID; static jfieldID setParamsID;
...@@ -49,7 +48,6 @@ static jfieldID bufID, offID, lenID; ...@@ -49,7 +48,6 @@ static jfieldID bufID, offID, lenID;
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls) Java_java_util_zip_Deflater_initIDs(JNIEnv *env, jclass cls)
{ {
strmID = (*env)->GetFieldID(env, cls, "strm", "J");
levelID = (*env)->GetFieldID(env, cls, "level", "I"); levelID = (*env)->GetFieldID(env, cls, "level", "I");
strategyID = (*env)->GetFieldID(env, cls, "strategy", "I"); strategyID = (*env)->GetFieldID(env, cls, "strategy", "I");
setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z"); setParamsID = (*env)->GetFieldID(env, cls, "setParams", "Z");
...@@ -94,7 +92,7 @@ Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level, ...@@ -94,7 +92,7 @@ Java_java_util_zip_Deflater_init(JNIEnv *env, jclass cls, jint level,
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm, Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
jarray b, jint off, jint len) jarray b, jint off, jint len)
{ {
Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
...@@ -102,7 +100,7 @@ Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm, ...@@ -102,7 +100,7 @@ Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm,
if (buf == 0) {/* out of memory */ if (buf == 0) {/* out of memory */
return; return;
} }
res = deflateSetDictionary((z_stream *)jlong_to_ptr(strm), buf + off, len); res = deflateSetDictionary((z_stream *)jlong_to_ptr(addr), buf + off, len);
(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0); (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
switch (res) { switch (res) {
case Z_OK: case Z_OK:
...@@ -111,151 +109,144 @@ Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm, ...@@ -111,151 +109,144 @@ Java_java_util_zip_Deflater_setDictionary(JNIEnv *env, jclass cls, jlong strm,
JNU_ThrowIllegalArgumentException(env, 0); JNU_ThrowIllegalArgumentException(env, 0);
break; break;
default: default:
JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(strm))->msg); JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
break; break;
} }
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
jarray b, jint off, jint len, jint flush) jarray b, jint off, jint len, jint flush)
{ {
z_stream *strm = jlong_to_ptr((*env)->GetLongField(env, this, strmID)); z_stream *strm = jlong_to_ptr(addr);
if (strm == 0) { jarray this_buf = (*env)->GetObjectField(env, this, bufID);
JNU_ThrowNullPointerException(env, 0); jint this_off = (*env)->GetIntField(env, this, offID);
return 0; jint this_len = (*env)->GetIntField(env, this, lenID);
} else { jbyte *in_buf;
jarray this_buf = (*env)->GetObjectField(env, this, bufID); jbyte *out_buf;
jint this_off = (*env)->GetIntField(env, this, offID); int res;
jint this_len = (*env)->GetIntField(env, this, lenID); if ((*env)->GetBooleanField(env, this, setParamsID)) {
jbyte *in_buf; int level = (*env)->GetIntField(env, this, levelID);
jbyte *out_buf; int strategy = (*env)->GetIntField(env, this, strategyID);
int res;
if ((*env)->GetBooleanField(env, this, setParamsID)) {
int level = (*env)->GetIntField(env, this, levelID);
int strategy = (*env)->GetIntField(env, this, strategyID);
in_buf = (jbyte *) malloc(this_len);
if (in_buf == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
(*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
out_buf = (jbyte *) malloc(len);
if (out_buf == 0) {
free(in_buf);
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
strm->next_in = (Bytef *) in_buf;
strm->next_out = (Bytef *) out_buf;
strm->avail_in = this_len;
strm->avail_out = len;
res = deflateParams(strm, level, strategy);
if (res == Z_OK) { in_buf = (jbyte *) malloc(this_len);
(*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf); if (in_buf == 0) {
} JNU_ThrowOutOfMemoryError(env, 0);
free(out_buf); return 0;
}
(*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
out_buf = (jbyte *) malloc(len);
if (out_buf == 0) {
free(in_buf); free(in_buf);
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
switch (res) { strm->next_in = (Bytef *) in_buf;
case Z_OK: strm->next_out = (Bytef *) out_buf;
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE); strm->avail_in = this_len;
this_off += this_len - strm->avail_in; strm->avail_out = len;
(*env)->SetIntField(env, this, offID, this_off); res = deflateParams(strm, level, strategy);
(*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out;
case Z_BUF_ERROR:
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
return 0;
default:
JNU_ThrowInternalError(env, strm->msg);
return 0;
}
} else {
jboolean finish = (*env)->GetBooleanField(env, this, finishID);
in_buf = (jbyte *) malloc(this_len);
if (in_buf == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
(*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
out_buf = (jbyte *) malloc(len);
if (out_buf == 0) {
free(in_buf);
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
strm->next_in = (Bytef *) in_buf; if (res == Z_OK) {
strm->next_out = (Bytef *) out_buf; (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
strm->avail_in = this_len; }
strm->avail_out = len; free(out_buf);
res = deflate(strm, finish ? Z_FINISH : flush); free(in_buf);
switch (res) {
case Z_OK:
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out;
case Z_BUF_ERROR:
(*env)->SetBooleanField(env, this, setParamsID, JNI_FALSE);
return 0;
default:
JNU_ThrowInternalError(env, strm->msg);
return 0;
}
} else {
jboolean finish = (*env)->GetBooleanField(env, this, finishID);
in_buf = (jbyte *) malloc(this_len);
if (in_buf == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
(*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
if (res == Z_STREAM_END || res == Z_OK) { out_buf = (jbyte *) malloc(len);
(*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf); if (out_buf == 0) {
}
free(out_buf);
free(in_buf); free(in_buf);
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
switch (res) { strm->next_in = (Bytef *) in_buf;
case Z_STREAM_END: strm->next_out = (Bytef *) out_buf;
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE); strm->avail_in = this_len;
/* fall through */ strm->avail_out = len;
case Z_OK: res = deflate(strm, finish ? Z_FINISH : flush);
this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off); if (res == Z_STREAM_END || res == Z_OK) {
(*env)->SetIntField(env, this, lenID, strm->avail_in); (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
return len - strm->avail_out; }
case Z_BUF_ERROR: free(out_buf);
return 0; free(in_buf);
switch (res) {
case Z_STREAM_END:
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
/* fall through */
case Z_OK:
this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out;
case Z_BUF_ERROR:
return 0;
default: default:
JNU_ThrowInternalError(env, strm->msg); JNU_ThrowInternalError(env, strm->msg);
return 0; return 0;
}
} }
} }
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Deflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->adler; return ((z_stream *)jlong_to_ptr(addr))->adler;
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_java_util_zip_Deflater_getBytesRead(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Deflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->total_in; return ((z_stream *)jlong_to_ptr(addr))->total_in;
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_java_util_zip_Deflater_getBytesWritten(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Deflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->total_out; return ((z_stream *)jlong_to_ptr(addr))->total_out;
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Deflater_reset(JNIEnv *env, jclass cls, jlong addr)
{ {
if (deflateReset((z_stream *)jlong_to_ptr(strm)) != Z_OK) { if (deflateReset((z_stream *)jlong_to_ptr(addr)) != Z_OK) {
JNU_ThrowInternalError(env, 0); JNU_ThrowInternalError(env, 0);
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Deflater_end(JNIEnv *env, jclass cls, jlong addr)
{ {
if (deflateEnd((z_stream *)jlong_to_ptr(strm)) == Z_STREAM_ERROR) { if (deflateEnd((z_stream *)jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
JNU_ThrowInternalError(env, 0); JNU_ThrowInternalError(env, 0);
} else { } else {
free((z_stream *)jlong_to_ptr(strm)); free((z_stream *)jlong_to_ptr(addr));
} }
} }
/* /*
* Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -41,7 +41,6 @@ ...@@ -41,7 +41,6 @@
#define ThrowDataFormatException(env, msg) \ #define ThrowDataFormatException(env, msg) \
JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg) JNU_ThrowByName(env, "java/util/zip/DataFormatException", msg)
static jfieldID strmID;
static jfieldID needDictID; static jfieldID needDictID;
static jfieldID finishedID; static jfieldID finishedID;
static jfieldID bufID, offID, lenID; static jfieldID bufID, offID, lenID;
...@@ -49,7 +48,6 @@ static jfieldID bufID, offID, lenID; ...@@ -49,7 +48,6 @@ static jfieldID bufID, offID, lenID;
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls) Java_java_util_zip_Inflater_initIDs(JNIEnv *env, jclass cls)
{ {
strmID = (*env)->GetFieldID(env, cls, "strm", "J");
needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z"); needDictID = (*env)->GetFieldID(env, cls, "needDict", "Z");
finishedID = (*env)->GetFieldID(env, cls, "finished", "Z"); finishedID = (*env)->GetFieldID(env, cls, "finished", "Z");
bufID = (*env)->GetFieldID(env, cls, "buf", "[B"); bufID = (*env)->GetFieldID(env, cls, "buf", "[B");
...@@ -84,134 +82,129 @@ Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap) ...@@ -84,134 +82,129 @@ Java_java_util_zip_Inflater_init(JNIEnv *env, jclass cls, jboolean nowrap)
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong strm, Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
jarray b, jint off, jint len) jarray b, jint off, jint len)
{ {
Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0); Bytef *buf = (*env)->GetPrimitiveArrayCritical(env, b, 0);
int res; int res;
if (buf == 0) /* out of memory */ if (buf == 0) /* out of memory */
return; return;
res = inflateSetDictionary(jlong_to_ptr(strm), buf + off, len); res = inflateSetDictionary(jlong_to_ptr(addr), buf + off, len);
(*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0); (*env)->ReleasePrimitiveArrayCritical(env, b, buf, 0);
switch (res) { switch (res) {
case Z_OK: case Z_OK:
break; break;
case Z_STREAM_ERROR: case Z_STREAM_ERROR:
case Z_DATA_ERROR: case Z_DATA_ERROR:
JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(strm))->msg); JNU_ThrowIllegalArgumentException(env, ((z_stream *)jlong_to_ptr(addr))->msg);
break; break;
default: default:
JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(strm))->msg); JNU_ThrowInternalError(env, ((z_stream *)jlong_to_ptr(addr))->msg);
break; break;
} }
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
jarray b, jint off, jint len) jarray b, jint off, jint len)
{ {
z_stream *strm = jlong_to_ptr((*env)->GetLongField(env, this, strmID)); z_stream *strm = jlong_to_ptr(addr);
if (strm == 0) { jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID);
JNU_ThrowNullPointerException(env, 0); jint this_off = (*env)->GetIntField(env, this, offID);
jint this_len = (*env)->GetIntField(env, this, lenID);
jbyte *in_buf;
jbyte *out_buf;
int ret;
in_buf = (jbyte *) malloc(this_len);
if (in_buf == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0; return 0;
} else { }
jarray this_buf = (jarray)(*env)->GetObjectField(env, this, bufID); (*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
jint this_off = (*env)->GetIntField(env, this, offID);
jint this_len = (*env)->GetIntField(env, this, lenID);
jbyte *in_buf;
jbyte *out_buf;
int ret;
in_buf = (jbyte *) malloc(this_len);
if (in_buf == 0) {
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}
(*env)->GetByteArrayRegion(env, this_buf, this_off, this_len, in_buf);
out_buf = (jbyte *) malloc(len); out_buf = (jbyte *) malloc(len);
if (out_buf == 0) { if (out_buf == 0) {
free(in_buf); free(in_buf);
JNU_ThrowOutOfMemoryError(env, 0); JNU_ThrowOutOfMemoryError(env, 0);
return 0; return 0;
} }
strm->next_in = (Bytef *) in_buf; strm->next_in = (Bytef *) in_buf;
strm->next_out = (Bytef *) out_buf; strm->next_out = (Bytef *) out_buf;
strm->avail_in = this_len; strm->avail_in = this_len;
strm->avail_out = len; strm->avail_out = len;
ret = inflate(strm, Z_PARTIAL_FLUSH); ret = inflate(strm, Z_PARTIAL_FLUSH);
if (ret == Z_STREAM_END || ret == Z_OK) { if (ret == Z_STREAM_END || ret == Z_OK) {
(*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf); (*env)->SetByteArrayRegion(env, b, off, len - strm->avail_out, out_buf);
} }
free(out_buf); free(out_buf);
free(in_buf); free(in_buf);
switch (ret) { switch (ret) {
case Z_STREAM_END: case Z_STREAM_END:
(*env)->SetBooleanField(env, this, finishedID, JNI_TRUE); (*env)->SetBooleanField(env, this, finishedID, JNI_TRUE);
/* fall through */ /* fall through */
case Z_OK: case Z_OK:
this_off += this_len - strm->avail_in; this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off); (*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in); (*env)->SetIntField(env, this, lenID, strm->avail_in);
return len - strm->avail_out; return len - strm->avail_out;
case Z_NEED_DICT: case Z_NEED_DICT:
(*env)->SetBooleanField(env, this, needDictID, JNI_TRUE); (*env)->SetBooleanField(env, this, needDictID, JNI_TRUE);
/* Might have consumed some input here! */ /* Might have consumed some input here! */
this_off += this_len - strm->avail_in; this_off += this_len - strm->avail_in;
(*env)->SetIntField(env, this, offID, this_off); (*env)->SetIntField(env, this, offID, this_off);
(*env)->SetIntField(env, this, lenID, strm->avail_in); (*env)->SetIntField(env, this, lenID, strm->avail_in);
return 0; return 0;
case Z_BUF_ERROR: case Z_BUF_ERROR:
return 0; return 0;
case Z_DATA_ERROR: case Z_DATA_ERROR:
ThrowDataFormatException(env, strm->msg); ThrowDataFormatException(env, strm->msg);
return 0; return 0;
case Z_MEM_ERROR: case Z_MEM_ERROR:
JNU_ThrowOutOfMemoryError(env, 0); JNU_ThrowOutOfMemoryError(env, 0);
return 0; return 0;
default: default:
JNU_ThrowInternalError(env, strm->msg); JNU_ThrowInternalError(env, strm->msg);
return 0; return 0;
}
} }
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Inflater_getAdler(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->adler; return ((z_stream *)jlong_to_ptr(addr))->adler;
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Inflater_getBytesRead(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->total_in; return ((z_stream *)jlong_to_ptr(addr))->total_in;
} }
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Inflater_getBytesWritten(JNIEnv *env, jclass cls, jlong addr)
{ {
return ((z_stream *)jlong_to_ptr(strm))->total_out; return ((z_stream *)jlong_to_ptr(addr))->total_out;
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Inflater_reset(JNIEnv *env, jclass cls, jlong addr)
{ {
if (inflateReset(jlong_to_ptr(strm)) != Z_OK) { if (inflateReset(jlong_to_ptr(addr)) != Z_OK) {
JNU_ThrowInternalError(env, 0); JNU_ThrowInternalError(env, 0);
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong strm) Java_java_util_zip_Inflater_end(JNIEnv *env, jclass cls, jlong addr)
{ {
if (inflateEnd(jlong_to_ptr(strm)) == Z_STREAM_ERROR) { if (inflateEnd(jlong_to_ptr(addr)) == Z_STREAM_ERROR) {
JNU_ThrowInternalError(env, 0); JNU_ThrowInternalError(env, 0);
} else { } else {
free(jlong_to_ptr(strm)); free(jlong_to_ptr(addr));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册