Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Inflater

public class Inflater
extends Object

java.lang.Object
   ↳ java.util.zip.Inflater


This class decompresses data that was compressed using the DEFLATE algorithm (see specification).

It is usually more convenient to use InflaterInputStream.

To decompress an in-memory byte[] to another in-memory byte[] manually:

     byte[] compressedBytes = ...
     int decompressedByteCount = ... // From your format's metadata.
     Inflater inflater = new Inflater();
     inflater.setInput(compressedBytes, 0, compressedBytes.length);
     byte[] decompressedBytes = new byte[decompressedByteCount];
     if (inflater.inflate(decompressedBytes) != decompressedByteCount) {
         throw new AssertionError();
     }
     inflater.end();
 

In situations where you don't have all the input in one array (or have so much input that you want to feed it to the inflater in chunks), it's possible to call setInput(byte[]) repeatedly, but you're much better off using InflaterInputStream to handle all this for you.

If you don't know how big the decompressed data will be, you can call inflate(byte[]) repeatedly on a temporary buffer, copying the bytes to a ByteArrayOutputStream, but this is probably another sign you'd be better off using InflaterInputStream.

Summary

Public constructors

Inflater()

This constructor creates an inflater that expects a header from the input stream.

Inflater(boolean noHeader)

This constructor allows to create an inflater that expects no header from the input stream.

Public methods

void end()

Releases resources associated with this Inflater.

boolean finished()

Indicates if the Inflater has inflated the entire deflated stream.

int getAdler()

Returns the Adler32 checksum of the bytes inflated so far, or the checksum of the preset dictionary if needsDictionary() returns true.

long getBytesRead()

Returns the total number of bytes read by the Inflater.

long getBytesWritten()

Returns a the total number of bytes written by this Inflater.

int getRemaining()

Returns the number of bytes of current input remaining to be read by this inflater.

int getTotalIn()

Returns the total number of bytes of input read by this Inflater.

int getTotalOut()

Returns the total number of bytes written to the output buffer by this Inflater.

int inflate(byte[] buf, int offset, int byteCount)

Inflates up to byteCount bytes from the current input and stores them in buf starting at offset.

int inflate(byte[] buf)

Inflates bytes from the current input and stores them in buf.

boolean needsDictionary()

Returns true if the input bytes were compressed with a preset dictionary.

boolean needsInput()

Returns true if setInput(byte[]) must be called before inflation can continue.

void reset()

Resets this Inflater.

void setDictionary(byte[] dictionary, int offset, int byteCount)

Sets the preset dictionary to be used for inflation to a subsequence of dictionary starting at offset and continuing for byteCount bytes.

void setDictionary(byte[] dictionary)

Sets the preset dictionary to be used for inflation to dictionary.

void setInput(byte[] buf, int offset, int byteCount)

Sets the current input to to be decompressed.

void setInput(byte[] buf)

Sets the current input to to be decompressed.

Protected methods

void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.

Inherited methods

From class java.lang.Object

Public constructors

Inflater

Added in API level 1
Inflater ()

This constructor creates an inflater that expects a header from the input stream. Use Inflater(boolean) if the input comes without a ZLIB header.

Inflater

Added in API level 1
Inflater (boolean noHeader)

This constructor allows to create an inflater that expects no header from the input stream.

Parameters
noHeader boolean: true indicates that no ZLIB header comes with the input.

Public methods

end

Added in API level 1
void end ()

Releases resources associated with this Inflater. Any unused input or output is discarded. This method should be called explicitly in order to free native resources as soon as possible. After end() is called, other methods will typically throw IllegalStateException.

finished

Added in API level 1
boolean finished ()

Indicates if the Inflater has inflated the entire deflated stream. If deflated bytes remain and needsInput() returns true this method will return false. This method should be called after all deflated input is supplied to the Inflater.

Returns
boolean true if all input has been inflated, false otherwise.

getAdler

Added in API level 1
int getAdler ()

Returns the Adler32 checksum of the bytes inflated so far, or the checksum of the preset dictionary if needsDictionary() returns true.

Returns
int

getBytesRead

Added in API level 1
long getBytesRead ()

Returns the total number of bytes read by the Inflater. This method is the same as getTotalIn() except that it returns a long value instead of an integer.

Returns
long

getBytesWritten

Added in API level 1
long getBytesWritten ()

Returns a the total number of bytes written by this Inflater. This method is the same as getTotalOut except it returns a long value instead of an integer.

Returns
long

getRemaining

Added in API level 1
int getRemaining ()

Returns the number of bytes of current input remaining to be read by this inflater.

Returns
int

getTotalIn

Added in API level 1
int getTotalIn ()

Returns the total number of bytes of input read by this Inflater. This method is limited to 32 bits; use getBytesRead() instead.

Returns
int

getTotalOut

Added in API level 1
int getTotalOut ()

Returns the total number of bytes written to the output buffer by this Inflater. The method is limited to 32 bits; use getBytesWritten() instead.

Returns
int

inflate

Added in API level 1
int inflate (byte[] buf, 
                int offset, 
                int byteCount)

Inflates up to byteCount bytes from the current input and stores them in buf starting at offset.

Parameters
buf byte
offset int
byteCount int
Returns
int the number of bytes inflated.
Throws
DataFormatException if the underlying stream is corrupted or was not compressed using a Deflater.

inflate

Added in API level 1
int inflate (byte[] buf)

Inflates bytes from the current input and stores them in buf.

Parameters
buf byte: the buffer where decompressed data bytes are written.
Returns
int the number of bytes inflated.
Throws
DataFormatException if the underlying stream is corrupted or was not compressed using a Deflater.

needsDictionary

Added in API level 1
boolean needsDictionary ()

Returns true if the input bytes were compressed with a preset dictionary. This method should be called if the first call to inflate(byte[]) returns 0, to determine whether a dictionary is required. If so, setDictionary(byte[]) should be called with the appropriate dictionary before calling inflate again. Use getAdler() to determine which dictionary is required.

Returns
boolean

needsInput

Added in API level 1
boolean needsInput ()

Returns true if setInput(byte[]) must be called before inflation can continue.

Returns
boolean

reset

Added in API level 1
void reset ()

Resets this Inflater. Should be called prior to inflating a new set of data.

setDictionary

Added in API level 1
void setDictionary (byte[] dictionary, 
                int offset, 
                int byteCount)

Sets the preset dictionary to be used for inflation to a subsequence of dictionary starting at offset and continuing for byteCount bytes. See needsDictionary() for details.

Parameters
dictionary byte
offset int
byteCount int

setDictionary

Added in API level 1
void setDictionary (byte[] dictionary)

Sets the preset dictionary to be used for inflation to dictionary. See needsDictionary() for details.

Parameters
dictionary byte

setInput

Added in API level 1
void setInput (byte[] buf, 
                int offset, 
                int byteCount)

Sets the current input to to be decompressed. This method should only be called if needsInput() returns true.

Parameters
buf byte
offset int
byteCount int

setInput

Added in API level 1
void setInput (byte[] buf)

Sets the current input to to be decompressed. This method should only be called if needsInput() returns true.

Parameters
buf byte

Protected methods

finalize

Added in API level 1
void finalize ()

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.