java.util.zip
Class Deflater

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.zip.Deflater

public class Deflater
extends Object sample code for java.lang.Object definition code for java.lang.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.

 // 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");
 

See Also:
Inflater sample code for java.util.zip.Inflater definition code for java.util.zip.Inflater

Field Summary
static int BEST_COMPRESSION sample code for java.util.zip.Deflater.BEST_COMPRESSION definition code for java.util.zip.Deflater.BEST_COMPRESSION
          Compression level for best compression.
static int BEST_SPEED sample code for java.util.zip.Deflater.BEST_SPEED definition code for java.util.zip.Deflater.BEST_SPEED
          Compression level for fastest compression.
static int DEFAULT_COMPRESSION sample code for java.util.zip.Deflater.DEFAULT_COMPRESSION definition code for java.util.zip.Deflater.DEFAULT_COMPRESSION
          Default compression level.
static int DEFAULT_STRATEGY sample code for java.util.zip.Deflater.DEFAULT_STRATEGY definition code for java.util.zip.Deflater.DEFAULT_STRATEGY
          Default compression strategy.
static int DEFLATED sample code for java.util.zip.Deflater.DEFLATED definition code for java.util.zip.Deflater.DEFLATED
          Compression method for the deflate algorithm (the only one currently supported).
static int FILTERED sample code for java.util.zip.Deflater.FILTERED definition code for java.util.zip.Deflater.FILTERED
          Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.
static int HUFFMAN_ONLY sample code for java.util.zip.Deflater.HUFFMAN_ONLY definition code for java.util.zip.Deflater.HUFFMAN_ONLY
          Compression strategy for Huffman coding only.
static int NO_COMPRESSION sample code for java.util.zip.Deflater.NO_COMPRESSION definition code for java.util.zip.Deflater.NO_COMPRESSION
          Compression level for no compression.
 
Constructor Summary
Deflater sample code for java.util.zip.Deflater.Deflater() definition code for java.util.zip.Deflater.Deflater() ()
          Creates a new compressor with the default compression level.
Deflater sample code for java.util.zip.Deflater.Deflater(int) definition code for java.util.zip.Deflater.Deflater(int) (int level)
          Creates a new compressor using the specified compression level.
Deflater sample code for java.util.zip.Deflater.Deflater(int, boolean) definition code for java.util.zip.Deflater.Deflater(int, boolean) (int level, boolean nowrap)
          Creates a new compressor using the specified compression level.
 
Method Summary
 int deflate sample code for java.util.zip.Deflater.deflate(byte[]) definition code for java.util.zip.Deflater.deflate(byte[]) (byte[] b)
          Fills specified buffer with compressed data.
 int deflate sample code for java.util.zip.Deflater.deflate(byte[], int, int) definition code for java.util.zip.Deflater.deflate(byte[], int, int) (byte[] b, int off, int len)
          Fills specified buffer with compressed data.
 void end sample code for java.util.zip.Deflater.end() definition code for java.util.zip.Deflater.end() ()
          Closes the compressor and discards any unprocessed input.
protected  void finalize sample code for java.util.zip.Deflater.finalize() definition code for java.util.zip.Deflater.finalize() ()
          Closes the compressor when garbage is collected.
 void finish sample code for java.util.zip.Deflater.finish() definition code for java.util.zip.Deflater.finish() ()
          When called, indicates that compression should end with the current contents of the input buffer.
 boolean finished sample code for java.util.zip.Deflater.finished() definition code for java.util.zip.Deflater.finished() ()
          Returns true if the end of the compressed data output stream has been reached.
 int getAdler sample code for java.util.zip.Deflater.getAdler() definition code for java.util.zip.Deflater.getAdler() ()
          Returns the ADLER-32 value of the uncompressed data.
 long getBytesRead sample code for java.util.zip.Deflater.getBytesRead() definition code for java.util.zip.Deflater.getBytesRead() ()
          Returns the total number of uncompressed bytes input so far.
 long getBytesWritten sample code for java.util.zip.Deflater.getBytesWritten() definition code for java.util.zip.Deflater.getBytesWritten() ()
          Returns the total number of compressed bytes output so far.
 int getTotalIn sample code for java.util.zip.Deflater.getTotalIn() definition code for java.util.zip.Deflater.getTotalIn() ()
          Returns the total number of uncompressed bytes input so far.
 int getTotalOut sample code for java.util.zip.Deflater.getTotalOut() definition code for java.util.zip.Deflater.getTotalOut() ()
          Returns the total number of compressed bytes output so far.
 boolean needsInput sample code for java.util.zip.Deflater.needsInput() definition code for java.util.zip.Deflater.needsInput() ()
          Returns true if the input data buffer is empty and setInput() should be called in order to provide more input.
 void reset sample code for java.util.zip.Deflater.reset() definition code for java.util.zip.Deflater.reset() ()
          Resets deflater so that a new set of input data can be processed.
 void setDictionary sample code for java.util.zip.Deflater.setDictionary(byte[]) definition code for java.util.zip.Deflater.setDictionary(byte[]) (byte[] b)
          Sets preset dictionary for compression.
 void setDictionary sample code for java.util.zip.Deflater.setDictionary(byte[], int, int) definition code for java.util.zip.Deflater.setDictionary(byte[], int, int) (byte[] b, int off, int len)
          Sets preset dictionary for compression.
 void setInput sample code for java.util.zip.Deflater.setInput(byte[]) definition code for java.util.zip.Deflater.setInput(byte[]) (byte[] b)
          Sets input data for compression.
 void setInput sample code for java.util.zip.Deflater.setInput(byte[], int, int) definition code for java.util.zip.Deflater.setInput(byte[], int, int) (byte[] b, int off, int len)
          Sets input data for compression.
 void setLevel sample code for java.util.zip.Deflater.setLevel(int) definition code for java.util.zip.Deflater.setLevel(int) (int level)
          Sets the current compression level to the specified value.
 void setStrategy sample code for java.util.zip.Deflater.setStrategy(int) definition code for java.util.zip.Deflater.setStrategy(int) (int strategy)
          Sets the compression strategy to the specified value.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

DEFLATED sample code for java.util.zip.Deflater.DEFLATED

public static final int DEFLATED
Compression method for the deflate algorithm (the only one currently supported).

See Also:
Constant Field Values

NO_COMPRESSION sample code for java.util.zip.Deflater.NO_COMPRESSION

public static final int NO_COMPRESSION
Compression level for no compression.

See Also:
Constant Field Values

BEST_SPEED sample code for java.util.zip.Deflater.BEST_SPEED

public static final int BEST_SPEED
Compression level for fastest compression.

See Also:
Constant Field Values

BEST_COMPRESSION sample code for java.util.zip.Deflater.BEST_COMPRESSION

public static final int BEST_COMPRESSION
Compression level for best compression.

See Also:
Constant Field Values

DEFAULT_COMPRESSION sample code for java.util.zip.Deflater.DEFAULT_COMPRESSION

public static final int DEFAULT_COMPRESSION
Default compression level.

See Also:
Constant Field Values

FILTERED sample code for java.util.zip.Deflater.FILTERED

public static final int FILTERED
Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. Forces more Huffman coding and less string matching.

See Also:
Constant Field Values

HUFFMAN_ONLY sample code for java.util.zip.Deflater.HUFFMAN_ONLY

public static final int HUFFMAN_ONLY
Compression strategy for Huffman coding only.

See Also:
Constant Field Values

DEFAULT_STRATEGY sample code for java.util.zip.Deflater.DEFAULT_STRATEGY

public static final int DEFAULT_STRATEGY
Default compression strategy.

See Also:
Constant Field Values
Constructor Detail

Deflater sample code for java.util.zip.Deflater(int, boolean) definition code for java.util.zip.Deflater(int, boolean)

public Deflater(int level,
                boolean nowrap)
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.

Parameters:
level - the compression level (0-9)
nowrap - if true then use GZIP compatible compression

Deflater sample code for java.util.zip.Deflater(int) definition code for java.util.zip.Deflater(int)

public Deflater(int level)
Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.

Parameters:
level - the compression level (0-9)

Deflater sample code for java.util.zip.Deflater() definition code for java.util.zip.Deflater()

public Deflater()
Creates a new compressor with the default compression level. Compressed data will be generated in ZLIB format.

Method Detail

setInput sample code for java.util.zip.Deflater.setInput(byte[], int, int) definition code for java.util.zip.Deflater.setInput(byte[], int, int)

public void setInput(byte[] b,
                     int off,
                     int len)
Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.

Parameters:
b - the input data bytes
off - the start offset of the data
len - the length of the data
See Also:
needsInput() sample code for java.util.zip.Deflater.needsInput() definition code for java.util.zip.Deflater.needsInput()

setInput sample code for java.util.zip.Deflater.setInput(byte[]) definition code for java.util.zip.Deflater.setInput(byte[])

public void setInput(byte[] b)
Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.

Parameters:
b - the input data bytes
See Also:
needsInput() sample code for java.util.zip.Deflater.needsInput() definition code for java.util.zip.Deflater.needsInput()

setDictionary sample code for java.util.zip.Deflater.setDictionary(byte[], int, int) definition code for java.util.zip.Deflater.setDictionary(byte[], int, int)

public void setDictionary(byte[] b,
                          int off,
                          int len)
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.

Parameters:
b - the dictionary data bytes
off - the start offset of the data
len - the length of the data
See Also:
Inflater.inflate(byte[], int, int) sample code for java.util.zip.Inflater.inflate(byte[], int, int) definition code for java.util.zip.Inflater.inflate(byte[], int, int) , Inflater.getAdler() sample code for java.util.zip.Inflater.getAdler() definition code for java.util.zip.Inflater.getAdler()

setDictionary sample code for java.util.zip.Deflater.setDictionary(byte[]) definition code for java.util.zip.Deflater.setDictionary(byte[])

public void setDictionary(byte[] b)
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.

Parameters:
b - the dictionary data bytes
See Also:
Inflater.inflate(byte[], int, int) sample code for java.util.zip.Inflater.inflate(byte[], int, int) definition code for java.util.zip.Inflater.inflate(byte[], int, int) , Inflater.getAdler() sample code for java.util.zip.Inflater.getAdler() definition code for java.util.zip.Inflater.getAdler()

setStrategy sample code for java.util.zip.Deflater.setStrategy(int) definition code for java.util.zip.Deflater.setStrategy(int)

public void setStrategy(int strategy)
Sets the compression strategy to the specified value.

Parameters:
strategy - the new compression strategy
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the compression strategy is invalid

setLevel sample code for java.util.zip.Deflater.setLevel(int) definition code for java.util.zip.Deflater.setLevel(int)

public void setLevel(int level)
Sets the current compression level to the specified value.

Parameters:
level - the new compression level (0-9)
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the compression level is invalid

needsInput sample code for java.util.zip.Deflater.needsInput() definition code for java.util.zip.Deflater.needsInput()

public boolean needsInput()
Returns true if the input data buffer is empty and setInput() should be called in order to provide more input.

Returns:
true if the input data buffer is empty and setInput() should be called in order to provide more input

finish sample code for java.util.zip.Deflater.finish() definition code for java.util.zip.Deflater.finish()

public void finish()
When called, indicates that compression should end with the current contents of the input buffer.


finished sample code for java.util.zip.Deflater.finished() definition code for java.util.zip.Deflater.finished()

public boolean finished()
Returns true if the end of the compressed data output stream has been reached.

Returns:
true if the end of the compressed data output stream has been reached

deflate sample code for java.util.zip.Deflater.deflate(byte[], int, int) definition code for java.util.zip.Deflater.deflate(byte[], int, int)

public int deflate(byte[] b,
                   int off,
                   int len)
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.

Parameters:
b - the buffer for the compressed data
off - the start offset of the data
len - the maximum number of bytes of compressed data
Returns:
the actual number of bytes of compressed data

deflate sample code for java.util.zip.Deflater.deflate(byte[]) definition code for java.util.zip.Deflater.deflate(byte[])

public int deflate(byte[] b)
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.

Parameters:
b - the buffer for the compressed data
Returns:
the actual number of bytes of compressed data

getAdler sample code for java.util.zip.Deflater.getAdler() definition code for java.util.zip.Deflater.getAdler()

public int getAdler()
Returns the ADLER-32 value of the uncompressed data.

Returns:
the ADLER-32 value of the uncompressed data

getTotalIn sample code for java.util.zip.Deflater.getTotalIn() definition code for java.util.zip.Deflater.getTotalIn()

public int getTotalIn()
Returns the total number of uncompressed bytes input so far.

Since the number of bytes may be greater than Integer.MAX_VALUE, the getBytesRead() sample code for java.util.zip.Deflater.getBytesRead() definition code for java.util.zip.Deflater.getBytesRead() method is now the preferred means of obtaining this information.

Returns:
the total number of uncompressed bytes input so far

getBytesRead sample code for java.util.zip.Deflater.getBytesRead() definition code for java.util.zip.Deflater.getBytesRead()

public long getBytesRead()
Returns the total number of uncompressed bytes input so far.

Returns:
the total (non-negative) number of uncompressed bytes input so far

getTotalOut sample code for java.util.zip.Deflater.getTotalOut() definition code for java.util.zip.Deflater.getTotalOut()

public int getTotalOut()
Returns the total number of compressed bytes output so far.

Since the number of bytes may be greater than Integer.MAX_VALUE, the getBytesWritten() sample code for java.util.zip.Deflater.getBytesWritten() definition code for java.util.zip.Deflater.getBytesWritten() method is now the preferred means of obtaining this information.

Returns:
the total number of compressed bytes output so far

getBytesWritten sample code for java.util.zip.Deflater.getBytesWritten() definition code for java.util.zip.Deflater.getBytesWritten()

public long getBytesWritten()
Returns the total number of compressed bytes output so far.

Returns:
the total (non-negative) number of compressed bytes output so far

reset sample code for java.util.zip.Deflater.reset() definition code for java.util.zip.Deflater.reset()

public void reset()
Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.


end sample code for java.util.zip.Deflater.end() definition code for java.util.zip.Deflater.end()

public void end()
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.


finalize sample code for java.util.zip.Deflater.finalize() definition code for java.util.zip.Deflater.finalize()

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

Overrides:
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() in class Object sample code for java.lang.Object definition code for java.lang.Object