Skip to content

Commit

Permalink
Merge pull request eclipse-ee4j#31 from JamesHillyard/FISH-376
Browse files Browse the repository at this point in the history
FISH-376 Allow finer configuration details of HTTP GZIP compression
  • Loading branch information
JamesHillyard committed Oct 28, 2021
1 parent 8bf950d commit 96e09e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 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 static int compressionLevel;
private static 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 @@ -283,7 +291,8 @@ protected static final class GZipOutputState extends LastResultAwareState<Buffer
private Deflater deflater;

private void initialize() {
final Deflater newDeflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
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 96e09e6

Please sign in to comment.