APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.zip

public Class Inflater

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Imports
java.lang.ref.Cleaner.Cleanable, java.nio.ByteBuffer, .ReadOnlyBufferException, java.util.Objects, jdk.internal.ref.CleanerFactory, jdk.internal.util.Preconditions

This class provides support for general purpose decompression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents. It is fully described in the specifications at the java.util.zip package description.

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€€"; 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);
// 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);
Author
David Connelly
Since
1.1
See Also
Deflater

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Inflater.InflaterZStreamRef

A reference to the native zlib's z_stream structure.

Field Summary

Modifier and TypeField 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.InflaterZStreamRef

Constructor Summary

AccessConstructor and Description
public
Inflater(boolean
if true then support GZIP compatible compression
nowrap
)

Creates a new decompressor.

public
Inflater()

Creates a new decompressor.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.lang.AutoCloseable.close.

Closes and releases the resources held by this Inflater and discards any unprocessed input.

public void
end()

Closes and releases the resources held by this Inflater and discards any unprocessed input.

private static native void
end(long addr)

private void
public boolean

Returns:

true if the end of the compressed data stream has been reached
finished
()

Returns true if the end of the compressed data stream has been reached.

public int

Returns:

the ADLER-32 value of the uncompressed data
getAdler
()

Returns the ADLER-32 value of the uncompressed data.

private static native int
getAdler(long addr)

public long

Returns:

the total (non-negative) number of compressed bytes input so far
getBytesRead
()

Returns the total number of compressed bytes input so far.

public long

Returns:

the total (non-negative) number of uncompressed bytes output so far
getBytesWritten
()

Returns the total number of uncompressed bytes output so far.

public int

Returns:

the total number of bytes remaining in the input buffer
getRemaining
()

Returns the total number of bytes remaining in the input buffer.

public int

Returns:

the total number of compressed bytes input so far
getTotalIn
()
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
getTotalOut
()
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 bytes
inflate
(byte[]
the 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 bytes
inflate
(byte[]
the buffer for the uncompressed data
output
)

Uncompresses bytes into specified buffer.

public int

Returns:

the actual number of uncompressed bytes
inflate
(ByteBuffer
the buffer for the uncompressed data
output
)

Uncompresses bytes into specified buffer.

private native long
inflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen)

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
init(boolean nowrap)

private static native void
public boolean

Returns:

true if a preset dictionary is needed for decompression
needsDictionary
()

Returns true if a preset dictionary is needed for decompression.

public boolean

Returns:

true if no data remains in the input buffer
needsInput
()

Returns true if no data remains in the input buffer.

public void
reset()

Resets inflater so that a new set of input data can be processed.

private static native void
reset(long addr)

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
setDictionary(long addr, byte[] b, int off, int len)

private static native void
setDictionaryBuffer(long addr, long bufAddress, int len)

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
setInput(byte[]
the input data bytes
input
)

Sets input data for decompression.

public void
setInput(ByteBuffer
the input data bytes
input
)

Sets input data for decompression.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytesReadback to summary
private long bytesRead
bytesWrittenback to summary
private long bytesWritten
finishedback to summary
private boolean finished
inputback to summary
private ByteBuffer input
inputArrayback to summary
private byte[] inputArray
inputConsumedback to summary
private int inputConsumed
inputLimback to summary
private int inputLim
inputPosback to summary
private int inputPos
needDictback to summary
private boolean needDict
outputConsumedback to summary
private int outputConsumed
pendingOutputback to summary
private boolean pendingOutput
zsRefback to summary
private final Inflater.InflaterZStreamRef zsRef

Constructor Detail

Inflaterback 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.

Parameters
nowrap:boolean

if true then support GZIP compatible compression

Annotations
@SuppressWarnings:this-escape
Inflaterback to summary
public Inflater()

Creates a new decompressor.

Method Detail

closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Closes and releases the resources held by this Inflater and discards any unprocessed input.

Implementation Specification

This method calls the end() method.

Annotations
@Override
Since
25
endback to summary
public void end()

Closes and releases the resources held by this Inflater and discards any unprocessed input.

If the Inflater is already closed then invoking this method has no effect.

Implementation Specification

Subclasses should override this method to clean up the resources acquired by the subclass.

See Also
close()
endback to summary
private static native void end(long addr)
ensureOpenback to summary
private void ensureOpen()
finishedback to summary
public boolean finished()

Returns true if the end of the compressed data stream has been reached.

Returns:boolean

true if the end of the compressed data stream has been reached

getAdlerback to summary
public int getAdler()

Returns the ADLER-32 value of the uncompressed data.

Returns:int

the ADLER-32 value of the uncompressed data

Exceptions
IllegalStateException:
if the Inflater is closed
getAdlerback to summary
private static native int getAdler(long addr)
getBytesReadback to summary
public long getBytesRead()

Returns the total number of compressed bytes input so far.

Returns:long

the total (non-negative) number of compressed bytes input so far

Exceptions
IllegalStateException:
if the Inflater is closed
Since
1.5
getBytesWrittenback to summary
public long getBytesWritten()

Returns the total number of uncompressed bytes output so far.

Returns:long

the total (non-negative) number of uncompressed bytes output so far

Exceptions
IllegalStateException:
if the Inflater is closed
Since
1.5
getRemainingback 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.

Returns:int

the total number of bytes remaining in the input buffer

getTotalInback to summary
public int getTotalIn()

Deprecated

since 23.

Use getBytesRead() instead

Returns the total number of compressed bytes input so far.

Implementation Specification

This method returns the equivalent of (int) getBytesRead() and therefore cannot return the correct value when it is greater than Integer#MAX_VALUE.

Returns:int

the total number of compressed bytes input so far

Annotations
@Deprecated
since:23
Exceptions
IllegalStateException:
if the Inflater is closed
getTotalOutback to summary
public int getTotalOut()

Deprecated

since 23.

Use getBytesWritten() instead

Returns the total number of uncompressed bytes output so far.

Implementation Specification

This method returns the equivalent of (int) getBytesWritten() and therefore cannot return the correct value when it is greater than Integer#MAX_VALUE.

Returns:int

the total number of uncompressed bytes output so far

Annotations
@Deprecated
since:23
Exceptions
IllegalStateException:
if the Inflater is closed
hasPendingOutputback to summary
pack-priv boolean hasPendingOutput()
inflateback 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 setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced by the number of bytes consumed by this operation, even in the event that a DataFormatException is thrown.

The remaining byte count will be reduced by the number of consumed input bytes. If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced the number of consumed bytes.

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 DataFormatException is thrown to reflect the amount of data consumed and produced before the exception occurred.

Parameters
output:byte[]

the buffer for the uncompressed data

off:int

the start offset of the data

len:int

the maximum number of uncompressed bytes

Returns:int

the actual number of uncompressed bytes

Exceptions
DataFormatException:
if the compressed data format is invalid
IllegalStateException:
if the Inflater is closed
See Also
Inflater#needsInput, Inflater#needsDictionary
inflateback 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 setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced the number of consumed bytes.

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 DataFormatException is thrown to reflect the amount of data consumed and produced before the exception occurred.

Parameters
output:byte[]

the buffer for the uncompressed data

Returns:int

the actual number of uncompressed bytes

Exceptions
DataFormatException:
if the compressed data format is invalid
IllegalStateException:
if the Inflater is closed
See Also
Inflater#needsInput, Inflater#needsDictionary
inflateback 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 output byte buffer will be advanced by as many bytes as were produced by the operation, which is equal to the number returned by this method. Note that the position of the output buffer will be advanced even in the event that a DataFormatException is thrown.

The remaining byte count will be reduced by the number of consumed input bytes. If the setInput(ByteBuffer) method was called to provide a buffer for input, the input buffer's position will be advanced the number of consumed bytes.

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 DataFormatException is thrown to reflect the amount of data consumed and produced before the exception occurred.

Parameters
output:ByteBuffer

the buffer for the uncompressed data

Returns:int

the actual number of uncompressed bytes

Exceptions
DataFormatException:
if the compressed data format is invalid
ReadOnlyBufferException:
if the given output buffer is read-only
IllegalStateException:
if the Inflater is closed
Since
11
See Also
Inflater#needsInput, Inflater#needsDictionary
inflateBufferBufferback to summary
private native long inflateBufferBuffer(long addr, long inputAddress, int inputLen, long outputAddress, int outputLen) throws DataFormatException
inflateBufferBytesback to summary
private native long inflateBufferBytes(long addr, long inputAddress, int inputLen, byte[] outputArray, int outputOff, int outputLen) throws DataFormatException
inflateBytesBufferback to summary
private native long inflateBytesBuffer(long addr, byte[] inputArray, int inputOff, int inputLen, long outputAddress, int outputLen) throws DataFormatException
inflateBytesBytesback to summary
private native long inflateBytesBytes(long addr, byte[] inputArray, int inputOff, int inputLen, byte[] outputArray, int outputOff, int outputLen) throws DataFormatException
initback to summary
private static native long init(boolean nowrap)
initIDsback to summary
private static native void initIDs()
needsDictionaryback to summary
public boolean needsDictionary()

Returns true if a preset dictionary is needed for decompression.

Returns:boolean

true if a preset dictionary is needed for decompression

See Also
Inflater#setDictionary
needsInputback 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 setInput() methods should be called in order to provide more input.

Returns:boolean

true if no data remains in the input buffer

resetback to summary
public void reset()

Resets inflater so that a new set of input data can be processed.

Exceptions
IllegalStateException:
if the Inflater is closed
resetback to summary
private static native void reset(long addr)
setDictionaryback 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.

Parameters
dictionary:byte[]

the dictionary data bytes

off:int

the start offset of the data

len:int

the length of the data

Exceptions
IllegalStateException:
if the Inflater is closed
See Also
Inflater#needsDictionary, Inflater#getAdler
setDictionaryback 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.

Parameters
dictionary:byte[]

the dictionary data bytes

Exceptions
IllegalStateException:
if the Inflater is closed
See Also
Inflater#needsDictionary, Inflater#getAdler
setDictionaryback 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.

Parameters
dictionary:ByteBuffer

the dictionary data bytes

Exceptions
IllegalStateException:
if the Inflater is closed
Since
11
See Also
Inflater#needsDictionary, Inflater#getAdler
setDictionaryback to summary
private static native void setDictionary(long addr, byte[] b, int off, int len)
setDictionaryBufferback to summary
private static native void setDictionaryBuffer(long addr, long bufAddress, int len)
setInputback to summary
public void setInput(byte[] input, int off, int len)

Sets input data for decompression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
input:byte[]

the input data bytes

off:int

the start offset of the input data

len:int

the length of the input data

See Also
Inflater#needsInput
setInputback to summary
public void setInput(byte[] input)

Sets input data for decompression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
input:byte[]

the input data bytes

See Also
Inflater#needsInput
setInputback to summary
public void setInput(ByteBuffer input)

Sets input data for decompression.

One of the setInput() methods should be called whenever needsInput() returns true indicating that more input data is required.

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.

Parameters
input:ByteBuffer

the input data bytes

Since
11
See Also
Inflater#needsInput
java.util.zip back to summary

pack-priv Class Inflater.InflaterZStreamRef

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

A reference to the native zlib's z_stream structure. It also serves as the "cleaner" to clean up the native resource when the Inflater is ended, closed or cleaned.

Field Summary

Modifier and TypeField and Description
private long
private final Cleaner.Cleanable

Constructor Summary

AccessConstructor and Description
private
InflaterZStreamRef(Inflater owner, long addr)

Method Summary

Modifier and TypeMethod and Description
pack-priv long
pack-priv void
public synchronized void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addressback to summary
private long address
cleanableback to summary
private final Cleaner.Cleanable cleanable

Constructor Detail

InflaterZStreamRefback to summary
private InflaterZStreamRef(Inflater owner, long addr)

Method Detail

addressback to summary
pack-priv long address()
cleanback to summary
pack-priv void clean()
runback to summary
public synchronized void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.