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

*** ** * ** ***

<br />

```
@UnstableApi
class FailOnCloseDataSink : DataSink
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/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/kotlin/androidx/media3/test/utils/FailOnCloseDataSink#close()` is called.

## Summary

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

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

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FailOnCloseDataSink#close()()` Closes the sink. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FailOnCloseDataSink#open(androidx.media3.datasource.DataSpec)(dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!)` Opens the sink to consume the specified data. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/test/utils/FailOnCloseDataSink#write(byte[],int,int)(buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!, offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Consumes the provided data. |

## Public constructors

### FailOnCloseDataSink

```
FailOnCloseDataSink(cache: Cache!, failOnClose: AtomicBoolean!)
```

Creates an instance.

| Parameters |
|---|---|
| `cache: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache!` | The cache to write to when not in fail-on-close mode. |
| `failOnClose: https://developer.android.com/reference/java/util/concurrent/atomic/AtomicBoolean.html!` | 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/kotlin/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 functions

### close

```
fun close(): Unit
```

Closes the sink.

Note: This method must be called even if the corresponding call to `https://developer.android.com/reference/kotlin/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/kotlin/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

```
fun open(dataSpec: DataSpec!): Unit
```

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/kotlin/androidx/media3/datasource/DataSink#close()` to ensure that any partial effects of the invocation are cleaned up.

| Parameters |
|---|---|
| `dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/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

```
fun write(buffer: ByteArray<Byte>!, offset: Int, length: Int): Unit
```

Consumes the provided data.

| Parameters |
|---|---|
| `buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The buffer from which data should be consumed. |
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The offset of the data to consume in `buffer`. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | 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. |