# DeflaterOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Fields](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#lfields)\|[Inherited Fields](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#inhfields)\|[Ctors](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#pubctors)\|[Methods](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#pubmethods)\|[Protected Methods](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#promethods)\|[Inherited Methods](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#inhmethods)  

# DeflaterOutputStream

`
public


class
DeflaterOutputStream
`  
`


``


``

extends `[FilterOutputStream](https://developer.android.com/reference/java/io/FilterOutputStream)`


``


`

|---|---|---|------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||||
| ↳ | [java.io.OutputStream](https://developer.android.com/reference/java/io/OutputStream) |||
|   | ↳ | [java.io.FilterOutputStream](https://developer.android.com/reference/java/io/FilterOutputStream) ||
|   |   | ↳ | java.util.zip.DeflaterOutputStream |

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known direct subclasses [GZIPOutputStream](https://developer.android.com/reference/java/util/zip/GZIPOutputStream),[ZipOutputStream](https://developer.android.com/reference/java/util/zip/ZipOutputStream) |--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | [GZIPOutputStream](https://developer.android.com/reference/java/util/zip/GZIPOutputStream) | This class implements a stream filter for writing compressed data in the GZIP file format. | | [ZipOutputStream](https://developer.android.com/reference/java/util/zip/ZipOutputStream)   | This class implements an output stream filter for writing files in the ZIP file format.    | |

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known indirect subclasses [JarOutputStream](https://developer.android.com/reference/java/util/jar/JarOutputStream) |------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------| | [JarOutputStream](https://developer.android.com/reference/java/util/jar/JarOutputStream) | The`JarOutputStream`class is used to write the contents of a JAR file to any output stream. | |

<br />

*** ** * ** ***

This class implements an output stream filter for compressing data in the "deflate" compression format. It is also used as the basis for other types of compression filters, such as GZIPOutputStream.  
**See also:**

- [Deflater](https://developer.android.com/reference/java/util/zip/Deflater)

## Summary

|                                                                                                        ### Fields                                                                                                         ||
|-----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|
| ` protected byte[]`                                                                     | [buf](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#buf) Output buffer for writing compressed data. |
| ` protected `[Deflater](https://developer.android.com/reference/java/util/zip/Deflater) | [def](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#def) Compressor for this stream.                |

|                                                                                                                                                                                                                                                                       ### Inherited fields                                                                                                                                                                                                                                                                       |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From class[java.io.FilterOutputStream](https://developer.android.com/reference/java/io/FilterOutputStream) |-------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------| | ` protected `[OutputStream](https://developer.android.com/reference/java/io/OutputStream) | [out](https://developer.android.com/reference/java/io/FilterOutputStream#out) The underlying output stream to be filtered. | |

|                                                                                                                                                                                                                                   ### Public constructors                                                                                                                                                                                                                                    ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out) ` Creates a new output stream with a default compressor and buffer size.                                                                                                                                                                            |
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20boolean))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out, boolean syncFlush) ` Creates a new output stream with a default compressor, a default buffer size and the specified flush mode.                                                                                                          |
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20java.util.zip.Deflater))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out, `[Deflater](https://developer.android.com/reference/java/util/zip/Deflater)` def) ` Creates a new output stream with the specified compressor and a default buffer size.                                                  |
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20java.util.zip.Deflater,%20boolean))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out, `[Deflater](https://developer.android.com/reference/java/util/zip/Deflater)` def, boolean syncFlush) ` Creates a new output stream with the specified compressor, flush mode and a default buffer size.        |
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20java.util.zip.Deflater,%20int))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out, `[Deflater](https://developer.android.com/reference/java/util/zip/Deflater)` def, int size) ` Creates a new output stream with the specified compressor and buffer size.                                           |
| ` `[DeflaterOutputStream](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20java.util.zip.Deflater,%20int,%20boolean))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out, `[Deflater](https://developer.android.com/reference/java/util/zip/Deflater)` def, int size, boolean syncFlush) ` Creates a new output stream with the specified compressor, buffer size and flush mode. |

|                                                                                                  ### Public methods                                                                                                  ||
|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[close](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#close())`() ` Writes remaining compressed data to the output stream and closes the underlying stream.                 |
| ` void` | ` `[finish](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#finish())`() ` Finishes writing compressed data to the output stream without closing the underlying stream.          |
| ` void` | ` `[flush](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#flush())`() ` Flushes the compressed output stream.                                                                   |
| ` void` | ` `[write](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#write(byte[],%20int,%20int))`(byte[] b, int off, int len) ` Writes an array of bytes to the compressed output stream. |
| ` void` | ` `[write](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#write(int))`(int b) ` Writes a byte to the compressed output stream.                                                  |

|                                                                            ### Protected methods                                                                            ||
|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[deflate](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#deflate())`() ` Writes next block of compressed data to the output stream. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.io.FilterOutputStream](https://developer.android.com/reference/java/io/FilterOutputStream)` ` |---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void` | ` `[close](https://developer.android.com/reference/java/io/FilterOutputStream#close())`() ` Closes this output stream and releases any system resources associated with the stream.                                                    | | ` void` | ` `[flush](https://developer.android.com/reference/java/io/FilterOutputStream#flush())`() ` Flushes this output stream and forces any buffered output bytes to be written out to the stream.                                           | | ` void` | ` `[write](https://developer.android.com/reference/java/io/FilterOutputStream#write(byte[]))`(byte[] b) ` Writes`b.length`bytes to this output stream.                                                                                 | | ` void` | ` `[write](https://developer.android.com/reference/java/io/FilterOutputStream#write(byte[],%20int,%20int))`(byte[] b, int off, int len) ` Writes`len`bytes from the specified`byte`array starting at offset`off`to this output stream. | | ` void` | ` `[write](https://developer.android.com/reference/java/io/FilterOutputStream#write(int))`(int b) ` Writes the specified`byte`to this output stream.                                                                                   | ||
| From class` `[java.io.OutputStream](https://developer.android.com/reference/java/io/OutputStream)` ` |----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void`                                                                                | ` `[close](https://developer.android.com/reference/java/io/OutputStream#close())`() ` Closes this output stream and releases any system resources associated with this stream.                                                   | | ` void`                                                                                | ` `[flush](https://developer.android.com/reference/java/io/OutputStream#flush())`() ` Flushes this output stream and forces any buffered output bytes to be written out.                                                         | | ` static `[OutputStream](https://developer.android.com/reference/java/io/OutputStream) | ` `[nullOutputStream](https://developer.android.com/reference/java/io/OutputStream#nullOutputStream())`() ` Returns a new`OutputStream`which discards all bytes.                                                                 | | ` void`                                                                                | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(byte[]))`(byte[] b) ` Writes`b.length`bytes from the specified byte array to this output stream.                                                   | | ` void`                                                                                | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(byte[],%20int,%20int))`(byte[] b, int off, int len) ` Writes`len`bytes from the specified byte array starting at offset`off`to this output stream. | | ` abstract void`                                                                       | ` `[write](https://developer.android.com/reference/java/io/OutputStream#write(int))`(int b) ` Writes the specified byte to this output stream.                                                                                   | ||
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[java.io.Closeable](https://developer.android.com/reference/java/io/Closeable)` ` |------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/io/Closeable#close())`() ` Closes this stream and releases any system resources associated with it. | ||
| From interface` `[java.io.Flushable](https://developer.android.com/reference/java/io/Flushable)` ` |------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[flush](https://developer.android.com/reference/java/io/Flushable#flush())`() ` Flushes this stream by writing any buffered output to the underlying stream. | ||
| From interface` `[java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable)` ` |------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[close](https://developer.android.com/reference/java/lang/AutoCloseable#close())`() ` Closes this resource, relinquishing any underlying resources. | ||

## Fields

### buf

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected byte[] buf
```

Output buffer for writing compressed data.

<br />

### def

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected Deflater def
```

Compressor for this stream.

<br />

## Public constructors

### DeflaterOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out)
```

Creates a new output stream with a default compressor and buffer size.

The new output stream instance is created as if by invoking the 2-argument constructor DeflaterOutputStream(out, false).

<br />

|                   Parameters                    ||
|-------|------------------------------------------|
| `out` | `OutputStream`: the output stream <br /> |

### DeflaterOutputStream

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out, 
                boolean syncFlush)
```

Creates a new output stream with a default compressor, a default buffer size and the specified flush mode.

<br />

|                                                                                                                                                                                    Parameters                                                                                                                                                                                    ||
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `out`       | `OutputStream`: the output stream <br />                                                                                                                                                                                                                                                                                                                            |
| `syncFlush` | `boolean`: if`true`the[flush()](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#flush())method of this instance flushes the compressor with flush mode[Deflater.SYNC_FLUSH](https://developer.android.com/reference/java/util/zip/Deflater#SYNC_FLUSH)before flushing the output stream, otherwise only flushes the output stream <br /> |

### DeflaterOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out, 
                Deflater def)
```

Creates a new output stream with the specified compressor and a default buffer size.

The new output stream instance is created as if by invoking the 3-argument constructor DeflaterOutputStream(out, def, false).

<br />

|                      Parameters                       ||
|-------|------------------------------------------------|
| `out` | `OutputStream`: the output stream <br />       |
| `def` | `Deflater`: the compressor ("deflater") <br /> |

### DeflaterOutputStream

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out, 
                Deflater def, 
                boolean syncFlush)
```

Creates a new output stream with the specified compressor, flush mode and a default buffer size.

<br />

|                                                                                                                                                                                    Parameters                                                                                                                                                                                    ||
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `out`       | `OutputStream`: the output stream <br />                                                                                                                                                                                                                                                                                                                            |
| `def`       | `Deflater`: the compressor ("deflater") <br />                                                                                                                                                                                                                                                                                                                      |
| `syncFlush` | `boolean`: if`true`the[flush()](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#flush())method of this instance flushes the compressor with flush mode[Deflater.SYNC_FLUSH](https://developer.android.com/reference/java/util/zip/Deflater#SYNC_FLUSH)before flushing the output stream, otherwise only flushes the output stream <br /> |

### DeflaterOutputStream

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out, 
                Deflater def, 
                int size)
```

Creates a new output stream with the specified compressor and buffer size.

The new output stream instance is created as if by invoking the 4-argument constructor DeflaterOutputStream(out, def, size, false).

<br />

|                       Parameters                       ||
|--------|------------------------------------------------|
| `out`  | `OutputStream`: the output stream <br />       |
| `def`  | `Deflater`: the compressor ("deflater") <br /> |
| `size` | `int`: the output buffer size <br />           |

|                                                        Throws                                                         ||
|--------------------------------------------------------------------------------------------------------|---------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if`size <= 0` |

### DeflaterOutputStream

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public DeflaterOutputStream (OutputStream out, 
                Deflater def, 
                int size, 
                boolean syncFlush)
```

Creates a new output stream with the specified compressor, buffer size and flush mode.

<br />

|                                                                                                                                                                                    Parameters                                                                                                                                                                                    ||
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `out`       | `OutputStream`: the output stream <br />                                                                                                                                                                                                                                                                                                                            |
| `def`       | `Deflater`: the compressor ("deflater") <br />                                                                                                                                                                                                                                                                                                                      |
| `size`      | `int`: the output buffer size <br />                                                                                                                                                                                                                                                                                                                                |
| `syncFlush` | `boolean`: if`true`the[flush()](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#flush())method of this instance flushes the compressor with flush mode[Deflater.SYNC_FLUSH](https://developer.android.com/reference/java/util/zip/Deflater#SYNC_FLUSH)before flushing the output stream, otherwise only flushes the output stream <br /> |

|                                                        Throws                                                         ||
|--------------------------------------------------------------------------------------------------------|---------------|
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if`size <= 0` |

## Public methods

### close

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void close ()
```

Writes remaining compressed data to the output stream and closes the underlying stream.

<br />

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |

### finish

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void finish ()
```

Finishes writing compressed data to the output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.

<br />

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |

### flush

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void flush ()
```

Flushes the compressed output stream. If[syncFlush](https://developer.android.com/reference/java/util/zip/DeflaterOutputStream#DeflaterOutputStream(java.io.OutputStream,%20java.util.zip.Deflater,%20int,%20boolean))is`true`when this compressed output stream is constructed, this method first flushes the underlying`compressor`with the flush mode[Deflater.SYNC_FLUSH](https://developer.android.com/reference/java/util/zip/Deflater#SYNC_FLUSH)to force all pending data to be flushed out to the output stream and then flushes the output stream. Otherwise this method only flushes the output stream without flushing the`compressor`.

<br />

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (byte[] b, 
                int off, 
                int len)
```

Writes an array of bytes to the compressed output stream. This method will block until all the bytes are written.

<br />

|                    Parameters                     ||
|-------|--------------------------------------------|
| `b`   | `byte`: the data to be written <br />      |
| `off` | `int`: the start offset of the data <br /> |
| `len` | `int`: the length of the data <br />       |

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |

### write

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void write (int b)
```

Writes a byte to the compressed output stream. This method will block until the byte can be written.

<br />

|                Parameters                 ||
|-----|--------------------------------------|
| `b` | `int`: the byte to be written <br /> |

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |

## Protected methods

### deflate

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected void deflate ()
```

Writes next block of compressed data to the output stream.

<br />

|                                                  Throws                                                  ||
|----------------------------------------------------------------------------|------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if an I/O error has occurred |