public class

Inflater

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

Class Overview

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
synchronized void end()
Releases resources associated with this Inflater.
synchronized boolean finished()
Indicates if the Inflater has inflated the entire deflated stream.
synchronized int getAdler()
Returns the Adler32 checksum of the bytes inflated so far, or the checksum of the preset dictionary if needsDictionary() returns true.
synchronized long getBytesRead()
Returns the total number of bytes read by the Inflater.
synchronized long getBytesWritten()
Returns a the total number of bytes written by this Inflater.
synchronized int getRemaining()
Returns the number of bytes of current input remaining to be read by this inflater.
synchronized int getTotalIn()
Returns the total number of bytes of input read by this Inflater.
synchronized int getTotalOut()
Returns the total number of bytes written to the output buffer by this Inflater.
synchronized 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.
synchronized boolean needsDictionary()
Returns true if the input bytes were compressed with a preset dictionary.
synchronized boolean needsInput()
Returns true if setInput(byte[]) must be called before inflation can continue.
synchronized void reset()
Resets this Inflater.
synchronized 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.
synchronized void setDictionary(byte[] dictionary)
Sets the preset dictionary to be used for inflation to dictionary.
synchronized void setInput(byte[] buf, int offset, int byteCount)
Sets the current input to to be decompressed.
synchronized void setInput(byte[] buf)
Sets the current input to to be decompressed.
Protected Methods
void finalize()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Inflater ()

Since: API Level 1

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

public Inflater (boolean noHeader)

Since: API Level 1

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

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

Public Methods

public synchronized void end ()

Since: API Level 1

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.

public synchronized boolean finished ()

Since: API Level 1

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
  • true if all input has been inflated, false otherwise.

public synchronized int getAdler ()

Since: API Level 1

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

public synchronized long getBytesRead ()

Since: API Level 1

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.

public synchronized long getBytesWritten ()

Since: API Level 1

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.

public synchronized int getRemaining ()

Since: API Level 1

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

public synchronized int getTotalIn ()

Since: API Level 1

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

public synchronized int getTotalOut ()

Since: API Level 1

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

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

Since: API Level 1

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

Returns
  • the number of bytes inflated.
Throws
DataFormatException if the underlying stream is corrupted or was not compressed using a Deflater.

public int inflate (byte[] buf)

Since: API Level 1

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

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

public synchronized boolean needsDictionary ()

Since: API Level 1

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.

public synchronized boolean needsInput ()

Since: API Level 1

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

public synchronized void reset ()

Since: API Level 1

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

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

Since: API Level 1

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.

public synchronized void setDictionary (byte[] dictionary)

Since: API Level 1

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

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

Since: API Level 1

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

public synchronized void setInput (byte[] buf)

Since: API Level 1

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

Protected Methods

protected void finalize ()

Since: API Level 1

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.