java.lang.Object | |
↳ | java.util.zip.Deflater |
This class provides support for general purpose compression 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.
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); // 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 }
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | BEST_COMPRESSION | Compression level for best compression. | |||||||||
int | BEST_SPEED | Compression level for fastest compression. | |||||||||
int | DEFAULT_COMPRESSION | Default compression level. | |||||||||
int | DEFAULT_STRATEGY | Default compression strategy. | |||||||||
int | DEFLATED | Compression method for the deflate algorithm (the only one currently supported). | |||||||||
int | FILTERED | Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. | |||||||||
int | HUFFMAN_ONLY | Compression strategy for Huffman coding only. | |||||||||
int | NO_COMPRESSION | Compression level for no compression. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a new compressor using the specified compression level.
| |||||||||||
Creates a new compressor using the specified compression level.
| |||||||||||
Creates a new compressor with the default compression level.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Fills specified buffer with compressed data.
| |||||||||||
Fills specified buffer with compressed data.
| |||||||||||
Closes the compressor and discards any unprocessed input.
| |||||||||||
When called, indicates that compression should end with the current
contents of the input buffer.
| |||||||||||
Returns true if the end of the compressed data output stream has
been reached.
| |||||||||||
Returns the ADLER-32 value of the uncompressed data.
| |||||||||||
Returns the total number of uncompressed bytes input so far.
| |||||||||||
Returns the total number of compressed bytes output so far.
| |||||||||||
Returns the total number of uncompressed bytes input so far.
| |||||||||||
Returns the total number of compressed bytes output so far.
| |||||||||||
Returns true if the input data buffer is empty and setInput()
should be called in order to provide more input.
| |||||||||||
Resets deflater so that a new set of input data can be processed.
| |||||||||||
Sets preset dictionary for compression.
| |||||||||||
Sets preset dictionary for compression.
| |||||||||||
Sets input data for compression.
| |||||||||||
Sets input data for compression.
| |||||||||||
Sets the current compression level to the specified value.
| |||||||||||
Sets the compression strategy to the specified value.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Closes the compressor when garbage is collected.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Compression level for best compression.
Compression level for fastest compression.
Default compression level.
Default compression strategy.
Compression method for the deflate algorithm (the only one currently supported).
Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching.
Compression strategy for Huffman coding only.
Compression level for no compression.
Creates a new compressor using the specified compression level. If 'nowrap' is true then the ZLIB header and checksum fields will not be used in order to support the compression format used in both GZIP and PKZIP.
level | the compression level (0-9) |
---|---|
nowrap | if true then use GZIP compatible compression |
Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.
level | the compression level (0-9) |
---|
Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format.
Fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput() should be called in order to determine if more input data is required.
b | the buffer for the compressed data |
---|---|
off | the start offset of the data |
len | the maximum number of bytes of compressed data |
Fills specified buffer with compressed data. Returns actual number of bytes of compressed data. A return value of 0 indicates that needsInput() should be called in order to determine if more input data is required.
b | the buffer for the compressed data |
---|
Closes the compressor and discards any unprocessed input. This method should be called when the compressor is no longer being used, but will also be called automatically by the finalize() method. Once this method is called, the behavior of the Deflater object is undefined.
When called, indicates that compression should end with the current contents of the input buffer.
Returns true if the end of the compressed data output stream has been reached.
Returns the ADLER-32 value of the uncompressed data.
Returns the total number of uncompressed bytes input so far.
Returns the total number of compressed bytes output so far.
Returns the total number of uncompressed bytes input so far.
Since the number of bytes may be greater than
Integer.MAX_VALUE, the getBytesRead()
method is now
the preferred means of obtaining this information.
Returns the total number of compressed bytes output so far.
Since the number of bytes may be greater than
Integer.MAX_VALUE, the getBytesWritten()
method is now
the preferred means of obtaining this information.
Returns true if the input data buffer is empty and setInput() should be called in order to provide more input.
Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.
Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.
b | the dictionary data bytes |
---|---|
off | the start offset of the data |
len | the length of the data |
Sets preset dictionary for compression. A preset dictionary is used when the history buffer can be predetermined. When the data is later uncompressed with Inflater.inflate(), Inflater.getAdler() can be called in order to get the Adler-32 value of the dictionary required for decompression.
b | the dictionary data bytes |
---|
Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.
b | the input data bytes |
---|---|
off | the start offset of the data |
len | the length of the data |
Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.
b | the input data bytes |
---|
Sets the current compression level to the specified value.
level | the new compression level (0-9) |
---|
IllegalArgumentException | if the compression level is invalid |
---|
Sets the compression strategy to the specified value.
strategy | the new compression strategy |
---|
IllegalArgumentException | if the compression strategy is invalid |
---|
Closes the compressor when garbage is collected.