public class Deflater extends Object
The following code fragment demonstrates a trivial compression and decompression of a string using Deflater and Inflater.
try { // Encode a String into bytes String inputString = "blahblahblah"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output = new byte[100]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); compresser.end(); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output, 0, compressedDataLength); byte[] result = new byte[100]; int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); } catch(java.io.UnsupportedEncodingException ex) { // handle } catch (java.util.zip.DataFormatException ex) { // handle }
Inflater
Modifier and Type | Field and Description |
---|---|
static int |
BEST_COMPRESSION
Compression level for best compression.
|
static int |
BEST_SPEED
Compression level for fastest compression.
|
static int |
DEFAULT_COMPRESSION
Default compression level.
|
static int |
DEFAULT_STRATEGY
Default compression strategy.
|
static int |
DEFLATED
Compression method for the deflate algorithm (the only one currently
supported).
|
static int |
FILTERED
Compression strategy best used for data consisting mostly of small
values with a somewhat random distribution.
|
static int |
FULL_FLUSH
Compression flush mode used to flush out all pending output and
reset the deflater.
|
static int |
HUFFMAN_ONLY
Compression strategy for Huffman coding only.
|
static int |
NO_COMPRESSION
Compression level for no compression.
|
static int |
NO_FLUSH
Compression flush mode used to achieve best compression result.
|
static int |
SYNC_FLUSH
Compression flush mode used to flush out all pending output; may
degrade compression for some compression algorithms.
|
Constructor and Description |
---|
Deflater()
Creates a new compressor with the default compression level.
|
Deflater(int level)
Creates a new compressor using the specified compression level.
|
Deflater(int level,
boolean nowrap)
Creates a new compressor using the specified compression level.
|
Modifier and Type | Method and Description |
---|---|
int |
deflate(byte[] b)
Compresses the input data and fills specified buffer with compressed
data.
|
int |
deflate(byte[] b,
int off,
int len)
Compresses the input data and fills specified buffer with compressed
data.
|
int |
deflate(byte[] b,
int off,
int len,
int flush)
Compresses the input data and fills the specified buffer with compressed
data.
|
void |
end()
Closes the compressor and discards any unprocessed input.
|
protected void |
finalize()
Closes the compressor when garbage is collected.
|
void |
finish()
When called, indicates that compression should end with the current
contents of the input buffer.
|
boolean |
finished()
Returns true if the end of the compressed data output stream has
been reached.
|
int |
getAdler()
Returns the ADLER-32 value of the uncompressed data.
|
long |
getBytesRead()
Returns the total number of uncompressed bytes input so far.
|
long |
getBytesWritten()
Returns the total number of compressed bytes output so far.
|
int |
getTotalIn()
Returns the total number of uncompressed bytes input so far.
|
int |
getTotalOut()
Returns the total number of compressed bytes output so far.
|
boolean |
needsInput()
Returns true if the input data buffer is empty and setInput()
should be called in order to provide more input.
|
void |
reset()
Resets deflater so that a new set of input data can be processed.
|
void |
setDictionary(byte[] b)
Sets preset dictionary for compression.
|
void |
setDictionary(byte[] b,
int off,
int len)
Sets preset dictionary for compression.
|
void |
setInput(byte[] b)
Sets input data for compression.
|
void |
setInput(byte[] b,
int off,
int len)
Sets input data for compression.
|
void |
setLevel(int level)
Sets the compression level to the specified value.
|
void |
setStrategy(int strategy)
Sets the compression strategy to the specified value.
|
public static final int DEFLATED
public static final int NO_COMPRESSION
public static final int BEST_SPEED
public static final int BEST_COMPRESSION
public static final int DEFAULT_COMPRESSION
public static final int FILTERED
public static final int HUFFMAN_ONLY
public static final int DEFAULT_STRATEGY
public static final int NO_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Valuespublic static final int SYNC_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Valuespublic static final int FULL_FLUSH
deflate(byte[], int, int, int)
,
Constant Field Valuespublic Deflater(int level, boolean nowrap)
level
- the compression level (0-9)nowrap
- if true then use GZIP compatible compressionpublic Deflater(int level)
level
- the compression level (0-9)public Deflater()
public void setInput(byte[] b, int off, int len)
b
- the input data bytesoff
- the start offset of the datalen
- the length of the dataneedsInput()
public void setInput(byte[] b)
b
- the input data bytesneedsInput()
public void setDictionary(byte[] b, int off, int len)
b
- the dictionary data bytesoff
- the start offset of the datalen
- the length of the dataInflater.inflate(byte[], int, int)
,
Inflater.getAdler()
public void setDictionary(byte[] b)
b
- the dictionary data bytesInflater.inflate(byte[], int, int)
,
Inflater.getAdler()
public void setStrategy(int strategy)
If the compression strategy is changed, the next invocation
of deflate
will compress the input available so far with
the old strategy (and may be flushed); the new strategy will take
effect only after that invocation.
strategy
- the new compression strategyIllegalArgumentException
- if the compression strategy is
invalidpublic void setLevel(int level)
If the compression level is changed, the next invocation
of deflate
will compress the input available so far
with the old level (and may be flushed); the new level will
take effect only after that invocation.
level
- the new compression level (0-9)IllegalArgumentException
- if the compression level is invalidpublic boolean needsInput()
public void finish()
public boolean finished()
public int deflate(byte[] b, int off, int len)
needsInput
should be called
in order to determine if more input data is required.
This method uses NO_FLUSH
as its compression flush mode.
An invocation of this method of the form deflater.deflate(b, off, len)
yields the same result as the invocation of
deflater.deflate(b, off, len, Deflater.NO_FLUSH)
.
b
- the buffer for the compressed dataoff
- the start offset of the datalen
- the maximum number of bytes of compressed datapublic int deflate(byte[] b)
needsInput
should be called
in order to determine if more input data is required.
This method uses NO_FLUSH
as its compression flush mode.
An invocation of this method of the form deflater.deflate(b)
yields the same result as the invocation of
deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH)
.
b
- the buffer for the compressed datapublic int deflate(byte[] b, int off, int len, int flush)
Compression flush mode is one of the following three modes:
NO_FLUSH
: allows the deflater to decide how much data
to accumulate, before producing output, in order to achieve the best
compression (should be used in normal use scenario). A return value
of 0 in this flush mode indicates that needsInput()
should
be called in order to determine if more input data is required.
SYNC_FLUSH
: all pending output in the deflater is flushed,
to the specified output buffer, so that an inflater that works on
compressed data can get all input data available so far (In particular
the needsInput()
returns true
after this invocation
if enough output space is provided). Flushing with SYNC_FLUSH
may degrade compression for some compression algorithms and so it
should be used only when necessary.
FULL_FLUSH
: all pending output is flushed out as with
SYNC_FLUSH
. The compression state is reset so that the inflater
that works on the compressed output data can restart from this point
if previous compressed data has been damaged or if random access is
desired. Using FULL_FLUSH
too often can seriously degrade
compression.
In the case of FULL_FLUSH
or SYNC_FLUSH
, if
the return value is len
, the space available in output
buffer b
, this method should be invoked again with the same
flush
parameter and more output space.
b
- the buffer for the compressed dataoff
- the start offset of the datalen
- the maximum number of bytes of compressed dataflush
- the compression flush modeIllegalArgumentException
- if the flush mode is invalidpublic int getAdler()
public int getTotalIn()
Since the number of bytes may be greater than
Integer.MAX_VALUE, the getBytesRead()
method is now
the preferred means of obtaining this information.
public long getBytesRead()
public int getTotalOut()
Since the number of bytes may be greater than
Integer.MAX_VALUE, the getBytesWritten()
method is now
the preferred means of obtaining this information.
public long getBytesWritten()
public void reset()
public void end()
protected void finalize()
finalize
in class Object
WeakReference
,
PhantomReference
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.