Skip to content

Commit

Permalink
Backport the pull request netty#174 to fix netty#163
Browse files Browse the repository at this point in the history
  • Loading branch information
trustin committed Feb 2, 2012
1 parent 9c0aa0c commit cd69bd4
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,66 @@
public class HttpContentCompressor extends HttpContentEncoder {

private final int compressionLevel;
private final int windowBits;
private final int memLevel;

/**
* Creates a new handler with the default compression level (<tt>6</tt>).
* Creates a new handler with the default compression level (<tt>6</tt>),
* default window size (<tt>15</tt>) and default memory level (<tt>8</tt>).
*/
public HttpContentCompressor() {
this(6);
}

/**
* Creates a new handler with the specified compression level.
* Creates a new handler with the specified compression level, default
* window size (<tt>15</tt>) and default memory level (<tt>8</tt>).
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
*/
public HttpContentCompressor(int compressionLevel) {
this(compressionLevel, 15, 8);
}

/**
* Creates a new handler with the specified compression level, window size,
* and memory level..
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
*/
public HttpContentCompressor(int compressionLevel, int windowBits, int memLevel) {
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel +
" (expected: 0-9)");
}
if (windowBits < 9 || windowBits > 15) {
throw new IllegalArgumentException(
"windowBits: " + windowBits + " (expected: 9-15)");
}
if (memLevel < 1 || memLevel > 9) {
throw new IllegalArgumentException(
"memLevel: " + memLevel + " (expected: 1-9)");
}
this.compressionLevel = compressionLevel;
this.windowBits = windowBits;
this.memLevel = memLevel;
}

@Override
Expand Down Expand Up @@ -83,7 +120,7 @@ protected Result beginEncode(HttpMessage msg, String acceptEncoding) throws Exce
return new Result(
targetContentEncoding,
new EncoderEmbedder<ChannelBuffer>(
new ZlibEncoder(wrapper, compressionLevel)));
new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)));
}

private ZlibWrapper determineWrapper(String acceptEncoding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public class ZlibEncoder extends OneToOneEncoder implements LifeCycleAwareChanne
private volatile ChannelHandlerContext ctx;

/**
* Creates a new zlib encoder with the default compression level ({@code 6})
* Creates a new zlib encoder with the default compression level ({@code 6}),
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the default wrapper ({@link ZlibWrapper#ZLIB}).
*
* @throws CompressionException if failed to initialize zlib
Expand All @@ -56,7 +57,8 @@ public ZlibEncoder() {
}

/**
* Creates a new zlib encoder with the specified {@code compressionLevel}
* Creates a new zlib encoder with the specified {@code compressionLevel},
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the default wrapper ({@link ZlibWrapper#ZLIB}).
*
* @param compressionLevel
Expand All @@ -71,7 +73,8 @@ public ZlibEncoder(int compressionLevel) {
}

/**
* Creates a new zlib encoder with the default compression level ({@code 6})
* Creates a new zlib encoder with the default compression level ({@code 6}),
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the specified wrapper.
*
* @throws CompressionException if failed to initialize zlib
Expand All @@ -81,8 +84,10 @@ public ZlibEncoder(ZlibWrapper wrapper) {
}

/**
* Creates a new zlib encoder with the specified {@code compressionLevel}
* Creates a new zlib encoder with the specified {@code compressionLevel},
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the specified wrapper.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
Expand All @@ -91,11 +96,46 @@ public ZlibEncoder(ZlibWrapper wrapper) {
* @throws CompressionException if failed to initialize zlib
*/
public ZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
this(wrapper, compressionLevel, 15, 8);
}

/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel}, and
* the specified wrapper.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
*
* @throws CompressionException if failed to initialize zlib
*/
public ZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {

if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel +
" (expected: 0-9)");
}
if (windowBits < 9 || windowBits > 15) {
throw new IllegalArgumentException(
"windowBits: " + windowBits + " (expected: 9-15)");
}
if (memLevel < 1 || memLevel > 9) {
throw new IllegalArgumentException(
"memLevel: " + memLevel + " (expected: 1-9)");
}
if (wrapper == null) {
throw new NullPointerException("wrapper");
}
Expand All @@ -106,15 +146,18 @@ public ZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
}

synchronized (z) {
int resultCode = z.deflateInit(compressionLevel, ZlibUtil.convertWrapperType(wrapper));
int resultCode = z.deflateInit(
compressionLevel, windowBits, memLevel,
ZlibUtil.convertWrapperType(wrapper));
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
}
}
}

/**
* Creates a new zlib encoder with the default compression level ({@code 6})
* Creates a new zlib encoder with the default compression level ({@code 6}),
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the specified preset dictionary. The wrapper is always
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
* the preset dictionary.
Expand All @@ -128,7 +171,8 @@ public ZlibEncoder(byte[] dictionary) {
}

/**
* Creates a new zlib encoder with the specified {@code compressionLevel}
* Creates a new zlib encoder with the specified {@code compressionLevel},
* default window bits ({@code 15}), default memory level ({@code 8}),
* and the specified preset dictionary. The wrapper is always
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
* the preset dictionary.
Expand All @@ -142,17 +186,55 @@ public ZlibEncoder(byte[] dictionary) {
* @throws CompressionException if failed to initialize zlib
*/
public ZlibEncoder(int compressionLevel, byte[] dictionary) {
this(compressionLevel, 15, 8, dictionary);
}

/**
* Creates a new zlib encoder with the specified {@code compressionLevel},
* the specified {@code windowBits}, the specified {@code memLevel},
* and the specified preset dictionary. The wrapper is always
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
* the preset dictionary.
*
* @param compressionLevel
* {@code 1} yields the fastest compression and {@code 9} yields the
* best compression. {@code 0} means no compression. The default
* compression level is {@code 6}.
* @param windowBits
* The base two logarithm of the size of the history buffer. The
* value should be in the range {@code 9} to {@code 15} inclusive.
* Larger values result in better compression at the expense of
* memory usage. The default value is {@code 15}.
* @param memLevel
* How much memory should be allocated for the internal compression
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
* memory. Larger values result in better and faster compression
* at the expense of memory usage. The default value is {@code 8}
* @param dictionary the preset dictionary
*
* @throws CompressionException if failed to initialize zlib
*/
public ZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)");
}

if (windowBits < 9 || windowBits > 15) {
throw new IllegalArgumentException(
"windowBits: " + windowBits + " (expected: 9-15)");
}
if (memLevel < 1 || memLevel > 9) {
throw new IllegalArgumentException(
"memLevel: " + memLevel + " (expected: 1-9)");
}
if (dictionary == null) {
throw new NullPointerException("dictionary");
}

synchronized (z) {
int resultCode;
resultCode = z.deflateInit(compressionLevel, JZlib.W_ZLIB); // Default: ZLIB format
resultCode = z.deflateInit(
compressionLevel, windowBits, memLevel,
JZlib.W_ZLIB); // Default: ZLIB format
if (resultCode != JZlib.Z_OK) {
ZlibUtil.fail(z, "initialization failure", resultCode);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1296,9 +1296,9 @@ private int longest_match(int cur_match) {
return lookahead;
}

int deflateInit(ZStream strm, int level, int bits, WrapperType wrapperType) {
int deflateInit(ZStream strm, int level, int bits, int memLevel, WrapperType wrapperType) {
return deflateInit2(strm, level, JZlib.Z_DEFLATED, bits,
JZlib.DEF_MEM_LEVEL, JZlib.Z_DEFAULT_STRATEGY, wrapperType);
memLevel, JZlib.Z_DEFAULT_STRATEGY, wrapperType);
}

private int deflateInit2(ZStream strm, int level, int method, int windowBits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@ public int deflateInit(int level, int bits) {
return deflateInit(level, bits, WrapperType.ZLIB);
}

public int deflateInit(int level, int bits, @SuppressWarnings("rawtypes") Enum wrapperType) {
public int deflateInit(int level, int bits, Enum<?> wrapperType) {
return deflateInit(level, bits, JZlib.DEF_MEM_LEVEL, wrapperType);
}

public int deflateInit(int level, int bits, int memLevel, @SuppressWarnings("rawtypes") Enum wrapperType) {
dstate = new Deflate();
return dstate.deflateInit(this, level, bits, (WrapperType) wrapperType);
return dstate.deflateInit(this, level, bits, memLevel, (WrapperType) wrapperType);
}

public int deflate(int flush) {
Expand Down

0 comments on commit cd69bd4

Please sign in to comment.