public class

Deflater

extends Object
java.lang.Object
   ↳ java.util.zip.Deflater

Class Overview

This class compresses data using the DEFLATE algorithm (see specification).

Basically this class is part of the API to the stream based ZLIB compression library and is used as such by DeflaterOutputStream and its descendants.

The typical usage of a Deflater instance outside this package consists of a specific call to one of its constructors before being passed to an instance of DeflaterOutputStream.

Summary

Constants
int BEST_COMPRESSION Upper bound for the compression level range.
int BEST_SPEED Lower bound for compression level range.
int DEFAULT_COMPRESSION Usage of the default compression level.
int DEFAULT_STRATEGY Default value for compression strategy.
int DEFLATED Default value for compression method.
int FILTERED Possible value for compression strategy.
int HUFFMAN_ONLY Possible value for compression strategy.
int NO_COMPRESSION Possible value for compression level.
Public Constructors
Deflater()
Constructs a new Deflater instance with default compression level.
Deflater(int level)
Constructs a new Deflater instance with a specific compression level.
Deflater(int level, boolean noHeader)
Constructs a new Deflater instance with a specific compression level.
Public Methods
int deflate(byte[] buf)
Deflates the data (previously passed to setInput) into the supplied buffer.
synchronized int deflate(byte[] buf, int off, int nbytes)
Deflates data (previously passed to setInput) into a specific region within the supplied buffer.
synchronized void end()
Frees all resources held onto by this deflating algorithm.
synchronized void finish()
Indicates to the Deflater that all uncompressed input has been provided to it.
synchronized boolean finished()
Returns whether or not all provided data has been successfully compressed.
synchronized int getAdler()
Returns the Adler32 checksum of uncompressed data currently read.
synchronized long getBytesRead()
Returns a long int of total number of bytes read by the Deflater.
synchronized long getBytesWritten()
Returns a long int of total number of bytes of read by the Deflater.
synchronized int getTotalIn()
Returns the total number of bytes of input consumed by the Deflater.
synchronized int getTotalOut()
Returns the total number of compressed bytes output by this Deflater.
synchronized boolean needsInput()
Counterpart to setInput().
synchronized void reset()
Resets the Deflater to accept new input without affecting any previously made settings for the compression strategy or level.
synchronized void setDictionary(byte[] buf, int off, int nbytes)
Sets the dictionary to be used for compression by this Deflater.
void setDictionary(byte[] buf)
Sets the dictionary to be used for compression by this Deflater.
synchronized void setInput(byte[] buf, int off, int nbytes)
Sets the input buffer the Deflater will use to extract uncompressed bytes for later compression.
void setInput(byte[] buf)
Sets the input buffer the Deflater will use to extract uncompressed bytes for later compression.
synchronized void setLevel(int level)
Sets the compression level to be used when compressing data.
synchronized void setStrategy(int strategy)
Sets the compression strategy to be used.
Protected Methods
void finalize()
Is called before the object's memory is being reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int BEST_COMPRESSION

Upper bound for the compression level range.

Constant Value: 9 (0x00000009)

public static final int BEST_SPEED

Lower bound for compression level range.

Constant Value: 1 (0x00000001)

public static final int DEFAULT_COMPRESSION

Usage of the default compression level.

Constant Value: -1 (0xffffffff)

public static final int DEFAULT_STRATEGY

Default value for compression strategy.

Constant Value: 0 (0x00000000)

public static final int DEFLATED

Default value for compression method.

Constant Value: 8 (0x00000008)

public static final int FILTERED

Possible value for compression strategy.

Constant Value: 1 (0x00000001)

public static final int HUFFMAN_ONLY

Possible value for compression strategy.

Constant Value: 2 (0x00000002)

public static final int NO_COMPRESSION

Possible value for compression level.

Constant Value: 0 (0x00000000)

Public Constructors

public Deflater ()

Constructs a new Deflater instance with default compression level. The strategy can be specified with setStrategy(int), only. A header is added to the output by default; use constructor Deflater(level, boolean) if you need to omit the header.

public Deflater (int level)

Constructs a new Deflater instance with a specific compression level. The strategy can be specified with setStrategy, only. A header is added to the output by default; use Deflater(level, boolean) if you need to omit the header.

Parameters
level the compression level in the range between 0 and 9.

public Deflater (int level, boolean noHeader)

Constructs a new Deflater instance with a specific compression level. If noHeader is passed as true no ZLib header is added to the output. In a ZIP archive every entry (compressed file) comes with such a header. The strategy can be specified with the setStrategy method, only.

Parameters
level the compression level in the range between 0 and 9.
noHeader true indicates that no ZLIB header should be written.

Public Methods

public int deflate (byte[] buf)

Deflates the data (previously passed to setInput) into the supplied buffer.

Parameters
buf buffer to write compressed data to.
Returns
  • number of bytes of compressed data written to buf.

public synchronized int deflate (byte[] buf, int off, int nbytes)

Deflates data (previously passed to setInput) into a specific region within the supplied buffer.

Parameters
buf the buffer to write compressed data to.
off the offset within buf at which to start writing to.
nbytes maximum number of bytes of compressed data to be written.
Returns
  • the number of bytes of compressed data written to buf.

public synchronized void end ()

Frees all resources held onto by this deflating algorithm. Any unused input or output is discarded. While this method is used by finalize(), it can be called explicitly in order to free native resources before the next GC cycle. After end() was called other methods will typically throw an IllegalStateException.

public synchronized void finish ()

Indicates to the Deflater that all uncompressed input has been provided to it.

See Also

public synchronized boolean finished ()

Returns whether or not all provided data has been successfully compressed.

Returns
  • true if all data has been compressed, false otherwise.

public synchronized int getAdler ()

Returns the Adler32 checksum of uncompressed data currently read. If a preset dictionary is used getAdler() will return the Adler32 checksum of the dictionary used.

Returns
  • the Adler32 checksum of uncompressed data or preset dictionary if used.

public synchronized long getBytesRead ()

Returns a long int of total number of bytes read by the Deflater. This method performs the same as getTotalIn except it returns a long value instead of an integer

Returns
  • total number of bytes read by Deflater.

public synchronized long getBytesWritten ()

Returns a long int of total number of bytes of read by the Deflater. This method performs the same as getTotalOut except it returns a long value instead of an integer

Returns
  • bytes exactly write by Deflater

public synchronized int getTotalIn ()

Returns the total number of bytes of input consumed by the Deflater.

Returns
  • number of bytes of input read.

public synchronized int getTotalOut ()

Returns the total number of compressed bytes output by this Deflater.

Returns
  • number of compressed bytes output.

public synchronized boolean needsInput ()

Counterpart to setInput(). Indicates whether or not all bytes of uncompressed input have been consumed by the Deflater. If needsInput() returns true setInput() must be called before deflation can continue. If all bytes of uncompressed data have been provided to the Deflater finish() must be called to ensure the compressed data is output.

Returns
  • true if input is required for deflation to continue, false otherwise.

public synchronized void reset ()

Resets the Deflater to accept new input without affecting any previously made settings for the compression strategy or level. This operation must be called after finished() returns true if the Deflater is to be reused.

See Also

public synchronized void setDictionary (byte[] buf, int off, int nbytes)

Sets the dictionary to be used for compression by this Deflater. setDictionary() can only be called if this Deflater supports the writing of ZLIB headers. This is the default behaviour but can be overridden using Deflater(int, boolean).

Parameters
buf the buffer containing the dictionary data bytes.
off the offset of the data.
nbytes the length of the data.

public void setDictionary (byte[] buf)

Sets the dictionary to be used for compression by this Deflater. setDictionary() can only be called if this Deflater supports the writing of ZLIB headers. This is the default behaviour but can be overridden using Deflater(int, boolean).

Parameters
buf the buffer containing the dictionary data bytes.

public synchronized void setInput (byte[] buf, int off, int nbytes)

Sets the input buffer the Deflater will use to extract uncompressed bytes for later compression. Input will be taken from the buffer region starting at off and ending at nbytes - 1.

Parameters
buf the buffer containing the input data bytes.
off the offset of the data.
nbytes the length of the data.

public void setInput (byte[] buf)

Sets the input buffer the Deflater will use to extract uncompressed bytes for later compression.

Parameters
buf the buffer.

public synchronized void setLevel (int level)

Sets the compression level to be used when compressing data. The compression level must be a value between 0 and 9. This value must be set prior to calling setInput().

Parameters
level compression level to use
Throws
IllegalArgumentException If the compression level is invalid.

public synchronized void setStrategy (int strategy)

Sets the compression strategy to be used. The strategy must be one of FILTERED, HUFFMAN_ONLY or DEFAULT_STRATEGY.This value must be set prior to calling setInput().

Parameters
strategy compression strategy to use
Throws
IllegalArgumentException If the strategy specified is not one of FILTERED, HUFFMAN_ONLY or DEFAULT_STRATEGY.

Protected Methods

protected void finalize ()

Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.