Skip to content

Finer configuration of GZIP Compression #2143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class GZipEncoder extends AbstractTransformer<Buffer, Buffer> {
private static final int TRAILER_SIZE = 8;

private final int bufferSize;
private final int compressionLevel;
private final int compressionStrategy;

private static final Buffer header;

Expand All @@ -67,7 +69,13 @@ public GZipEncoder() {
}

public GZipEncoder(int bufferSize) {
this(bufferSize, Deflater.DEFAULT_COMPRESSION, Deflater.DEFAULT_STRATEGY);
}

public GZipEncoder(int bufferSize, int compressionLevel, int compressionStrategy) {
this.bufferSize = bufferSize;
this.compressionLevel = compressionLevel;
this.compressionStrategy = compressionStrategy;
}

/**
Expand Down Expand Up @@ -104,7 +112,7 @@ protected TransformationResult<Buffer, Buffer> transformImpl(AttributeStorage st
final GZipOutputState state = (GZipOutputState) obtainStateObject(storage);

if (!state.isInitialized) {
state.initialize();
state.initialize(compressionLevel, compressionStrategy);
}

Buffer encodedBuffer = null;
Expand Down Expand Up @@ -282,8 +290,9 @@ protected static final class GZipOutputState extends LastResultAwareState<Buffer
*/
private Deflater deflater;

private void initialize() {
final Deflater newDeflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
private void initialize(int compressionLevel, int compressionStrategy) {
final Deflater newDeflater = new Deflater(compressionLevel, true);
newDeflater.setStrategy(compressionStrategy);
final CRC32 newCrc32 = new CRC32();
newCrc32.reset();
deflater = newDeflater;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.glassfish.grizzly.compression.zip.GZipEncoder;
import org.glassfish.grizzly.memory.Buffers;

import java.util.zip.Deflater;

/**
* GZip {@link ContentEncoding} implementation, which compresses/decompresses HTTP content using gzip algorithm.
*
Expand Down Expand Up @@ -50,7 +52,7 @@ public GZipContentEncoding() {

/**
* Construct <tt>GZipContentEncoding</tt> using specific buffer sizes.
*
*
* @param inBufferSize input buffer size
* @param outBufferSize output buffer size
*/
Expand All @@ -59,17 +61,32 @@ public GZipContentEncoding(int inBufferSize, int outBufferSize) {
}

/**
* Construct <tt>GZipContentEncoding</tt> using specific buffer sizes.
*
* Construct <tt>GZipContentEncoding</tt> using specific buffer sizes, with default compression level and strategy.
* @param inBufferSize input buffer size
* @param outBufferSize output buffer size
* @param encoderFilter {@link EncodingFilter}, which will decide if <tt>GZipContentEncoding</tt> should be applied to
* encode specific {@link HttpHeader} packet.
* @param encoderFilter {@link EncodingFilter}, which will decide if
* <tt>GZipContentEncoding</tt> should be applied to encode specific
* {@link HttpHeader} packet.
*/
public GZipContentEncoding(int inBufferSize, int outBufferSize, EncodingFilter encoderFilter) {
this.decoder = new GZipDecoder(inBufferSize);
this.encoder = new GZipEncoder(outBufferSize);
this(inBufferSize, outBufferSize, Deflater.DEFAULT_COMPRESSION, Deflater.DEFAULT_STRATEGY, encoderFilter);
}

/**
* Construct <tt>GZipContentEncoding</tt> using specific buffer sizes, compression level and strategy.
* @param inBufferSize input buffer size
* @param outBufferSize output buffer size
* @param compressionLevel the compression level used by the GZipEncoder
* @param compressionStrategy the compression strategy used by the GZipEncoder
* @param encoderFilter {@link EncodingFilter}, which will decide if
* <tt>GZipContentEncoding</tt> should be applied to encode specific
* {@link HttpHeader} packet.
*/
public GZipContentEncoding(int inBufferSize, int outBufferSize, int compressionLevel, int compressionStrategy,
EncodingFilter encoderFilter) {

this.decoder = new GZipDecoder(inBufferSize);
this.encoder = new GZipEncoder(outBufferSize, compressionLevel, compressionStrategy);
if (encoderFilter != null) {
this.encoderFilter = encoderFilter;
} else {
Expand Down