Unless otherwise noted, passing a null argument to a method
in this class will cause a NullPointerException to be
thrown.
This class deflates sequences of bytes into ZLIB compressed data format.
The input byte sequence is provided in either a byte array or a ByteBuffer,
via one of the setInput() methods. The output byte sequence is
written to the output byte array or ByteBuffer passed to the
deflate() methods.
To release the resources used by a Deflater, an application must close it
by invoking its end() or close() method.
API Note
This class implements AutoCloseable to facilitate its usage with
try-with-resources statement. The close() method simply
calls end().
The following code fragment demonstrates a trivial compression
and decompression of a string using Deflater and Inflater.
// Encode a String into bytes String inputString = "blahblahblah\u20AC\u20AC"; byte[] input = inputString.getBytes(StandardCharsets.UTF_8); // Compress the bytes ByteArrayOutputStream compressedBaos = new ByteArrayOutputStream(); try (Deflater compressor = new Deflater()) { compressor.setInput(input); // Let the compressor know that the complete input // has been made available compressor.finish(); // Keep compressing the input till the compressor // is finished compressing while (!compressor.finished()) { // Use some reasonable size for the temporary buffer // based on the data being compressed byte[] tmpBuffer = new byte[100]; int numCompressed = compressor.deflate(tmpBuffer); // Copy over the compressed bytes from the temporary // buffer into the final byte array compressedBaos.write(tmpBuffer, 0, numCompressed); } } // Decompress the bytes ByteArrayOutputStream decompressedBaos = new ByteArrayOutputStream(); try (Inflater decompressor = new Inflater()) { byte[] compressed = compressedBaos.toByteArray(); decompressor.setInput(compressed, 0, compressed.length); while (!decompressor.finished()) { // Use some reasonable size for the temporary buffer, // based on the data being decompressed; in this example, // we use a small buffer size byte[] tmpBuffer = new byte[100]; int numDecompressed = 0; try { numDecompressed = decompressor.inflate(tmpBuffer); } catch (DataFormatException dfe) { // Handle the exception suitably, in this example // we just rethrow it throw new RuntimeException(dfe); } // Copy over the decompressed bytes from the temporary // buffer into the final byte array decompressedBaos.write(tmpBuffer, 0, numDecompressed); } } // Decode the bytes into a String String outputString = decompressedBaos.toString(StandardCharsets.UTF_8);
Inflater
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | Deflater.
A reference to the native zlib's z_stream structure. |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | BEST_COMPRESSION
Compression level for best compression. |
| public static final int | BEST_SPEED
Compression level for fastest compression. |
| private long | |
| private long | |
| public static final int | DEFAULT_COMPRESSION
Default compression level. |
| public static final int | DEFAULT_STRATEGY
Default compression strategy. |
| public static final int | DEFLATED
Compression method for the deflate algorithm (the only one currently supported). |
| public static final int | FILTERED
Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. |
| private boolean | |
| private static final int | FINISH
Flush mode to use at the end of output. |
| private boolean | |
| public static final int | FULL_FLUSH
Compression flush mode used to flush out all pending output and reset the deflater. |
| public static final int | HUFFMAN_ONLY
Compression strategy for Huffman coding only. |
| private ByteBuffer | |
| private byte[] | |
| private int | |
| private int | |
| private int | |
| public static final int | NO_COMPRESSION
Compression level for no compression. |
| public static final int | NO_FLUSH
Compression flush mode used to achieve best compression result. |
| private boolean | |
| private int | |
| public static final int | SYNC_FLUSH
Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms. |
| private final Deflater. |
| Access | Constructor and Description |
|---|---|
| public | Deflater(int
the compression level (0-9) level, boolean if true then use GZIP compatible compression nowrap)Creates a new compressor using the specified compression level. |
| public | Deflater(int
the compression level (0-9) level)Creates a new compressor using the specified compression level. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements java. Closes and releases the resources held by this |
| public int | Returns: the actual number of bytes of compressed data written to the output bufferthe buffer for the compressed data output, int the start offset of the data off, int the maximum number of bytes of compressed data len)Compresses the input data and fills specified buffer with compressed data. |
| public int | Returns: the actual number of bytes of compressed data written to the output bufferthe buffer for the compressed data output)Compresses the input data and fills specified buffer with compressed data. |
| public int | Returns: the actual number of bytes of compressed data written to the output bufferthe buffer for the compressed data output)Compresses the input data and fills specified buffer with compressed data. |
| public int | Returns: the actual number of bytes of compressed data written to the output bufferthe buffer for the compressed data output, int the start offset of the data off, int the maximum number of bytes of compressed data len, int the compression flush mode flush)Compresses the input data and fills the specified buffer with compressed data. |
| public int | Returns: the actual number of bytes of compressed data written to the output bufferthe buffer for the compressed data output, int the compression flush mode flush)Compresses the input data and fills the specified buffer with compressed data. |
| private native long | deflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen, int flush, int params)
|
| private native long | deflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)
|
| private native long | deflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen, int flush, int params)
|
| private native long | deflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params)
|
| public void | |
| private static native void | |
| private void | |
| public void | finish()
When called, indicates that compression should end with the current contents of the input buffer. |
| public boolean | Returns: true if the end of the compressed data output stream has been reachedReturns true if the end of the compressed data output stream has been reached. |
| public int | Returns: the ADLER-32 value of the uncompressed dataReturns the ADLER-32 value of the uncompressed data. |
| private static native int | |
| public long | Returns: the total (non-negative) number of uncompressed bytes input so farReturns the total number of uncompressed bytes input so far. |
| public long | Returns: the total (non-negative) number of compressed bytes output so farReturns the total number of compressed bytes output so far. |
| public int | Returns: the total number of uncompressed bytes input so far
Deprecated
since 23. Use
getBytesRead() instead
Returns the total number of uncompressed bytes input so far. |
| public int | Returns: the total number of compressed bytes output so far
Deprecated
since 23. Use
getBytesWritten() instead
Returns the total number of compressed bytes output so far. |
| private static native long | |
| public boolean | Returns: true if the input data buffer is empty and setInput() should be called in order to provide more inputReturns true if no data remains in the input buffer. |
| public void | |
| private static native void | |
| public void | setDictionary(byte[]
the dictionary data bytes dictionary, int the start offset of the data off, int the length of the data len)Sets preset dictionary for compression. |
| public void | |
| public void | setDictionary(ByteBuffer
the dictionary data bytes dictionary)Sets preset dictionary for compression. |
| private static native void | |
| private static native void | |
| public void | setInput(byte[]
the input data bytes input, int the start offset of the data off, int the length of the data len)Sets input data for compression. |
| public void | |
| public void | |
| public void | setLevel(int
the new compression level (0-9) level)Sets the compression level to the specified value. |
| public void | setStrategy(int
the new compression strategy strategy)Sets the compression strategy to the specified value. |
| pack-priv boolean |
| BEST_COMPRESSION | back to summary |
|---|---|
| public static final int BEST_COMPRESSION Compression level for best compression. | |
| BEST_SPEED | back to summary |
|---|---|
| public static final int BEST_SPEED Compression level for fastest compression. | |
| bytesRead | back to summary |
|---|---|
| private long bytesRead | |
| bytesWritten | back to summary |
|---|---|
| private long bytesWritten | |
| DEFAULT_COMPRESSION | back to summary |
|---|---|
| public static final int DEFAULT_COMPRESSION Default compression level. | |
| DEFAULT_STRATEGY | back to summary |
|---|---|
| public static final int DEFAULT_STRATEGY Default compression strategy. | |
| DEFLATED | back to summary |
|---|---|
| public static final int DEFLATED Compression method for the deflate algorithm (the only one currently supported). | |
| FILTERED | back to summary |
|---|---|
| public static final int FILTERED Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching. | |
| finish | back to summary |
|---|---|
| private boolean finish | |
| FINISH | back to summary |
|---|---|
| private static final int FINISH Flush mode to use at the end of output. Can only be provided by the
user by way of | |
| finished | back to summary |
|---|---|
| private boolean finished | |
| FULL_FLUSH | back to summary |
|---|---|
| public static final int FULL_FLUSH Compression flush mode used to flush out all pending output and reset the deflater. Using this mode too often can seriously degrade compression.
| |
| HUFFMAN_ONLY | back to summary |
|---|---|
| public static final int HUFFMAN_ONLY Compression strategy for Huffman coding only. | |
| input | back to summary |
|---|---|
| private ByteBuffer input | |
| inputArray | back to summary |
|---|---|
| private byte[] inputArray | |
| inputLim | back to summary |
|---|---|
| private int inputLim | |
| inputPos | back to summary |
|---|---|
| private int inputPos | |
| level | back to summary |
|---|---|
| private int level | |
| NO_COMPRESSION | back to summary |
|---|---|
| public static final int NO_COMPRESSION Compression level for no compression. | |
| NO_FLUSH | back to summary |
|---|---|
| public static final int NO_FLUSH Compression flush mode used to achieve best compression result.
| |
| setParams | back to summary |
|---|---|
| private boolean setParams | |
| strategy | back to summary |
|---|---|
| private int strategy | |
| SYNC_FLUSH | back to summary |
|---|---|
| public static final int SYNC_FLUSH Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.
| |
| zsRef | back to summary |
|---|---|
| private final Deflater. | |
| Deflater | back to summary |
|---|---|
| public Deflater(int level, boolean nowrap) Creates a new compressor using the specified compression level. If 'nowrap' is true then the ZLIB header and checksum fields will not be used in order to support the compression format used in both GZIP and PKZIP.
| |
| Deflater | back to summary |
|---|---|
| public Deflater(int level) Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.
| |
| Deflater | back to summary |
|---|---|
| public Deflater() Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format. | |
| close | back to summary |
|---|---|
| public void close() Implements java. Closes and releases the resources held by this Implementation Specification This method calls the
| |
| deflate | back to summary |
|---|---|
| public int deflate(byte[] output, int off, int len) Compresses the input data and fills specified buffer with compressed
data. Returns actual number of bytes of compressed data. A return value
of 0 indicates that This method uses
| |
| deflate | back to summary |
|---|---|
| public int deflate(byte[] output) Compresses the input data and fills specified buffer with compressed
data. Returns actual number of bytes of compressed data. A return value
of 0 indicates that This method uses
| |
| deflate | back to summary |
|---|---|
| public int deflate(ByteBuffer output) Compresses the input data and fills specified buffer with compressed
data. Returns actual number of bytes of compressed data. A return value
of 0 indicates that This method uses
| |
| deflate | back to summary |
|---|---|
| public int deflate(byte[] output, int off, int len, int flush) Compresses the input data and fills the specified buffer with compressed data. Returns actual number of bytes of data compressed. Compression flush mode is one of the following three modes:
In the case of If the
| |
| deflate | back to summary |
|---|---|
| public int deflate(ByteBuffer output, int flush) Compresses the input data and fills the specified buffer with compressed data. Returns actual number of bytes of data compressed. Compression flush mode is one of the following three modes:
In the case of On success, the position of the given If the
| |
| deflateBufferBuffer | back to summary |
|---|---|
| private native long deflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen, int flush, int params) | |
| deflateBufferBytes | back to summary |
|---|---|
| private native long deflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params) | |
| deflateBytesBuffer | back to summary |
|---|---|
| private native long deflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen, int flush, int params) | |
| deflateBytesBytes | back to summary |
|---|---|
| private native long deflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen, int flush, int params) | |
| end | back to summary |
|---|---|
| public void end() Closes and releases the resources held by this
If the Implementation Specification Subclasses should override this method to clean up the resources acquired by the subclass.
| |
| end | back to summary |
|---|---|
| private static native void end(long addr) | |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() | |
| finish | back to summary |
|---|---|
| public void finish() When called, indicates that compression should end with the current contents of the input buffer. | |
| finished | back to summary |
|---|---|
| public boolean finished() Returns true if the end of the compressed data output stream has been reached.
| |
| getAdler | back to summary |
|---|---|
| public int getAdler() Returns the ADLER-32 value of the uncompressed data.
| |
| getAdler | back to summary |
|---|---|
| private static native int getAdler(long addr) | |
| getBytesRead | back to summary |
|---|---|
| public long getBytesRead() Returns the total number of uncompressed bytes input so far.
| |
| getBytesWritten | back to summary |
|---|---|
| public long getBytesWritten() Returns the total number of compressed bytes output so far.
| |
| getTotalIn | back to summary |
|---|---|
| public int getTotalIn()
Returns the total number of uncompressed bytes input so far. Implementation Specification This method returns the equivalent of
| |
| getTotalOut | back to summary |
|---|---|
| public int getTotalOut()
Returns the total number of compressed bytes output so far. Implementation Specification This method returns the equivalent of
| |
| init | back to summary |
|---|---|
| private static native long init(int level, int strategy, boolean nowrap) | |
| needsInput | back to summary |
|---|---|
| public boolean needsInput() Returns true if no data remains in the input buffer. This can
be used to determine if one of the
| |
| reset | back to summary |
|---|---|
| public void reset() Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.
| |
| reset | back to summary |
|---|---|
| private static native void reset(long addr) | |
| setDictionary | back to summary |
|---|---|
| public void setDictionary(byte[] dictionary, int off, int len) Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.
| |
| setDictionary | back to summary |
|---|---|
| public void setDictionary(byte[] dictionary) Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.
| |
| setDictionary | back to summary |
|---|---|
| public void setDictionary(ByteBuffer dictionary) Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression. The bytes in given byte buffer will be fully consumed by this method. On return, its position will equal its limit.
| |
| setDictionary | back to summary |
|---|---|
| private static native void setDictionary(long addr, byte[] b, int off, int len) | |
| setDictionaryBuffer | back to summary |
|---|---|
| private static native void setDictionaryBuffer(long addr, long bufAddress, int len) | |
| setInput | back to summary |
|---|---|
| public void setInput(byte[] input, int off, int len) Sets input data for compression.
One of the
| |
| setInput | back to summary |
|---|---|
| public void setInput(byte[] input) Sets input data for compression.
One of the
| |
| setInput | back to summary |
|---|---|
| public void setInput(ByteBuffer input) Sets input data for compression.
One of the The given buffer's position will be advanced as deflate operations are performed, up to the buffer's limit. The input buffer may be modified (refilled) between deflate operations; doing so is equivalent to creating a new buffer and setting it with this method. Modifying the input buffer's contents, position, or limit concurrently with an deflate operation will result in undefined behavior, which may include incorrect operation results or operation failure.
| |
| setLevel | back to summary |
|---|---|
| public void setLevel(int level) Sets the compression level to the specified value. If the compression level is changed, the next invocation
of
| |
| setStrategy | back to summary |
|---|---|
| public void setStrategy(int strategy) Sets the compression strategy to the specified value. If the compression strategy is changed, the next invocation
of
| |
| shouldFinish | back to summary |
|---|---|
| pack-priv boolean shouldFinish() Returns the value of 'finish' flag. 'finish' will be set to true if def.finish() method is called. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv long | |
| pack-priv void | |
| public synchronized void |
| address | back to summary |
|---|---|
| private long address | |
| cleanable | back to summary |
|---|---|
| private final Cleaner. | |
| DeflaterZStreamRef | back to summary |
|---|---|
| private DeflaterZStreamRef(Deflater owner, long addr) | |
| address | back to summary |
|---|---|
| pack-priv long address() | |
| clean | back to summary |
|---|---|
| pack-priv void clean() | |
| run | back to summary |
|---|---|
| public synchronized void run() Implements java. Doc from java. Runs this operation. | |