# CacheDataSink

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

*** ** * ** ***

<br />

```
@UnstableApi
class CacheDataSink : DataSink
```

<br />

*** ** * ** ***

Writes data into a cache.

If the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec` passed to `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#open(androidx.media3.datasource.DataSpec)` has the `length` field set to `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` and `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec#FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN()` set, then `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#write(byte[],int,int)` calls are ignored.

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink.CacheDataSinkException : https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache.CacheException` Thrown when an `https://developer.android.com/reference/java/io/IOException.html` is encountered when writing data to the sink. |
| `class https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink.Factory : https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSink.Factory` `https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSink.Factory` for `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink` instances. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_BUFFER_SIZE() = 20480` Default buffer size in bytes. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_FRAGMENT_SIZE() = 5242880` Default `fragmentSize` recommended for caching use cases. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#CacheDataSink(androidx.media3.datasource.cache.Cache,long)(cache: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache!, fragmentSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Constructs an instance using `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_BUFFER_SIZE()`. |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#CacheDataSink(androidx.media3.datasource.cache.Cache,long,int)(cache: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache!, fragmentSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, bufferSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#close()()` Closes the sink. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#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/datasource/cache/CacheDataSink#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. |

## Constants

### DEFAULT_BUFFER_SIZE

```
const val DEFAULT_BUFFER_SIZE = 20480: Int
```

Default buffer size in bytes.

### DEFAULT_FRAGMENT_SIZE

```
const val DEFAULT_FRAGMENT_SIZE = 5242880: Long
```

Default `fragmentSize` recommended for caching use cases.

## Public constructors

### CacheDataSink

```
CacheDataSink(cache: Cache!, fragmentSize: Long)
```

Constructs an instance using `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_BUFFER_SIZE()`.

| Parameters |
|---|---|
| `cache: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache!` | The cache into which data should be written. |
| `fragmentSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` then no fragmentation will occur. Using a small value allows for finer-grained cache eviction policies, at the cost of increased overhead both on the cache implementation and the file system. Values under `(2 * 1024 * 1024)` are not recommended. |

### CacheDataSink

```
CacheDataSink(cache: Cache!, fragmentSize: Long, bufferSize: Int)
```

| Parameters |
|---|---|
| `cache: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/Cache!` | The cache into which data should be written. |
| `fragmentSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to `https://developer.android.com/reference/kotlin/androidx/media3/common/C#LENGTH_UNSET()` then no fragmentation will occur. Using a small value allows for finer-grained cache eviction policies, at the cost of increased overhead both on the cache implementation and the file system. Values under `(2 * 1024 * 1024)` are not recommended. |
| `bufferSize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The buffer size in bytes for writing to a cache file. A zero or negative value disables buffering. |

## 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. |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink.CacheDataSinkException` |   |

### 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. |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink.CacheDataSinkException` |   |

### 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. |
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSink.CacheDataSinkException` |   |