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

*** ** * ** ***


```
@UnstableApi
public final class CacheWriter
```

<br />

*** ** * ** ***

Caching related utility methods.

## Summary

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

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/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/androidx/media3/datasource/cache/CacheWriter#CacheWriter(androidx.media3.datasource.cache.CacheDataSource,androidx.media3.datasource.DataSpec,byte[],androidx.media3.datasource.cache.CacheWriter.ProgressListener)( https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource dataSource, https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec, @https://developer.android.com/reference/androidx/annotation/Nullable byte[] temporaryBuffer, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/cache/CacheWriter.ProgressListener progressListener )` |

| ### Public methods |
|---|---|
| `void` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread https://developer.android.com/reference/androidx/media3/datasource/cache/CacheWriter#cache()()` Caches the requested data, skipping any that's already cached. |
| `void` | `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheWriter#cancel()()` Cancels this writer's caching operation. |

## Constants

### DEFAULT_BUFFER_SIZE_BYTES

```
public static final int DEFAULT_BUFFER_SIZE_BYTES = 131072
```

Default buffer size to be used while caching.

## Public constructors

### CacheWriter

```
public CacheWriter(
    CacheDataSource dataSource,
    DataSpec dataSpec,
    @Nullable byte[] temporaryBuffer,
    @Nullable CacheWriter.ProgressListener progressListener
)
```

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource dataSource` | A `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource` that writes to the target cache. |
| `https://developer.android.com/reference/androidx/media3/datasource/DataSpec dataSpec` | Defines the data to be written. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable byte[] temporaryBuffer` | A temporary buffer to be used during caching, or `null` if the writer should instantiate its own internal temporary buffer. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/androidx/media3/datasource/cache/CacheWriter.ProgressListener progressListener` | An optional progress listener. |

## Public methods

### cache

```
@WorkerThread
public void cache()
```

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

If the `https://developer.android.com/reference/androidx/media3/datasource/cache/CacheDataSource` used by the writer has a `https://developer.android.com/reference/androidx/media3/common/PriorityTaskManager`, then it's the responsibility of the caller to call `https://developer.android.com/reference/androidx/media3/common/PriorityTaskManager#add(int)` to register with the manager before calling this method, and to call `https://developer.android.com/reference/androidx/media3/common/PriorityTaskManager#remove(int)` afterwards to unregister. `https://developer.android.com/reference/androidx/media3/common/PriorityTaskManager.PriorityTooLowException` will be thrown if the priority required by the `https://developer.android.com/reference/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

```
public void cancel()
```

Cancels this writer's caching operation. `https://developer.android.com/reference/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.