Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Deflater

public class Deflater
extends Object

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


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 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.

Compression levels

A compression level must be DEFAULT_COMPRESSION to compromise between speed and compression (currently equivalent to level 6), or between 0 (NO_COMPRESSION, where the input is simply copied) and 9 (BEST_COMPRESSION). Level 1 (BEST_SPEED) performs some compression, but with minimal speed overhead.

Summary

Constants

int BEST_COMPRESSION

This compression level gives the best compression, but takes the most time.

int BEST_SPEED

This compression level gives minimal compression, but takes the least time (of any level that actually performs compression; see NO_COMPRESSION).

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 FULL_FLUSH

Flush buffers so recipients can immediately decode the data sent thus far.

int HUFFMAN_ONLY

A compression strategy.

int NO_COMPRESSION

This compression level does no compression.

int NO_FLUSH

Use buffering for best compression.

int SYNC_FLUSH

Flush buffers so recipients can immediately decode the data sent thus far.

Public constructors

Deflater()

Constructs a new Deflater instance using the default compression level.

Deflater(int level)

Constructs a new Deflater instance with the given compression level.

Deflater(int level, boolean noHeader)

Constructs a new Deflater instance with the given compression level.

Public methods

int deflate(byte[] buf, int offset, int byteCount, int flush)

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

int deflate(byte[] buf, int offset, int byteCount)

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

int deflate(byte[] buf)

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

void end()

Frees all resources held onto by this deflating algorithm.

void finish()

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

boolean finished()

Returns true if finish has been called and all data provided by setInput has been successfully compressed and consumed by deflate.

int getAdler()

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

long getBytesRead()

Returns the total number of bytes read by the Deflater.

long getBytesWritten()

Returns a the total number of bytes written by this Deflater.

int getTotalIn()

Returns the total number of bytes of input read by this Deflater.

int getTotalOut()

Returns the total number of bytes written to the output buffer by this Deflater.

boolean needsInput()

Returns true if setInput must be called before deflation can continue.

void reset()

Resets the Deflater to accept new input without affecting any previous compression strategy or level settings.

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.

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.

void setLevel(int level)

Sets the given compression level to be used when compressing data.

void setStrategy(int strategy)

Sets the compression strategy to be used.

Protected methods

void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.

Inherited methods

From class java.lang.Object

Constants

BEST_COMPRESSION

Added in API level 1
int BEST_COMPRESSION

This compression level gives the best compression, but takes the most time.

Constant Value: 9 (0x00000009)

BEST_SPEED

Added in API level 1
int BEST_SPEED

This compression level gives minimal compression, but takes the least time (of any level that actually performs compression; see NO_COMPRESSION).

Constant Value: 1 (0x00000001)

DEFAULT_COMPRESSION

Added in API level 1
int DEFAULT_COMPRESSION

The default compression level. This is a trade-off between speed and compression, currently equivalent to level 6.

Constant Value: -1 (0xffffffff)

DEFAULT_STRATEGY

Added in API level 1
int DEFAULT_STRATEGY

The default compression strategy.

Constant Value: 0 (0x00000000)

DEFLATED

Added in API level 1
int DEFLATED

The default compression method.

Constant Value: 8 (0x00000008)

FILTERED

Added in API level 1
int FILTERED

A compression strategy.

Constant Value: 1 (0x00000001)

FULL_FLUSH

Added in API level 19
int FULL_FLUSH

Flush buffers so recipients can immediately decode the data sent thus far. The compression state is also reset to permit random access and recovery for clients who have discarded or damaged their own copy. This mode may degrade compression.

Constant Value: 3 (0x00000003)

HUFFMAN_ONLY

Added in API level 1
int HUFFMAN_ONLY

A compression strategy.

Constant Value: 2 (0x00000002)

NO_COMPRESSION

Added in API level 1
int NO_COMPRESSION

This compression level does no compression. It is even faster than BEST_SPEED.

Constant Value: 0 (0x00000000)

NO_FLUSH

Added in API level 19
int NO_FLUSH

Use buffering for best compression.

Constant Value: 0 (0x00000000)

SYNC_FLUSH

Added in API level 19
int SYNC_FLUSH

Flush buffers so recipients can immediately decode the data sent thus far. This mode may degrade compression.

Constant Value: 2 (0x00000002)

Public constructors

Deflater

Added in API level 1
Deflater ()

Constructs a new Deflater instance using the default compression level. The compression 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.

Deflater

Added in API level 1
Deflater (int level)

Constructs a new Deflater instance with the given compression level. The compression 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 int

Deflater

Added in API level 1
Deflater (int level, 
                boolean noHeader)

Constructs a new Deflater instance with the given compression level. If noHeader is true, no ZLIB header is added to the output. In a zip file, every entry (compressed file) comes with such a header. The strategy can be specified using setStrategy(int).

Parameters
level int
noHeader boolean

Public methods

deflate

Added in API level 19
int deflate (byte[] buf, 
                int offset, 
                int byteCount, 
                int flush)

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

Parameters
buf byte
offset int
byteCount int
flush int: one of NO_FLUSH, SYNC_FLUSH or FULL_FLUSH.
Returns
int the number of compressed bytes written to buf. If this equals byteCount, the number of bytes of input to be flushed may have exceeded the output buffer's capacity. In this case, finishing a flush will require the output buffer to be drained and additional calls to deflate(byte[]) to be made.
Throws
IllegalArgumentException if flush is invalid.

deflate

Added in API level 1
int deflate (byte[] buf, 
                int offset, 
                int byteCount)

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

Parameters
buf byte
offset int
byteCount int
Returns
int the number of bytes of compressed data written to buf.

deflate

Added in API level 1
int deflate (byte[] buf)

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

Parameters
buf byte
Returns
int number of bytes of compressed data written to buf.

end

Added in API level 1
void end ()

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.

finish

Added in API level 1
void finish ()

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

See also:

finished

Added in API level 1
boolean finished ()

Returns true if finish has been called and all data provided by setInput has been successfully compressed and consumed by deflate.

Returns
boolean

getAdler

Added in API level 1
int getAdler ()

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

Returns
int

getBytesRead

Added in API level 1
long getBytesRead ()

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.

Returns
long

getBytesWritten

Added in API level 1
long getBytesWritten ()

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.

Returns
long

getTotalIn

Added in API level 1
int getTotalIn ()

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

Returns
int

getTotalOut

Added in API level 1
int getTotalOut ()

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

Returns
int

needsInput

Added in API level 1
boolean needsInput ()

Returns true if setInput 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.

Returns
boolean

reset

Added in API level 1
void reset ()

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

setDictionary

Added in API level 1
void setDictionary (byte[] buf, 
                int offset, 
                int byteCount)

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).

Parameters
buf byte
offset int
byteCount int

setDictionary

Added in API level 1
void setDictionary (byte[] dictionary)

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).

Parameters
dictionary byte

setInput

Added in API level 1
void setInput (byte[] buf, 
                int offset, 
                int byteCount)

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

Parameters
buf byte
offset int
byteCount int

setInput

Added in API level 1
void setInput (byte[] buf)

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

Parameters
buf byte

setLevel

Added in API level 1
void setLevel (int level)

Sets the given compression level to be used when compressing data. This value must be set prior to calling setInput.

Parameters
level int
Throws
IllegalArgumentException If the compression level is invalid.

setStrategy

Added in API level 1
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 int
Throws
IllegalArgumentException If the strategy specified is not one of FILTERED, HUFFMAN_ONLY or DEFAULT_STRATEGY.

Protected methods

finalize

Added in API level 1
void finalize ()

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.