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

*** ** * ** ***


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

<br />

*** ** * ** ***

Writes data into a cache.

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

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink.CacheDataSinkException extends https://developer.android.com/reference/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. |
| `public final class https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink.Factory implements https://developer.android.com/reference/androidx/media3/datasource/DataSink.Factory` `https://developer.android.com/reference/androidx/media3/datasource/DataSink.Factory` for `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink` instances. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_BUFFER_SIZE() = 20480` Default buffer size in bytes. |
| `static final long` | `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_FRAGMENT_SIZE() = 5242880` Default `fragmentSize` recommended for caching use cases. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink#CacheDataSink(androidx.media3.datasource.cache.Cache,long)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, long fragmentSize)` Constructs an instance using `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink#DEFAULT_BUFFER_SIZE()`. |
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSink#CacheDataSink(androidx.media3.datasource.cache.Cache,long,int)(https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache, long fragmentSize, int bufferSize)` |

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

## Constants

### DEFAULT_BUFFER_SIZE

```
public static final int DEFAULT_BUFFER_SIZE = 20480
```

Default buffer size in bytes.

### DEFAULT_FRAGMENT_SIZE

```
public static final long DEFAULT_FRAGMENT_SIZE = 5242880
```

Default `fragmentSize` recommended for caching use cases.

## Public constructors

### CacheDataSink

```
public CacheDataSink(Cache cache, long fragmentSize)
```

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

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The cache into which data should be written. |
| `long fragmentSize` | 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/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

```
public CacheDataSink(Cache cache, long fragmentSize, int bufferSize)
```

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/Cache cache` | The cache into which data should be written. |
| `long fragmentSize` | 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/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. |
| `int bufferSize` | The buffer size in bytes for writing to a cache file. A zero or negative value disables buffering. |

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

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

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