Unless otherwise noted, passing a null argument to a constructor
or method in this class will cause a NullPointerException to be
thrown.
This class inflates sequences of ZLIB compressed bytes. 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 inflate() methods.
To release the resources used by an Inflater, 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);
Deflater
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | Inflater.
A reference to the native zlib's z_stream structure. |
| Modifier and Type | Field and Description |
|---|---|
| private long | |
| private long | |
| private boolean | |
| private ByteBuffer | |
| private byte[] | |
| private int | |
| private int | |
| private int | |
| private boolean | |
| private int | |
| private boolean | |
| private final Inflater. |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements java. Closes and releases the resources held by this |
| public void | |
| private static native void | |
| private void | |
| public boolean | Returns: true if the end of the compressed data stream has been reachedReturns true if the end of the compressed data 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 compressed bytes input so farReturns the total number of compressed bytes input so far. |
| public long | Returns: the total (non-negative) number of uncompressed bytes output so farReturns the total number of uncompressed bytes output so far. |
| public int | Returns: the total number of bytes remaining in the input bufferReturns the total number of bytes remaining in the input buffer. |
| public int | Returns: the total number of compressed bytes input so far
Deprecated
since 23. Use
getBytesRead() instead
Returns the total number of compressed bytes input so far. |
| public int | Returns: the total number of uncompressed bytes output so far
Deprecated
since 23. Use
getBytesWritten() instead
Returns the total number of uncompressed bytes output so far. |
| pack-priv boolean | |
| public int | Returns: the actual number of uncompressed bytesthe buffer for the uncompressed data output, int the start offset of the data off, int the maximum number of uncompressed bytes len)Uncompresses bytes into specified buffer. |
| public int | Returns: the actual number of uncompressed bytesthe buffer for the uncompressed data output)Uncompresses bytes into specified buffer. |
| public int | Returns: the actual number of uncompressed bytesthe buffer for the uncompressed data output)Uncompresses bytes into specified buffer. |
| private native long | |
| private native long | inflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen)
|
| private native long | inflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen)
|
| private native long | inflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen)
|
| private static native long | |
| private static native void | |
| public boolean | Returns: true if a preset dictionary is needed for decompressionReturns true if a preset dictionary is needed for decompression. |
| public boolean | Returns: true if no data remains in the input bufferReturns 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 the preset dictionary to the given array of bytes. |
| public void | setDictionary(byte[]
the dictionary data bytes dictionary)Sets the preset dictionary to the given array of bytes. |
| public void | setDictionary(ByteBuffer
the dictionary data bytes dictionary)Sets the preset dictionary to the bytes in the given buffer. |
| private static native void | |
| private static native void | |
| public void | setInput(byte[]
the input data bytes input, int the start offset of the input data off, int the length of the input data len)Sets input data for decompression. |
| public void | |
| public void |
| bytesRead | back to summary |
|---|---|
| private long bytesRead | |
| bytesWritten | back to summary |
|---|---|
| private long bytesWritten | |
| finished | back to summary |
|---|---|
| private boolean finished | |
| input | back to summary |
|---|---|
| private ByteBuffer input | |
| inputArray | back to summary |
|---|---|
| private byte[] inputArray | |
| inputConsumed | back to summary |
|---|---|
| private int inputConsumed | |
| inputLim | back to summary |
|---|---|
| private int inputLim | |
| inputPos | back to summary |
|---|---|
| private int inputPos | |
| needDict | back to summary |
|---|---|
| private boolean needDict | |
| outputConsumed | back to summary |
|---|---|
| private int outputConsumed | |
| pendingOutput | back to summary |
|---|---|
| private boolean pendingOutput | |
| zsRef | back to summary |
|---|---|
| private final Inflater. | |
| Inflater | back to summary |
|---|---|
| public Inflater(boolean nowrap) Creates a new decompressor. If the parameter 'nowrap' is true then the ZLIB header and checksum fields will not be used. This provides compatibility with the compression format used by both GZIP and PKZIP. Note When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input. This is required by the ZLIB native library in order to support certain optimizations.
| |
| Inflater | back to summary |
|---|---|
| public Inflater() Creates a new decompressor. | |
| close | back to summary |
|---|---|
| public void close() Implements java. Closes and releases the resources held by this Implementation Specification This method calls the
| |
| 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() | |
| finished | back to summary |
|---|---|
| public boolean finished() Returns true if the end of the compressed data 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 compressed bytes input so far.
| |
| getBytesWritten | back to summary |
|---|---|
| public long getBytesWritten() Returns the total number of uncompressed bytes output so far.
| |
| getRemaining | back to summary |
|---|---|
| public int getRemaining() Returns the total number of bytes remaining in the input buffer. This can be used to find out what bytes still remain in the input buffer after decompression has finished.
| |
| getTotalIn | back to summary |
|---|---|
| public int getTotalIn()
Returns the total number of compressed bytes input so far. Implementation Specification This method returns the equivalent of
| |
| getTotalOut | back to summary |
|---|---|
| public int getTotalOut()
Returns the total number of uncompressed bytes output so far. Implementation Specification This method returns the equivalent of
| |
| hasPendingOutput | back to summary |
|---|---|
| pack-priv boolean hasPendingOutput() | |
| inflate | back to summary |
|---|---|
| public int inflate(byte[] output, int off, int len) throws DataFormatException Uncompresses bytes into specified buffer. Returns actual number of bytes uncompressed. A return value of 0 indicates that needsInput() or needsDictionary() should be called in order to determine if more input data or a preset dictionary is required. In the latter case, getAdler() can be used to get the Adler-32 value of the dictionary required.
If the
The remaining byte count will be reduced by
the number of consumed input bytes. If the
These byte totals, as well as
the total bytes read
and the total bytes written
values, will be updated even in the event that a
| |
| inflate | back to summary |
|---|---|
| public int inflate(byte[] output) throws DataFormatException Uncompresses bytes into specified buffer. Returns actual number of bytes uncompressed. A return value of 0 indicates that needsInput() or needsDictionary() should be called in order to determine if more input data or a preset dictionary is required. In the latter case, getAdler() can be used to get the Adler-32 value of the dictionary required.
The remaining byte count will be reduced by
the number of consumed input bytes. If the
These byte totals, as well as
the total bytes read
and the total bytes written
values, will be updated even in the event that a
| |
| inflate | back to summary |
|---|---|
| public int inflate(ByteBuffer output) throws DataFormatException Uncompresses bytes into specified buffer. Returns actual number of bytes uncompressed. A return value of 0 indicates that needsInput() or needsDictionary() should be called in order to determine if more input data or a preset dictionary is required. In the latter case, getAdler() can be used to get the Adler-32 value of the dictionary required.
On success, the position of the given
The remaining byte count will be reduced by
the number of consumed input bytes. If the
These byte totals, as well as
the total bytes read
and the total bytes written
values, will be updated even in the event that a
| |
| inflateBufferBuffer | back to summary |
|---|---|
| private native long inflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen) throws DataFormatException | |
| inflateBufferBytes | back to summary |
|---|---|
| private native long inflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen) throws DataFormatException | |
| inflateBytesBuffer | back to summary |
|---|---|
| private native long inflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen) throws DataFormatException | |
| inflateBytesBytes | back to summary |
|---|---|
| private native long inflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen) throws DataFormatException | |
| init | back to summary |
|---|---|
| private static native long init(boolean nowrap) | |
| initIDs | back to summary |
|---|---|
| private static native void initIDs() | |
| needsDictionary | back to summary |
|---|---|
| public boolean needsDictionary() Returns true if a preset dictionary is needed for decompression.
| |
| 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 inflater so that a new set of input data can be processed.
| |
| 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 the preset dictionary to the given array of bytes. Should be called when inflate() returns 0 and needsDictionary() returns true indicating that a preset dictionary is required. The method getAdler() can be used to get the Adler-32 value of the dictionary needed.
| |
| setDictionary | back to summary |
|---|---|
| public void setDictionary(byte[] dictionary) Sets the preset dictionary to the given array of bytes. Should be called when inflate() returns 0 and needsDictionary() returns true indicating that a preset dictionary is required. The method getAdler() can be used to get the Adler-32 value of the dictionary needed.
| |
| setDictionary | back to summary |
|---|---|
| public void setDictionary(ByteBuffer dictionary) Sets the preset dictionary to the bytes in the given buffer. Should be called when inflate() returns 0 and needsDictionary() returns true indicating that a preset dictionary is required. The method getAdler() can be used to get the Adler-32 value of the dictionary needed. 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 decompression.
One of the
| |
| setInput | back to summary |
|---|---|
| public void setInput(byte[] input) Sets input data for decompression.
One of the
| |
| setInput | back to summary |
|---|---|
| public void setInput(ByteBuffer input) Sets input data for decompression.
One of the The given buffer's position will be advanced as inflate operations are performed, up to the buffer's limit. The input buffer may be modified (refilled) between inflate 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 inflate operation will result in undefined behavior, which may include incorrect operation results or operation failure.
| |
| 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. | |
| InflaterZStreamRef | back to summary |
|---|---|
| private InflaterZStreamRef(Inflater 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. | |