Class Inflater
public class Inflater extends Object
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.
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidend()Releases resources associated with thisInflater.protected voidfinalize()Invoked when the garbage collector has detected that this instance is no longer reachable.booleanfinished()Indicates if theInflaterhas inflated the entire deflated stream.intgetAdler()Returns theAdler32checksum of the bytes inflated so far, or the checksum of the preset dictionary ifneedsDictionaryreturns true.longgetBytesRead()Returns the total number of bytes read by theInflater.longgetBytesWritten()Returns a the total number of bytes written by thisInflater.intgetRemaining()Returns the number of bytes of current input remaining to be read by this inflater.intgetTotalIn()Returns the total number of bytes of input read by thisInflater.intgetTotalOut()Returns the total number of bytes written to the output buffer by thisInflater.intinflate(byte[] buf)Inflates bytes from the current input and stores them inbuf.intinflate(byte[] buf, int offset, int byteCount)Inflates up tobyteCountbytes from the current input and stores them inbufstarting atoffset.booleanneedsDictionary()Returns true if the input bytes were compressed with a preset dictionary.booleanneedsInput()Returns true ifsetInput(byte[])must be called before inflation can continue.voidreset()Resets thisInflater.voidsetDictionary(byte[] dictionary)Sets the preset dictionary to be used for inflation todictionary.voidsetDictionary(byte[] dictionary, int offset, int byteCount)Sets the preset dictionary to be used for inflation to a subsequence ofdictionarystarting atoffsetand continuing forbyteCountbytes.voidsetInput(byte[] buf)Sets the current input to to be decompressed.voidsetInput(byte[] buf, int offset, int byteCount)Sets the current input to to be decompressed.
-
Constructor Details
-
Inflater
public Inflater()This constructor creates an inflater that expects a header from the input stream. UseInflater(boolean)if the input comes without a ZLIB header. -
Inflater
public Inflater(boolean noHeader)This constructor allows to create an inflater that expects no header from the input stream.- Parameters:
noHeader-trueindicates that no ZLIB header comes with the input.
-
-
Method Details
-
end
public void end()Releases resources associated with thisInflater. Any unused input or output is discarded. This method should be called explicitly in order to free native resources as soon as possible. Afterend()is called, other methods will typically throwIllegalStateException. -
finalize
protected void finalize()Description copied from class:ObjectInvoked 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
finalizeare 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 explicitclosemethod (and implementCloseable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like aBigIntegerwhere 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
ReferenceQueueand 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.
-
finished
public boolean finished()Indicates if theInflaterhas inflated the entire deflated stream. If deflated bytes remain andneedsInput()returnstruethis method will returnfalse. This method should be called after all deflated input is supplied to theInflater.- Returns:
trueif all input has been inflated,falseotherwise.
-
getAdler
public int getAdler()Returns theAdler32checksum of the bytes inflated so far, or the checksum of the preset dictionary ifneedsDictionaryreturns true. -
getBytesRead
public long getBytesRead()Returns the total number of bytes read by theInflater. This method is the same asgetTotalIn()except that it returns alongvalue instead of an integer. -
getBytesWritten
public long getBytesWritten()Returns a the total number of bytes written by thisInflater. This method is the same asgetTotalOutexcept it returns alongvalue instead of an integer. -
getRemaining
public int getRemaining()Returns the number of bytes of current input remaining to be read by this inflater. -
getTotalIn
public int getTotalIn()Returns the total number of bytes of input read by thisInflater. This method is limited to 32 bits; usegetBytesRead()instead. -
getTotalOut
public int getTotalOut()Returns the total number of bytes written to the output buffer by thisInflater. The method is limited to 32 bits; usegetBytesWritten()instead. -
inflate
Inflates bytes from the current input and stores them inbuf.- 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 aDeflater.
-
inflate
Inflates up tobyteCountbytes from the current input and stores them inbufstarting atoffset.- Returns:
- the number of bytes inflated.
- Throws:
DataFormatException- if the underlying stream is corrupted or was not compressed using aDeflater.
-
needsDictionary
public boolean needsDictionary()Returns true if the input bytes were compressed with a preset dictionary. This method should be called if the first call toinflate(byte[])returns 0, to determine whether a dictionary is required. If so,setDictionary(byte[])should be called with the appropriate dictionary before callinginflateagain. UsegetAdler()to determine which dictionary is required. -
needsInput
public boolean needsInput()Returns true ifsetInput(byte[])must be called before inflation can continue. -
reset
public void reset()Resets thisInflater. Should be called prior to inflating a new set of data. -
setDictionary
public void setDictionary(byte[] dictionary)Sets the preset dictionary to be used for inflation todictionary. SeeneedsDictionaryfor details. -
setDictionary
public void setDictionary(byte[] dictionary, int offset, int byteCount)Sets the preset dictionary to be used for inflation to a subsequence ofdictionarystarting atoffsetand continuing forbyteCountbytes. SeeneedsDictionaryfor details. -
setInput
public void setInput(byte[] buf)Sets the current input to to be decompressed. This method should only be called ifneedsInput()returnstrue. -
setInput
public void setInput(byte[] buf, int offset, int byteCount)Sets the current input to to be decompressed. This method should only be called ifneedsInput()returnstrue.
-