Skip to content

Commit

Permalink
Merge pull request #2143 from JamesHillyard/Finer_GZip_Configuration
Browse files Browse the repository at this point in the history
Finer configuration of GZIP Compression
  • Loading branch information
MattGill98 authored Jul 5, 2022
2 parents 1a01b8c + 90634d9 commit bdbe8cb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
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

0 comments on commit bdbe8cb

Please sign in to comment.