Refactored jzlib to support more wrapper types. zlib is the only wrapper at the moment though
This commit is contained in:
parent
96d3726a38
commit
1b178477d6
@ -76,7 +76,7 @@ public class ZlibEncoder extends OneToOneEncoder {
|
||||
}
|
||||
|
||||
synchronized (z) {
|
||||
int resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format
|
||||
int resultCode = z.deflateInit(compressionLevel, JZlib.W_ZLIB); // Default: ZLIB format
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
}
|
||||
@ -118,7 +118,7 @@ public class ZlibEncoder extends OneToOneEncoder {
|
||||
|
||||
synchronized (z) {
|
||||
int resultCode;
|
||||
resultCode = z.deflateInit(compressionLevel, false); // Default: ZLIB format
|
||||
resultCode = z.deflateInit(compressionLevel, JZlib.W_ZLIB); // Default: ZLIB format
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
} else {
|
||||
|
@ -48,6 +48,8 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package org.jboss.netty.util.internal.jzlib;
|
||||
|
||||
import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
|
||||
|
||||
final class Deflate {
|
||||
|
||||
private static final class Config {
|
||||
@ -133,7 +135,8 @@ final class Deflate {
|
||||
int pending_buf_size; // size of pending_buf
|
||||
int pending_out; // next pending byte to output to the stream
|
||||
int pending; // nb of bytes in the pending buffer
|
||||
int noheader; // suppress zlib header and adler32
|
||||
WrapperType wrapperType;
|
||||
private boolean wroteTrailer;
|
||||
byte data_type; // UNKNOWN, BINARY or ASCII
|
||||
byte method; // STORED (for zip only) or DEFLATED
|
||||
int last_flush; // value of flush param for previous deflate call
|
||||
@ -1296,18 +1299,13 @@ final class Deflate {
|
||||
return lookahead;
|
||||
}
|
||||
|
||||
int deflateInit(ZStream strm, int level, int bits) {
|
||||
int deflateInit(ZStream strm, int level, int bits, WrapperType wrapperType) {
|
||||
return deflateInit2(strm, level, JZlib.Z_DEFLATED, bits,
|
||||
JZlib.DEF_MEM_LEVEL, JZlib.Z_DEFAULT_STRATEGY);
|
||||
}
|
||||
|
||||
int deflateInit(ZStream strm, int level) {
|
||||
return deflateInit(strm, level, JZlib.MAX_WBITS);
|
||||
JZlib.DEF_MEM_LEVEL, JZlib.Z_DEFAULT_STRATEGY, wrapperType);
|
||||
}
|
||||
|
||||
private int deflateInit2(ZStream strm, int level, int method, int windowBits,
|
||||
int memLevel, int strategy) {
|
||||
int noheader = 0;
|
||||
int memLevel, int strategy, WrapperType wrapperType) {
|
||||
// byte[] my_version=ZLIB_VERSION;
|
||||
|
||||
//
|
||||
@ -1323,8 +1321,7 @@ final class Deflate {
|
||||
}
|
||||
|
||||
if (windowBits < 0) { // undocumented feature: suppress zlib header
|
||||
noheader = 1;
|
||||
windowBits = -windowBits;
|
||||
throw new IllegalArgumentException("windowBits: " + windowBits);
|
||||
}
|
||||
|
||||
if (memLevel < 1 || memLevel > JZlib.MAX_MEM_LEVEL ||
|
||||
@ -1336,7 +1333,7 @@ final class Deflate {
|
||||
|
||||
strm.dstate = this;
|
||||
|
||||
this.noheader = noheader;
|
||||
this.wrapperType = wrapperType;
|
||||
w_bits = windowBits;
|
||||
w_size = 1 << w_bits;
|
||||
w_mask = w_size - 1;
|
||||
@ -1378,10 +1375,8 @@ final class Deflate {
|
||||
pending = 0;
|
||||
pending_out = 0;
|
||||
|
||||
if (noheader < 0) {
|
||||
noheader = 0; // was set to -1 by deflate(..., Z_FINISH);
|
||||
}
|
||||
status = noheader != 0? BUSY_STATE : INIT_STATE;
|
||||
wroteTrailer = false;
|
||||
status = wrapperType == WrapperType.NONE? BUSY_STATE : INIT_STATE;
|
||||
strm.adler = Adler32.adler32(0, null, 0, 0);
|
||||
|
||||
last_flush = JZlib.Z_NO_FLUSH;
|
||||
@ -1605,7 +1600,8 @@ final class Deflate {
|
||||
if (flush != JZlib.Z_FINISH) {
|
||||
return JZlib.Z_OK;
|
||||
}
|
||||
if (noheader != 0) {
|
||||
|
||||
if (wrapperType == WrapperType.NONE || wroteTrailer) {
|
||||
return JZlib.Z_STREAM_END;
|
||||
}
|
||||
|
||||
@ -1616,7 +1612,7 @@ final class Deflate {
|
||||
|
||||
// If avail_out is zero, the application will call deflate again
|
||||
// to flush the rest.
|
||||
noheader = -1; // write the trailer only once!
|
||||
wroteTrailer = true; // write the trailer only once!
|
||||
return pending != 0? JZlib.Z_OK : JZlib.Z_STREAM_END;
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package org.jboss.netty.util.internal.jzlib;
|
||||
|
||||
import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
|
||||
|
||||
final class Inflate {
|
||||
|
||||
private static final int METHOD = 0; // waiting for method byte
|
||||
@ -74,7 +76,7 @@ final class Inflate {
|
||||
// if BAD, inflateSync's marker bytes count
|
||||
private int marker;
|
||||
// mode independent information
|
||||
private int nowrap; // flag for no wrapper
|
||||
private WrapperType wrapperType;
|
||||
private int wbits; // log2(window size) (8..15, defaults to 15)
|
||||
private InfBlocks blocks; // current inflate_blocks state
|
||||
|
||||
@ -85,7 +87,7 @@ final class Inflate {
|
||||
|
||||
z.total_in = z.total_out = 0;
|
||||
z.msg = null;
|
||||
z.istate.mode = z.istate.nowrap != 0? BLOCKS : METHOD;
|
||||
z.istate.mode = z.istate.wrapperType == WrapperType.NONE? BLOCKS : METHOD;
|
||||
z.istate.blocks.reset(z, null);
|
||||
return JZlib.Z_OK;
|
||||
}
|
||||
@ -99,15 +101,14 @@ final class Inflate {
|
||||
return JZlib.Z_OK;
|
||||
}
|
||||
|
||||
int inflateInit(ZStream z, int w) {
|
||||
int inflateInit(ZStream z, int w, WrapperType wrapperType) {
|
||||
z.msg = null;
|
||||
blocks = null;
|
||||
|
||||
// handle undocumented nowrap option (no zlib header or check)
|
||||
nowrap = 0;
|
||||
this.wrapperType = wrapperType;
|
||||
|
||||
if (w < 0) {
|
||||
w = -w;
|
||||
nowrap = 1;
|
||||
throw new IllegalArgumentException("w: " + w);
|
||||
}
|
||||
|
||||
// set window size
|
||||
@ -117,7 +118,8 @@ final class Inflate {
|
||||
}
|
||||
wbits = w;
|
||||
|
||||
z.istate.blocks = new InfBlocks(z, z.istate.nowrap != 0? null : this,
|
||||
z.istate.blocks = new InfBlocks(
|
||||
z, z.istate.wrapperType == WrapperType.NONE? null : this,
|
||||
1 << w);
|
||||
|
||||
// reset state
|
||||
@ -249,7 +251,7 @@ final class Inflate {
|
||||
}
|
||||
r = f;
|
||||
z.istate.blocks.reset(z, z.istate.was);
|
||||
if (z.istate.nowrap != 0) {
|
||||
if (z.istate.wrapperType == WrapperType.NONE) {
|
||||
z.istate.mode = DONE;
|
||||
break;
|
||||
}
|
||||
|
@ -50,10 +50,10 @@ package org.jboss.netty.util.internal.jzlib;
|
||||
|
||||
public final class JZlib {
|
||||
|
||||
// Wrappers
|
||||
public static final int W_NONE = 0;
|
||||
public static final int W_ZLIB = 1;
|
||||
public static final int W_GZIP = 2;
|
||||
// wrapper types
|
||||
public static final Enum<?> W_NONE = WrapperType.NONE;
|
||||
public static final Enum<?> W_ZLIB = WrapperType.ZLIB;
|
||||
public static final Enum<?> W_GZIP = WrapperType.GZIP;
|
||||
|
||||
// compression levels
|
||||
public static final int Z_NO_COMPRESSION = 0;
|
||||
@ -102,4 +102,7 @@ public final class JZlib {
|
||||
// Bit length codes must not exceed MAX_BL_BITS bits
|
||||
static final int MAX_BL_BITS = 7;
|
||||
|
||||
static enum WrapperType {
|
||||
NONE, ZLIB, GZIP;
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package org.jboss.netty.util.internal.jzlib;
|
||||
|
||||
import org.jboss.netty.util.internal.jzlib.JZlib.WrapperType;
|
||||
|
||||
public final class ZStream {
|
||||
|
||||
public byte[] next_in; // next input byte
|
||||
@ -68,17 +70,17 @@ public final class ZStream {
|
||||
return inflateInit(JZlib.DEF_WBITS);
|
||||
}
|
||||
|
||||
public int inflateInit(boolean nowrap) {
|
||||
return inflateInit(JZlib.DEF_WBITS, nowrap);
|
||||
public int inflateInit(Enum<?> wrapperType) {
|
||||
return inflateInit(JZlib.DEF_WBITS, wrapperType);
|
||||
}
|
||||
|
||||
public int inflateInit(int w) {
|
||||
return inflateInit(w, false);
|
||||
return inflateInit(w, WrapperType.ZLIB);
|
||||
}
|
||||
|
||||
public int inflateInit(int w, boolean nowrap) {
|
||||
public int inflateInit(int w, Enum<?> wrapperType) {
|
||||
istate = new Inflate();
|
||||
return istate.inflateInit(this, nowrap? -w : w);
|
||||
return istate.inflateInit(this, w, (WrapperType) wrapperType);
|
||||
}
|
||||
|
||||
public int inflate(int f) {
|
||||
@ -115,17 +117,17 @@ public final class ZStream {
|
||||
return deflateInit(level, JZlib.MAX_WBITS);
|
||||
}
|
||||
|
||||
public int deflateInit(int level, boolean nowrap) {
|
||||
return deflateInit(level, JZlib.MAX_WBITS, nowrap);
|
||||
public int deflateInit(int level, Enum<?> wrapperType) {
|
||||
return deflateInit(level, JZlib.MAX_WBITS, wrapperType);
|
||||
}
|
||||
|
||||
public int deflateInit(int level, int bits) {
|
||||
return deflateInit(level, bits, false);
|
||||
return deflateInit(level, bits, WrapperType.ZLIB);
|
||||
}
|
||||
|
||||
public int deflateInit(int level, int bits, boolean nowrap) {
|
||||
public int deflateInit(int level, int bits, Enum<?> wrapperType) {
|
||||
dstate = new Deflate();
|
||||
return dstate.deflateInit(this, level, nowrap? -bits : bits);
|
||||
return dstate.deflateInit(this, level, bits, (WrapperType) wrapperType);
|
||||
}
|
||||
|
||||
public int deflate(int flush) {
|
||||
@ -212,7 +214,7 @@ public final class ZStream {
|
||||
|
||||
avail_in -= len;
|
||||
|
||||
if (dstate.noheader == 0) {
|
||||
if (dstate.wrapperType == WrapperType.ZLIB) {
|
||||
adler = Adler32.adler32(adler, next_in, next_in_index, len);
|
||||
}
|
||||
System.arraycopy(next_in, next_in_index, buf, start, len);
|
||||
|
Loading…
Reference in New Issue
Block a user