# ByteArrayDataSink

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/ByteArrayDataSink.java+class:androidx.media3.datasource.ByteArrayDataSink)

*** ** * ** ***


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

<br />

*** ** * ** ***

A `https://developer.android.com/reference/androidx/media3/datasource/DataSink` for writing to a byte array.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#ByteArrayDataSink()()` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#close()()` Closes the sink. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable byte[]` | `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#getData()()` Returns the data written to the sink since the last call to `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#open(androidx.media3.datasource.DataSpec)`, or null if `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#open(androidx.media3.datasource.DataSpec)` has never been called. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#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/ByteArrayDataSink#write(byte[],int,int)(byte[] buffer, int offset, int length)` Consumes the provided data. |

## Public constructors

### ByteArrayDataSink

```
public ByteArrayDataSink()
```

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

### getData

```
public @Nullable byte[] getData()
```

Returns the data written to the sink since the last call to `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#open(androidx.media3.datasource.DataSpec)`, or null if `https://developer.android.com/reference/androidx/media3/datasource/ByteArrayDataSink#open(androidx.media3.datasource.DataSpec)` has never been called.

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