Save This Page
Home » openjdk-7 » java » util » zip » [javadoc | source]
java.util.zip
public class: Deflater [javadoc | source]
java.lang.Object
   java.util.zip.Deflater

Direct Known Subclasses:
    DeflatingCompressor

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\u20AC\u20AC";
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
}
Field Summary
public static final  int DEFLATED    Compression method for the deflate algorithm (the only one currently supported). 
public static final  int NO_COMPRESSION    Compression level for no compression. 
public static final  int BEST_SPEED    Compression level for fastest compression. 
public static final  int BEST_COMPRESSION    Compression level for best compression. 
public static final  int DEFAULT_COMPRESSION    Default compression level. 
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. 
public static final  int HUFFMAN_ONLY    Compression strategy for Huffman coding only. 
public static final  int DEFAULT_STRATEGY    Default compression strategy. 
Constructor:
 public Deflater() 
 public Deflater(int level) 
 public Deflater(int level,
    boolean nowrap) 
Method from java.util.zip.Deflater Summary:
deflate,   deflate,   end,   finalize,   finish,   finished,   getAdler,   getBytesRead,   getBytesWritten,   getTotalIn,   getTotalOut,   needsInput,   reset,   setDictionary,   setDictionary,   setInput,   setInput,   setLevel,   setStrategy
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.util.zip.Deflater Detail:
 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.
 public synchronized 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.
 public synchronized  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.
 protected  void finalize() 
    Closes the compressor when garbage is collected.
 public synchronized  void finish() 
    When called, indicates that compression should end with the current contents of the input buffer.
 public synchronized boolean finished() 
    Returns true if the end of the compressed data output stream has been reached.
 public synchronized int getAdler() 
    Returns the ADLER-32 value of the uncompressed data.
 public synchronized long getBytesRead() 
    Returns the total number of uncompressed bytes input so far.

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

 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() method is now the preferred means of obtaining this information.

 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() method is now the preferred means of obtaining this information.

 public boolean needsInput() 
    Returns true if the input data buffer is empty and setInput() should be called in order to provide more input.
 public synchronized  void reset() 
    Resets deflater so that a new set of input data can be processed. Keeps current compression level and strategy settings.
 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.
 public synchronized  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.
 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.
 public synchronized  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.
 public synchronized  void setLevel(int level) 
    Sets the current compression level to the specified value.
 public synchronized  void setStrategy(int strategy) 
    Sets the compression strategy to the specified value.