# FailOnCloseDataSink

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

*** ** * ** ***


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

<br />

*** ** * ** ***

A `https://developer.android.com/reference/androidx/media3/datasource/DataSink` that can simulate caching the bytes being written to it, and then failing to persist them when `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink#close()` is called.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink.Factory implements https://developer.android.com/reference/androidx/media3/datasource/DataSink.Factory` Factory to create a `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink`. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink#FailOnCloseDataSink(androidx.media3.datasource.cache.Cache,java.util.concurrent.atomic.AtomicBoolean)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, https://developer.android.com/reference/java/util/concurrent/atomic/AtomicBoolean.html failOnClose)` Creates an instance. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink#close()()` Closes the sink. |
| `void` | `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink#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/test/utils/FailOnCloseDataSink#write(byte[],int,int)(byte[] buffer, int offset, int length)` Consumes the provided data. |

## Public constructors

### FailOnCloseDataSink

```
public FailOnCloseDataSink(Cache cache, AtomicBoolean failOnClose)
```

Creates an instance.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The cache to write to when not in fail-on-close mode. |
| `https://developer.android.com/reference/java/util/concurrent/atomic/AtomicBoolean.html failOnClose` | An `https://developer.android.com/reference/java/util/concurrent/atomic/AtomicBoolean.html` whose value is read in each call to `https://developer.android.com/reference/androidx/media3/test/utils/FailOnCloseDataSink#open(androidx.media3.datasource.DataSpec)` to determine whether to enable fail-on-close for the read that's being started. |

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