Package com.google.android.exoplayer2.upstream.cache

## Class CacheWriter

- java.lang.Object
- - com.google.android.exoplayer2.upstream.cache.CacheWriter

-     public final class CacheWriter
      extends Object

  Caching related utility methods.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `CacheWriter.ProgressListener` | Receives progress updates during cache operations.

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static int` | `DEFAULT_BUFFER_SIZE_BYTES` | Default buffer size to be used while caching.

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `CacheWriter​(CacheDataSource dataSource, DataSpec dataSpec, byte[] temporaryBuffer, CacheWriter.ProgressListener progressListener)` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `cache()` | Caches the requested data, skipping any that's already cached.
    `void` | `cancel()` | Cancels this writer's caching operation.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_BUFFER_SIZE_BYTES

          public static final int DEFAULT_BUFFER_SIZE_BYTES

      Default buffer size to be used while caching.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### CacheWriter

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

      Parameters:
        `dataSource` - A `CacheDataSource` that writes to the target cache.
        `dataSpec` - Defines the data to be written.
        `temporaryBuffer` - A temporary buffer to be used during caching, or `null` if the writer should instantiate its own internal temporary buffer.
        `progressListener` - An optional progress listener.

  - ### Method Detail

    - #### cancel

          public void cancel()

      Cancels this writer's caching operation. `cache` checks for cancelation frequently during execution, and throws an `InterruptedIOException` if it sees that the caching operation has been canceled.

    - #### cache

          @WorkerThread
          public void cache()
                     throws IOException

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

      If the `CacheDataSource` used by the writer has a `PriorityTaskManager`, then it's the responsibility of the caller to call `PriorityTaskManager.add(int)` to register with the manager before calling this method, and to call `PriorityTaskManager.remove(int)` afterwards to unregister. `PriorityTaskManager.PriorityTooLowException` will be thrown if the priority required by the `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:
        `IOException` - If an error occurs reading the data, or writing the data into the cache, or if the operation is canceled. If canceled, an `InterruptedIOException` is thrown. The method may be called again to continue the operation from where the error occurred.