Deflater

public class Deflater extends Object

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);
     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
 }
 

See Also

Constant Summary

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 FULL_FLUSH Compression flush mode used to flush out all pending output and reset the deflater.
int HUFFMAN_ONLY Compression strategy for Huffman coding only.
int NO_COMPRESSION Compression level for no compression.
int NO_FLUSH Compression flush mode used to achieve best compression result.
int SYNC_FLUSH Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Public Constructor Summary

Deflater(int level, boolean nowrap)
Creates a new compressor using the specified compression level.
Deflater(int level)
Creates a new compressor using the specified compression level.
Deflater()
Creates a new compressor with the default compression level.

Public Method Summary

int
deflate(byte[] b, int off, int len)
Compresses the input data and fills specified buffer with compressed data.
int
deflate(byte[] b)
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.
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, int off, int len)
Sets preset dictionary for compression.
void
setDictionary(byte[] b)
Sets preset dictionary for compression.
void
setInput(byte[] b, int off, int len)
Sets input data for compression.
void
setInput(byte[] b)
Sets input data for compression.
void
setLevel(int level)
Sets the current compression level to the specified value.
void
setStrategy(int strategy)
Sets the compression strategy to the specified value.

Protected Method Summary

void
finalize()
Closes the compressor when garbage is collected.

Inherited Method Summary

Constants

public static final int BEST_COMPRESSION