# AesCipherDataSink

Artifact: [androidx.media3:media3-datasource](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/datasource/AesCipherDataSink.java+class:androidx.media3.datasource.AesCipherDataSink)

*** ** * ** ***


```
@UnstableApi
public final class AesCipherDataSink implements DataSink
```

<br />

*** ** * ** ***

A wrapping `https://developer.android.com/reference/androidx/media3/datasource/DataSink` that encrypts the data being consumed.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/AesCipherDataSink#AesCipherDataSink(byte[],androidx.media3.datasource.DataSink)(byte[] secretKey, https://developer.android.com/reference/androidx/media3/datasource/DataSink wrappedDataSink)` Create an instance whose `write` methods have the side effect of overwriting the input `data`. |
| `https://developer.android.com/reference/androidx/media3/datasource/AesCipherDataSink#AesCipherDataSink(byte[],androidx.media3.datasource.DataSink,byte[])( byte[] secretKey, https://developer.android.com/reference/androidx/media3/datasource/DataSink wrappedDataSink, @https://developer.android.com/reference/androidx/annotation/Nullable byte[] scratch )` Create an instance whose `write` methods are free of side effects. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/AesCipherDataSink#close()()` Closes the sink. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/AesCipherDataSink#open(androidx.media3.datasource.DataSpec)(https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec)` Opens the sink to consume the specified data. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/AesCipherDataSink#write(byte[],int,int)(byte[] buffer, int offset, int length)` Consumes the provided data. |

## Public constructors

### AesCipherDataSink

```
public AesCipherDataSink(byte[] secretKey, DataSink wrappedDataSink)
```

Create an instance whose `write` methods have the side effect of overwriting the input `data`. Use this constructor for maximum efficiency in the case that there is no requirement for the input data arrays to remain unchanged.

| Parameters |
|---|---|
| `byte[] secretKey` | The key data. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSink wrappedDataSink` | The wrapped `https://developer.android.com/reference/androidx/media3/datasource/DataSink`. |

### AesCipherDataSink

```
public AesCipherDataSink(
    byte[] secretKey,
    DataSink wrappedDataSink,
    @Nullable byte[] scratch
)
```

Create an instance whose `write` methods are free of side effects. Use this constructor when the input data arrays are required to remain unchanged.

| Parameters |
|---|---|
| `byte[] secretKey` | The key data. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSink wrappedDataSink` | The wrapped `https://developer.android.com/reference/androidx/media3/datasource/DataSink`. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable byte[] scratch` | Scratch space. Data is encrypted into this array before being written to the wrapped `https://developer.android.com/reference/androidx/media3/datasource/DataSink`. It should be of appropriate size for the expected writes. If a write is larger than the size of this array the write will still succeed, but multiple cipher calls will be required to complete the operation. If `null` then encryption will overwrite the input `data`. |

## Public methods

### close

```
public void close()
```

Closes the sink.

Note: This method must be called even if the corresponding call to `https://developer.android.com/reference/androidx/media3/datasource/DataSink#open(androidx.media3.datasource.DataSpec)` threw an `https://developer.android.com/reference/java/io/IOException.html`. See `https://developer.android.com/reference/androidx/media3/datasource/DataSink#open(androidx.media3.datasource.DataSpec)` for more details.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs closing the sink. |

### open

```
public void open(DataSpec dataSpec)
```

Opens the sink to consume the specified data.

Note: If an `https://developer.android.com/reference/java/io/IOException.html` is thrown, callers must still call `https://developer.android.com/reference/androidx/media3/datasource/DataSink#close()` to ensure that any partial effects of the invocation are cleaned up.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Defines the data to be consumed. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs opening the sink. |

### write

```
public void write(byte[] buffer, int offset, int length)
```

Consumes the provided data.

| Parameters |
|---|---|
| `byte[] buffer` | The buffer from which data should be consumed. |
| `int offset` | The offset of the data to consume in `buffer`. |
| `int length` | The length of the data to consume, in bytes. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs writing to the sink. |