Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Fields](https://developer.android.com/reference/javax/crypto/CipherOutputStream#inhfields) \| [Ctors](https://developer.android.com/reference/javax/crypto/CipherOutputStream#pubctors) \| [Protected Ctors](https://developer.android.com/reference/javax/crypto/CipherOutputStream#proctors) \| [Methods](https://developer.android.com/reference/javax/crypto/CipherOutputStream#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/javax/crypto/CipherOutputStream#inhmethods)

# CipherOutputStream


`
public


class
CipherOutputStream
`


`


`

`


`

`

extends 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) ||
|   |   | ↳ | javax.crypto.CipherOutputStream |

<br />

*** ** * ** ***

A CipherOutputStream is composed of an OutputStream and a Cipher so
that write() methods first process the data before writing them out
to the underlying OutputStream. The cipher must be fully
initialized before being used by a CipherOutputStream.

For example, if the cipher is initialized for encryption, the
CipherOutputStream will attempt to encrypt data before writing out the
encrypted data.

This class adheres strictly to the semantics, especially the
failure semantics, of its ancestor classes
java.io.OutputStream and java.io.FilterOutputStream. This class
has exactly those methods specified in its ancestor classes, and
overrides them all. Moreover, this class catches all exceptions
that are not thrown by its ancestor classes.

It is crucial for a programmer using this class not to use
methods that are not defined or overriden in this class (such as a
new method or constructor that is later added to one of the super
classes), because the design and implementation of those methods
are unlikely to have considered security impact with regard to
CipherOutputStream.
**See also:**

- `https://developer.android.com/reference/java/io/OutputStream`
- `https://developer.android.com/reference/java/io/FilterOutputStream`
- `https://developer.android.com/reference/javax/crypto/Cipher`
- `https://developer.android.com/reference/javax/crypto/CipherInputStream`

## Summary

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

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#CipherOutputStream(java.io.OutputStream,%20javax.crypto.Cipher)(https://developer.android.com/reference/java/io/OutputStream os, https://developer.android.com/reference/javax/crypto/Cipher c) ` Constructs a CipherOutputStream from an OutputStream and a Cipher. |

| ### Protected constructors ||
|---|---|
| ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#CipherOutputStream(java.io.OutputStream)(https://developer.android.com/reference/java/io/OutputStream os) ` Constructs a CipherOutputStream from an OutputStream without specifying a Cipher. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#close()() ` Closes this output stream and releases any system resources associated with this stream. |
| ` void` | ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#flush()() ` Flushes this output stream by forcing any buffered output bytes that have already been processed by the encapsulated cipher object to be written out. |
| ` void` | ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#write(byte[])(byte[] b) ` Writes `b.length` bytes from the specified byte array to this output stream. |
| ` void` | ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#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` | ` https://developer.android.com/reference/javax/crypto/CipherOutputStream#write(int)(int b) ` Writes the specified byte to this output stream. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/io/FilterOutputStream ` |---|---| | ` void` | ` https://developer.android.com/reference/java/io/FilterOutputStream#close()() ` Closes this output stream and releases any system resources associated with the stream. | | ` void` | ` 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` | ` https://developer.android.com/reference/java/io/FilterOutputStream#write(byte[])(byte[] b) ` Writes `b.length` bytes to this output stream. | | ` void` | ` 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` | ` https://developer.android.com/reference/java/io/FilterOutputStream#write(int)(int b) ` Writes the specified `byte` to this output stream. | ||
| From class ` https://developer.android.com/reference/java/io/OutputStream ` |---|---| | ` void` | ` https://developer.android.com/reference/java/io/OutputStream#close()() ` Closes this output stream and releases any system resources associated with this stream. | | ` void` | ` https://developer.android.com/reference/java/io/OutputStream#flush()() ` Flushes this output stream and forces any buffered output bytes to be written out. | | ` static https://developer.android.com/reference/java/io/OutputStream` | ` https://developer.android.com/reference/java/io/OutputStream#nullOutputStream()() ` Returns a new `OutputStream` which discards all bytes. | | ` void` | ` 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` | ` 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` | ` https://developer.android.com/reference/java/io/OutputStream#write(int)(int b) ` Writes the specified byte to this output stream. | ||
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` 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 https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` 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` | ` 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` | ` 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 ` https://developer.android.com/reference/java/io/Closeable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/io/Closeable#close()() ` Closes this stream and releases any system resources associated with it. | ||
| From interface ` https://developer.android.com/reference/java/io/Flushable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/io/Flushable#flush()() ` Flushes this stream by writing any buffered output to the underlying stream. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Public constructors

### CipherOutputStream

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

```
public CipherOutputStream (OutputStream os, 
                Cipher c)
```

Constructs a CipherOutputStream from an OutputStream and a
Cipher.

Note: if the specified output stream or cipher is
null, a NullPointerException may be thrown later when
they are used.

<br />

| Parameters ||
|---|---|
| `os` | `OutputStream`: the OutputStream object <br /> |
| `c` | `Cipher`: an initialized Cipher object <br /> |

## Protected constructors

### CipherOutputStream

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

```
protected CipherOutputStream (OutputStream os)
```

Constructs a CipherOutputStream from an OutputStream without
specifying a Cipher. This has the effect of constructing a
CipherOutputStream using a NullCipher.

Note: if the specified output stream is null, a
NullPointerException may be thrown later when it is used.

<br />

| Parameters ||
|---|---|
| `os` | `OutputStream`: the OutputStream object <br /> |

## Public methods

### close

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

```
public void close ()
```

Closes this output stream and releases any system resources
associated with this stream.


This method invokes the `doFinal` method of the encapsulated
cipher object, which causes any bytes buffered by the encapsulated
cipher to be processed. The result is written out by calling the
`flush` method of this output stream.


This method resets the encapsulated cipher object to its initial state
and calls the `close` method of the underlying output
stream.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs. |

### flush

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

```
public void flush ()
```

Flushes this output stream by forcing any buffered output bytes
that have already been processed by the encapsulated cipher object
to be written out.

Any bytes buffered by the encapsulated cipher
and waiting to be processed by it will not be written out. For example,
if the encapsulated cipher is a block cipher, and the total number of
bytes written using one of the `write` methods is less than
the cipher's block size, no bytes will be written out.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs. |

### write

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

```
public void write (byte[] b)
```

Writes `b.length` bytes from the specified byte array
to this output stream.


The `write` method of
`CipherOutputStream` calls the `write`
method of three arguments with the three arguments
`b`, `0`, and `b.length`.

<br />

| Parameters ||
|---|---|
| `b` | `byte`: the data. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs. |
| `https://developer.android.com/reference/java/lang/NullPointerException` | if `b` is null. |

**See also:**

- `https://developer.android.com/reference/javax/crypto/CipherOutputStream#write(byte[],%20int,%20int)`

### 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 `len` bytes from the specified byte array
starting at offset `off` to this output stream.

<br />

| Parameters ||
|---|---|
| `b` | `byte`: the data. <br /> |
| `off` | `int`: the start offset in the data. <br /> |
| `len` | `int`: the number of bytes to write. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs. |

### write

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

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

Writes the specified byte to this output stream.

<br />

| Parameters ||
|---|---|
| `b` | `int`: the `byte`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs. |