# Deflater

public class **Deflater** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
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](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/package-summary#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
>  }
>  
> ```

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Inflater.html`

### Constant Summary

|---|---|---|
| int | [BEST_COMPRESSION](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#BEST_COMPRESSION) | Compression level for best compression. |
| int | [BEST_SPEED](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#BEST_SPEED) | Compression level for fastest compression. |
| int | [DEFAULT_COMPRESSION](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#DEFAULT_COMPRESSION) | Default compression level. |
| int | [DEFAULT_STRATEGY](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#DEFAULT_STRATEGY) | Default compression strategy. |
| int | [DEFLATED](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#DEFLATED) | Compression method for the deflate algorithm (the only one currently supported). |
| int | [FILTERED](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#FILTERED) | Compression strategy best used for data consisting mostly of small values with a somewhat random distribution. |
| int | [FULL_FLUSH](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#FULL_FLUSH) | Compression flush mode used to flush out all pending output and reset the deflater. |
| int | [HUFFMAN_ONLY](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#HUFFMAN_ONLY) | Compression strategy for Huffman coding only. |
| int | [NO_COMPRESSION](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#NO_COMPRESSION) | Compression level for no compression. |
| int | [NO_FLUSH](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#NO_FLUSH) | Compression flush mode used to achieve best compression result. |
| int | [SYNC_FLUSH](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#SYNC_FLUSH) | Compression flush mode used to flush out all pending output; may degrade compression for some compression algorithms. |

### Public Constructor Summary

|---|---|
|   | [Deflater](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#Deflater(int,%20boolean))(int level, boolean nowrap) Creates a new compressor using the specified compression level. |
|   | [Deflater](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#Deflater(int))(int level) Creates a new compressor using the specified compression level. |
|   | [Deflater](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#Deflater())() Creates a new compressor with the default compression level. |

### Public Method Summary

|---|---|
| int | [deflate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int))(byte\[\] b, int off, int len) Compresses the input data and fills specified buffer with compressed data. |
| int | [deflate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[]))(byte\[\] b) Compresses the input data and fills specified buffer with compressed data. |
| int | [deflate](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int))(byte\[\] b, int off, int len, int flush) Compresses the input data and fills the specified buffer with compressed data. |
| void | [end](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#end())() Closes the compressor and discards any unprocessed input. |
| void | [finish](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#finish())() When called, indicates that compression should end with the current contents of the input buffer. |
| boolean | [finished](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#finished())() Returns true if the end of the compressed data output stream has been reached. |
| int | [getAdler](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getAdler())() Returns the ADLER-32 value of the uncompressed data. |
| long | [getBytesRead](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getBytesRead())() Returns the total number of uncompressed bytes input so far. |
| long | [getBytesWritten](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getBytesWritten())() Returns the total number of compressed bytes output so far. |
| int | [getTotalIn](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getTotalIn())() Returns the total number of uncompressed bytes input so far. |
| int | [getTotalOut](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getTotalOut())() Returns the total number of compressed bytes output so far. |
| boolean | [needsInput](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput())() Returns true if the input data buffer is empty and setInput() should be called in order to provide more input. |
| void | [reset](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#reset())() Resets deflater so that a new set of input data can be processed. |
| void | [setDictionary](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setDictionary(byte[],%20int,%20int))(byte\[\] b, int off, int len) Sets preset dictionary for compression. |
| void | [setDictionary](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setDictionary(byte[]))(byte\[\] b) Sets preset dictionary for compression. |
| void | [setInput](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setInput(byte[],%20int,%20int))(byte\[\] b, int off, int len) Sets input data for compression. |
| void | [setInput](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setInput(byte[]))(byte\[\] b) Sets input data for compression. |
| void | [setLevel](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setLevel(int))(int level) Sets the compression level to the specified value. |
| void | [setStrategy](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#setStrategy(int))(int strategy) Sets the compression strategy to the specified value. |

### Protected Method Summary

|---|---|
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#finalize())() Closes the compressor when garbage is collected. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Constants

#### public static final int
**BEST_COMPRESSION**

Compression level for best compression.
Constant Value: 9

#### public static final int
**BEST_SPEED**

Compression level for fastest compression.
Constant Value: 1

#### public static final int
**DEFAULT_COMPRESSION**

Default compression level.
Constant Value: -1

#### public static final int
**DEFAULT_STRATEGY**

Default compression strategy.
Constant Value: 0

#### public static final int
**DEFLATED**

Compression method for the deflate algorithm (the only one currently
supported).
Constant Value: 8

#### 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.
Constant Value: 1

#### public static final int
**FULL_FLUSH**

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

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int)`
Constant Value: 3

#### public static final int
**HUFFMAN_ONLY**

Compression strategy for Huffman coding only.
Constant Value: 2

#### public static final int
**NO_COMPRESSION**

Compression level for no compression.
Constant Value: 0

#### public static final int
**NO_FLUSH**

Compression flush mode used to achieve best compression result.

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int)`
Constant Value: 0

#### public static final int
**SYNC_FLUSH**

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

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#deflate(byte[],%20int,%20int,%20int)`
Constant Value: 2

## Public Constructors

#### public
**Deflater**
(int level, boolean nowrap)

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

#### public
**Deflater**
(int level)

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

##### Parameters

| level | the compression level (0-9) |
|---|---|

#### public
**Deflater**
()

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

## Public Methods

#### public int
**deflate**
(byte\[\] b, int off, int len)

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()` should be called
in order to determine if more input data is required.

This method uses `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 | the buffer for the compressed data |
| off | the start offset of the data |
| len | the maximum number of bytes of compressed data |
|---|---|

##### Returns

- the actual number of bytes of compressed data written to the output buffer

#### public int
**deflate**
(byte\[\] b)

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()` should be called
in order to determine if more input data is required.

This method uses `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 | the buffer for the compressed data |
|---|---|

##### Returns

- the actual number of bytes of compressed data written to the output buffer

#### public int
**deflate**
(byte\[\] b, int off, int len, int flush)

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:

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()` should be called in order to determine if more input data is required.
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()` returns `true` after this invocation if enough output space is provided). Flushing with `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#SYNC_FLUSH` may degrade compression for some compression algorithms and so it should be used only when necessary.
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#FULL_FLUSH`: all pending output is flushed out as with `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#FULL_FLUSH` too often can seriously degrade compression.

In the case of `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#FULL_FLUSH` or `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#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 | the buffer for the compressed data |
| off | the start offset of the data |
| len | the maximum number of bytes of compressed data |
| flush | the compression flush mode |
|---|---|

##### Returns

- the actual number of bytes of compressed data written to the output buffer

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the flush mode is invalid |
|---|---|

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

#### public void
**finish**
()

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

#### public boolean
**finished**
()

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

##### Returns

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

#### public int
**getAdler**
()

Returns the ADLER-32 value of the uncompressed data.

##### Returns

- the ADLER-32 value of the uncompressed data

#### public long
**getBytesRead**
()

Returns the total number of uncompressed bytes input so far.

##### Returns

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

#### public long
**getBytesWritten**
()

Returns the total number of compressed bytes output so far.

##### Returns

- the total (non-negative) 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getBytesRead()` method is now
the preferred means of obtaining this information.

<br />

##### Returns

- the total number of uncompressed bytes input so far

#### 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#getBytesWritten()` method is now
the preferred means of obtaining this information.

<br />

##### Returns

- the total number of compressed bytes output so far

#### public boolean
**needsInput**
()

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

##### Returns

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

#### public 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, 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.

##### Parameters

| b | the dictionary data bytes |
| off | the start offset of the data |
| len | the length of the data |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Inflater.html#inflate(byte[])`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Inflater.html#getAdler()`

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

##### Parameters

| b | the dictionary data bytes |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Inflater.html#inflate(byte[])`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Inflater.html#getAdler()`

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

##### Parameters

| b | the input data bytes |
| off | the start offset of the data |
| len | the length of the data |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()`

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

##### Parameters

| b | the input data bytes |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/zip/Deflater.html#needsInput()`

#### public void
**setLevel**
(int level)

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 | the new compression level (0-9) |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the compression level is invalid |
|---|---|

#### public void
**setStrategy**
(int strategy)

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 | the new compression strategy |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the compression strategy is invalid |
|---|---|

## Protected Methods

#### protected void
**finalize**
()

Closes the compressor when garbage is collected.