public class

Deflater

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

Class Overview

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

It is usually more convenient to use DeflaterOutputStream.

To compress an in-memory byte[] to another in-memory byte[] manually:

     byte[] originalBytes = ...

     Deflater deflater = new Deflater();
     deflater.setInput(originalBytes);
     deflater.finish();

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buf = new byte[8192];
     while (!deflater.finished()) {
         int byteCount = deflater.deflate(buf);
         baos.write(buf, 0, byteCount);
     }
     deflater.end();

     byte[] compressedBytes = baos.toByteArray();
 

In situations where you don't have all the input in one array (or have so much input that you want to feed it to the deflater in chunks), it's possible to call setInput(byte[]) repeatedly, but you're much better off using DeflaterOutputStream to handle all this for you. DeflaterOutputStream also helps minimize memory requirements — the sample code above is very expensive.

Summary

Constants
int BEST_COMPRESSION Upper bound for the compression level range.
int BEST_SPEED Lower bound for compression level range.
int DEFAULT_COMPRESSION The default compression level.
int DEFAULT_STRATEGY The default compression strategy.
int DEFLATED The default compression method.
int FILTERED A compression strategy.
int HUFFMAN_ONLY A compression strategy.
int NO_COMPRESSION A compression level.
Public Constructors
Deflater()
Constructs a new Deflater instance using the default compression level.
Deflater(int level)
Constructs a new Deflater instance using compression level level.
Deflater(int level, boolean noHeader)
Constructs a new Deflater instance with a specific compression level.
Public Methods
synchronized int deflate(byte[] buf, int offset, int byteCount)
Deflates data (previously passed to setInput(byte[])) into a specific region within the supplied buffer.
int deflate(byte[] buf)
Deflates the data (previously passed to setInput(byte[])) into 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 true if all provided data has been successfully compressed.
synchronized int getAdler()
Returns the Adler32 checksum of the uncompressed data read so far.
synchronized long getBytesRead()
Returns the total number of bytes read by the Deflater.
synchronized long getBytesWritten()
Returns a the total number of bytes written by this Deflater.
synchronized int getTotalIn()
Returns the total number of bytes of input read by this Deflater.
synchronized int getTotalOut()
Returns the total number of bytes written to the output buffer by this Deflater.
synchronized boolean needsInput()
Returns true if setInput(byte[]) must be called before deflation can continue.
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 offset, int byteCount)
Sets the dictionary to be used for compression by this Deflater.
void setDictionary(byte[] dictionary)
Sets the dictionary to be used for compression by this Deflater.
synchronized void setInput(byte[] buf, int offset, int byteCount)
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()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int BEST_COMPRESSION

Since: API Level 1

Upper bound for the compression level range.

Constant Value: 9 (0x00000009)

public static final int BEST_SPEED

Since: API Level 1

Lower bound for compression level range.

Constant Value: 1 (0x00000001)

public static final int DEFAULT_COMPRESSION

Since: API Level 1

The default compression level.

Constant Value: -1 (0xffffffff)

public static final int DEFAULT_STRATEGY

Since: API Level 1

The default compression strategy.

Constant Value: 0 (0x00000000)

public static final int DEFLATED

Since: API Level 1

The default compression method.

Constant Value: 8 (0x00000008)

public static final int FILTERED

Since: API Level 1

A compression strategy.

Constant Value: 1 (0x00000001)

public static final int HUFFMAN_ONLY

Since: API Level 1

A compression strategy.

Constant Value: 2 (0x00000002)

public static final int NO_COMPRESSION

Since: API Level 1

A compression level.

Constant Value: 0 (0x00000000)

Public Constructors

public Deflater ()

Since: API Level 1

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

public Deflater (int level)

Since: API Level 1

Constructs a new Deflater instance using compression level level. The strategy can be specified with setStrategy(int). A header is added to the output by default; use Deflater(int, 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)

Since: API Level 1

Constructs a new Deflater instance with a specific compression level. If noHeader is 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 using setStrategy(int).

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 synchronized int deflate (byte[] buf, int offset, int byteCount)

Since: API Level 1

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

Returns
  • the number of bytes of compressed data written to buf.

public int deflate (byte[] buf)

Since: API Level 1

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

Returns
  • number of bytes of compressed data written to buf.

public synchronized void end ()

Since: API Level 1

Frees all resources held onto by this deflating algorithm. Any unused input or output is discarded. This method should be called explicitly in order to free native resources as soon as possible. After end() is called, other methods will typically throw IllegalStateException.

public synchronized void finish ()

Since: API Level 1

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

See Also

public synchronized boolean finished ()

Since: API Level 1

Returns true if all provided data has been successfully compressed.

public synchronized int getAdler ()

Since: API Level 1

Returns the Adler32 checksum of the uncompressed data read so far.

public synchronized long getBytesRead ()

Since: API Level 1

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

public synchronized long getBytesWritten ()

Since: API Level 1

Returns a the total number of bytes written by this Deflater. This method is the same as getTotalOut except it returns a long value instead of an integer.

public synchronized int getTotalIn ()

Since: API Level 1

Returns the total number of bytes of input read by this Deflater. This method is limited to 32 bits; use getBytesRead() instead.

public synchronized int getTotalOut ()

Since: API Level 1

Returns the total number of bytes written to the output buffer by this Deflater. The method is limited to 32 bits; use getBytesWritten() instead.

public synchronized boolean needsInput ()

Since: API Level 1

Returns true if setInput(byte[]) must be called before deflation can continue. If all uncompressed data has been provided to the Deflater, finish() must be called to ensure the compressed data is output.

public synchronized void reset ()

Since: API Level 1

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.

public synchronized void setDictionary (byte[] buf, int offset, int byteCount)

Since: API Level 1

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

public void setDictionary (byte[] dictionary)

Since: API Level 1

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

public synchronized void setInput (byte[] buf, int offset, int byteCount)

Since: API Level 1

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

public void setInput (byte[] buf)

Since: API Level 1

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

public synchronized void setLevel (int level)

Since: API Level 1

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(byte[]).

Throws
IllegalArgumentException If the compression level is invalid.

public synchronized void setStrategy (int strategy)

Since: API Level 1

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(byte[]).

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

Protected Methods

protected void finalize ()

Since: API Level 1

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected 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.