Class Zlib
java.lang.Object
org.apache.mina.filter.compression.Zlib
A helper class for interfacing with the JZlib library. This class acts both
as a compressor and decompressor, but only as one at a time. The only
flush method supported is
Z_SYNC_FLUSH also known as Z_PARTIAL_FLUSH-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault compressionstatic final intTry o get the best possible compressionstatic final intFavor speed over compression ratiostatic final intNo compressionprivate intThe requested compression level(package private) static final longGrace size before decompression ratio check is enforced.private long(package private) static final longDefault maximum decompression ratio (decompressed / compressed).(package private) static final intThe maximum size of an inflated buffer.private intprivate longprivate intThe selected operation mode : INFLATE or DEFLATEstatic final intCompression modestatic final intUncompress modeprivate com.jcraft.jzlib.ZStreamThe inner stream used to inflate or deflate the data -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidChecks the cumulative decompression ratio against the configured maximum.voidcleanUp()Cleans up the resources used by the compression library.Compress the input.Uncompress the given buffer, returning it in a new buffer.
-
Field Details
-
COMPRESSION_MAX
public static final int COMPRESSION_MAXTry o get the best possible compression- See Also:
-
COMPRESSION_MIN
public static final int COMPRESSION_MINFavor speed over compression ratio- See Also:
-
COMPRESSION_NONE
public static final int COMPRESSION_NONENo compression- See Also:
-
COMPRESSION_DEFAULT
public static final int COMPRESSION_DEFAULTDefault compression- See Also:
-
MODE_DEFLATER
public static final int MODE_DEFLATERCompression mode- See Also:
-
MODE_INFLATER
public static final int MODE_INFLATERUncompress mode- See Also:
-
compressionLevel
private int compressionLevelThe requested compression level -
MAX_DECOMPRESSED_SIZE
static final int MAX_DECOMPRESSED_SIZEThe maximum size of an inflated buffer. Default to 1Mb- See Also:
-
MAX_DECOMPRESS_RATIO
static final long MAX_DECOMPRESS_RATIODefault maximum decompression ratio (decompressed / compressed).- See Also:
-
DECOMPRESS_RATIO_MIN_SIZE
static final long DECOMPRESS_RATIO_MIN_SIZEGrace size before decompression ratio check is enforced.Below this threshold the check is skipped to avoid false positives on small payloads where framing/header overhead dominates the ratio.
- See Also:
-
maxDecompressedSize
private int maxDecompressedSize -
maxDecompressRatio
private long maxDecompressRatio -
decompressRatioMinSize
private long decompressRatioMinSize -
zStream
private com.jcraft.jzlib.ZStream zStreamThe inner stream used to inflate or deflate the data -
mode
private int modeThe selected operation mode : INFLATE or DEFLATE
-
-
Constructor Details
-
Zlib
public Zlib(int compressionLevel, int mode) Creates an instance of the ZLib class.- Parameters:
compressionLevel- the level of compression that should be used. One ofCOMPRESSION_MAX,COMPRESSION_MIN,COMPRESSION_NONEorCOMPRESSION_DEFAULTmode- the mode in which the instance will operate. Can be either ofMODE_DEFLATERorMODE_INFLATER- Throws:
IllegalArgumentException- if the mode is incorrect
-
Zlib
public Zlib(int compressionLevel, int mode, int maxDecompressedSize, long maxDecompressRatio, long decompressRatioMinSize) Creates an instance of the ZLib class.- Parameters:
compressionLevel- the level of compression that should be used. One ofCOMPRESSION_MAX,COMPRESSION_MIN,COMPRESSION_NONEorCOMPRESSION_DEFAULTmode- the mode in which the instance will operate. Can be either ofMODE_DEFLATERorMODE_INFLATERmaxDecompressedSize- the maximum inflation size for a buffermaxDecompressRatio- the maximum allowed ratio of decompressed to compressed bytes, evaluated cumulatively over the lifetime of this inflater. A value <= 0 disables the check.decompressRatioMinSize- the minimum cumulative decompressed size (in bytes) below which the ratio check is skipped.- Throws:
IllegalArgumentException- if the mode is incorrect
-
-
Method Details
-
inflate
Uncompress the given buffer, returning it in a new buffer.- Parameters:
inBuffer- theIoBufferto be decompressed. The contents of the buffer are transferred into a local byte array and the buffer is flipped and returned intact.- Returns:
- the decompressed data
- Throws:
IOException- if the decompression of the data failed for some reason.IllegalArgumentException- if the mode is notMODE_DEFLATER
-
deflate
Compress the input. The result will be put in a new buffer.- Parameters:
inBuffer- the buffer to be compressed. The contents are transferred into a local byte array and the buffer is flipped and returned intact.- Returns:
- the buffer with the compressed data
- Throws:
IOException- if the compression of teh buffer failed for some reasonIllegalStateException- if the mode is notMODE_DEFLATER
-
checkDecompressRatio
Checks the cumulative decompression ratio against the configured maximum.- Throws:
IOException- if the cumulative ratio exceedsmaxDecompressRatio
-
cleanUp
public void cleanUp()Cleans up the resources used by the compression library.
-