@UnstableApi
public final class CacheWriter


Caching related utility methods.

Summary

Nested types

Receives progress updates during cache operations.

Constants

static final int

Default buffer size to be used while caching.

Public constructors

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

Public methods

void

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

void

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
CacheDataSource dataSource

A CacheDataSource that writes to the target cache.

DataSpec dataSpec

Defines the data to be written.

@Nullable byte[] temporaryBuffer

A temporary buffer to be used during caching, or null if the writer should instantiate its own internal temporary buffer.

@Nullable 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 CacheDataSource used by the writer has a PriorityTaskManager, then it's the responsibility of the caller to call add to register with the manager before calling this method, and to call remove afterwards to unregister. 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
java.io.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.

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.