Google is committed to advancing racial equity for Black communities. See how.
Added in API level 1

Deflater

open class Deflater
kotlin.Any
   ↳ java.util.zip.Deflater

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";
      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);
      compresser.end();
 
      // 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
  }
  

Summary

Constants

static Int

Compression level for best compression.

static Int

Compression level for fastest compression.

static Int

Default compression level.

static Int

Default compression strategy.

static Int

Compression method for the deflate algorithm (the only one currently supported).

static Int

Compression strategy best used for data consisting mostly of small values with a somewhat random distribution.

static Int

Compression flush mode used to flush out all pending output and reset the deflater.

static Int

Compression strategy for Huffman coding only.

static Int

Compression level for no compression.

static Int

Compression flush mode used to achieve best compression result.

static Int

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Public constructors

<init>(level: Int, nowrap: Boolean)

Creates a new compressor using the specified compression level.

<init>(level: Int)

Creates a new compressor using the specified compression level.

Creates a new compressor with the default compression level.

Public methods

open Int
deflate(b: ByteArray!, off: Int, len: Int)

Compresses the input data and fills specified buffer with compressed data.

open Int

Compresses the input data and fills specified buffer with compressed data.

open Int
deflate(b: ByteArray!, off: Int, len: Int, flush: Int)

Compresses the input data and fills the specified buffer with compressed data.

open Unit
end()

Closes the compressor and discards any unprocessed input.

open Unit

When called, indicates that compression should end with the current contents of the input buffer.

open Boolean

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

open Int

Returns the ADLER-32 value of the uncompressed data.

open Long

Returns the total number of uncompressed bytes input so far.

open Long

Returns the total number of compressed bytes output so far.

open Int

Returns the total number of uncompressed bytes input so far.

open Int

Returns the total number of compressed bytes output so far.

open Boolean

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

open Unit

Resets deflater so that a new set of input data can be processed.

open Unit
setDictionary(b: ByteArray!, off: Int, len: Int)

Sets preset dictionary for compression.

open Unit

Sets preset dictionary for compression.

open Unit
setInput(b: ByteArray!, off: Int, len: Int)

Sets input data for compression.

open Unit

Sets input data for compression.

open Unit
setLevel(level: Int)

Sets the compression level to the specified value.

open Unit
setStrategy(strategy: Int)

Sets the compression strategy to the specified value.

Protected methods

open Unit

Closes the compressor when garbage is collected.

Constants

BEST_COMPRESSION

Added in API level 1
static val BEST_COMPRESSION: Int

Compression level for best compression.

Value: 9

BEST_SPEED

Added in API level 1
static val BEST_SPEED: Int

Compression level for fastest compression.

Value: 1

DEFAULT_COMPRESSION

Added in API level 1
static val DEFAULT_COMPRESSION: Int

Default compression level.

Value: -1

DEFAULT_STRATEGY

Added in API level 1
static val DEFAULT_STRATEGY: Int

Default compression strategy.

Value: 0

DEFLATED

Added in API level 1
static val DEFLATED: Int

Compression method for the deflate algorithm (the only one currently supported).

Value: 8

FILTERED

Added in API level 1
static val FILTERED: Int

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

Value: 1

FULL_FLUSH

Added in API level 19
static val FULL_FLUSH: Int

Compression flush mode used to flush out all pending output and reset the deflater. Using this mode too often can seriously degrade compression.

Value: 3

HUFFMAN_ONLY

Added in API level 1
static val HUFFMAN_ONLY: Int

Compression strategy for Huffman coding only.

Value: 2

NO_COMPRESSION

Added in API level 1
static val NO_COMPRESSION: Int

Compression level for no compression.

Value: 0

NO_FLUSH

Added in API level 19
static val NO_FLUSH: Int

Compression flush mode used to achieve best compression result.

Value: 0

SYNC_FLUSH

Added in API level 19
static val SYNC_FLUSH: Int

Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms.

Value: 2

Public constructors

<init>

Added in API level 1
Deflater(
    level: Int,
    nowrap: Boolean)

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 Int: the compression level (0-9)
nowrap Boolean: if true then use GZIP compatible compression

<init>

Added in API level 1
Deflater(level: Int)

Creates a new compressor using the specified compression level. Compressed data will be generated in ZLIB format.

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

<init>

Added in API level 1
Deflater()

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

Public methods

deflate

Added in API level 1
open fun deflate(
    b: ByteArray!,
    off: Int,
    len: Int
): Int

Compresses the input data and 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.

This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b, off, len) yields the same result as the invocation of deflater.deflate(b, off, len, Deflater.NO_FLUSH).

Parameters
b ByteArray!: the buffer for the compressed data
off Int: the start offset of the data
len Int: the maximum number of bytes of compressed data
Return
Int the actual number of bytes of compressed data written to the output buffer

deflate

Added in API level 1
open fun deflate(b: ByteArray!): Int

Compresses the input data and 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.

This method uses NO_FLUSH as its compression flush mode. An invocation of this method of the form deflater.deflate(b) yields the same result as the invocation of deflater.deflate(b, 0, b.length, Deflater.NO_FLUSH).

Parameters
b ByteArray!: the buffer for the compressed data
Return
Int the actual number of bytes of compressed data written to the output buffer

deflate

Added in API level 19
open fun deflate(
    b: ByteArray!,
    off: Int,
    len: Int,
    flush: Int
): Int

Compresses the input data and fills the specified buffer with compressed data. Returns actual number of bytes of data compressed.

Compression flush mode is one of the following three modes:

  • NO_FLUSH: allows the deflater to decide how much data to accumulate, before producing output, in order to achieve the best compression (should be used in normal use scenario). A return value of 0 in this flush mode indicates that needsInput() should be called in order to determine if more input data is required.
  • SYNC_FLUSH: all pending output in the deflater is flushed, to the specified output buffer, so that an inflater that works on compressed data can get all input data available so far (In particular the needsInput() returns true after this invocation if enough output space is provided). Flushing with SYNC_FLUSH may degrade compression for some compression algorithms and so it should be used only when necessary.
  • FULL_FLUSH: all pending output is flushed out as with SYNC_FLUSH. The compression state is reset so that the inflater that works on the compressed output data can restart from this point if previous compressed data has been damaged or if random access is desired. Using FULL_FLUSH too often can seriously degrade compression.

In the case of FULL_FLUSH or SYNC_FLUSH, if the return value is len, the space available in output buffer b, this method should be invoked again with the same flush parameter and more output space.

Parameters
b ByteArray!: the buffer for the compressed data
off Int: the start offset of the data
len Int: the maximum number of bytes of compressed data
flush Int: the compression flush mode
Return
Int the actual number of bytes of compressed data written to the output buffer
Exceptions
java.lang.IllegalArgumentException if the flush mode is invalid

end

Added in API level 1
open fun end(): Unit

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.

finish

Added in API level 1
open fun finish(): Unit

When called, indicates that compression should end with the current contents of the input buffer.

finished

Added in API level 1
open fun finished(): Boolean

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

Return
Boolean true if the end of the compressed data output stream has been reached

getAdler

Added in API level 1
open fun getAdler(): Int

Returns the ADLER-32 value of the uncompressed data.

Return
Int the ADLER-32 value of the uncompressed data

getBytesRead

Added in API level 1
open fun getBytesRead(): Long

Returns the total number of uncompressed bytes input so far.

Return
Long the total (non-negative) number of uncompressed bytes input so far

getBytesWritten

Added in API level 1
open fun getBytesWritten(): Long

Returns the total number of compressed bytes output so far.

Return
Long the total (non-negative) number of compressed bytes output so far

getTotalIn

Added in API level 1
open fun getTotalIn(): Int

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.

Return
Int the total number of uncompressed bytes input so far

getTotalOut

Added in API level 1
open fun getTotalOut(): Int

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.

Return
Int the total number of compressed bytes output so far

needsInput

Added in API level 1
open fun needsInput(): Boolean

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

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

reset

Added in API level 1
open fun reset(): Unit

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

setDictionary

Added in API level 1
open fun setDictionary(
    b: ByteArray!,
    off: Int,
    len: Int
): Unit

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 ByteArray!: the dictionary data bytes
off Int: the start offset of the data
len Int: the length of the data

setDictionary

Added in API level 1
open fun setDictionary(b: ByteArray!): Unit

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 ByteArray!: the dictionary data bytes

setInput

Added in API level 1
open fun setInput(
    b: ByteArray!,
    off: Int,
    len: Int
): Unit

Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
b ByteArray!: the input data bytes
off Int: the start offset of the data
len Int: the length of the data

setInput

Added in API level 1
open fun setInput(b: ByteArray!): Unit

Sets input data for compression. This should be called whenever needsInput() returns true indicating that more input data is required.

Parameters
b ByteArray!: the input data bytes

setLevel

Added in API level 1
open fun setLevel(level: Int): Unit

Sets the compression level to the specified value.

If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation.

Parameters
level Int: the new compression level (0-9)
Exceptions
java.lang.IllegalArgumentException if the compression level is invalid

setStrategy

Added in API level 1
open fun setStrategy(strategy: Int): Unit

Sets the compression strategy to the specified value.

If the compression strategy is changed, the next invocation of deflate will compress the input available so far with the old strategy (and may be flushed); the new strategy will take effect only after that invocation.

Parameters
strategy Int: the new compression strategy
Exceptions
java.lang.IllegalArgumentException if the compression strategy is invalid

Protected methods

finalize

Added in API level 1
protected open fun finalize(): Unit

Closes the compressor when garbage is collected.

Exceptions
java.lang.Throwable the Exception raised by this method