# CacheWriter

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

*** ** * ** ***

<br />

```
@UnstableApi
class CacheWriter
```

<br />

*** ** * ** ***

Caching related utility methods.

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter.ProgressListener` Receives progress updates during cache operations. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter#DEFAULT_BUFFER_SIZE_BYTES() = 131072` Default buffer size to be used while caching. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter#CacheWriter(androidx.media3.datasource.cache.CacheDataSource,androidx.media3.datasource.DataSpec,byte[],androidx.media3.datasource.cache.CacheWriter.ProgressListener)( dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource!, dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!, temporaryBuffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>?, progressListener: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter.ProgressListener? )` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter#cache()()` Caches the requested data, skipping any that's already cached. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter#cancel()()` Cancels this writer's caching operation. |

## Constants

### DEFAULT_BUFFER_SIZE_BYTES

```
const val DEFAULT_BUFFER_SIZE_BYTES = 131072: Int
```

Default buffer size to be used while caching.

## Public constructors

### CacheWriter

```
CacheWriter(
    dataSource: CacheDataSource!,
    dataSpec: DataSpec!,
    temporaryBuffer: ByteArray<Byte>?,
    progressListener: CacheWriter.ProgressListener?
)
```

| Parameters |
|---|---|
| `dataSource: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource!` | A `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource` that writes to the target cache. |
| `dataSpec: https://developer.android.com/reference/kotlin/androidx/media3/datasource/DataSpec!` | Defines the data to be written. |
| `temporaryBuffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>?` | A temporary buffer to be used during caching, or `null` if the writer should instantiate its own internal temporary buffer. |
| `progressListener: https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter.ProgressListener?` | An optional progress listener. |

## Public functions

### cache

```
@WorkerThread
fun cache(): Unit
```

Caches the requested data, skipping any that's already cached.

If the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource` used by the writer has a `https://developer.android.com/reference/kotlin/androidx/media3/common/PriorityTaskManager`, then it's the responsibility of the caller to call `https://developer.android.com/reference/kotlin/androidx/media3/common/PriorityTaskManager#add(int)` to register with the manager before calling this method, and to call `https://developer.android.com/reference/kotlin/androidx/media3/common/PriorityTaskManager#remove(int)` afterwards to unregister. `https://developer.android.com/reference/kotlin/androidx/media3/common/PriorityTaskManager.PriorityTooLowException` will be thrown if the priority required by the `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheDataSource` is not high enough for progress to be made.

This method may be slow and shouldn't normally be called on the main thread.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs reading the data, or writing the data into the cache, or if the operation is canceled. If canceled, an `https://developer.android.com/reference/java/io/InterruptedIOException.html` is thrown. The method may be called again to continue the operation from where the error occurred. |

### cancel

```
fun cancel(): Unit
```

Cancels this writer's caching operation. `https://developer.android.com/reference/kotlin/androidx/media3/datasource/cache/CacheWriter#cache()` checks for cancelation frequently during execution, and throws an `https://developer.android.com/reference/java/io/InterruptedIOException.html` if it sees that the caching operation has been canceled.