@UnstableApi
public final class CacheDataSource implements DataSource


A DataSource that reads and writes a Cache. Requests are fulfilled from the cache when possible. When data is not cached it is requested from an upstream DataSource and written into the cache.

Summary

Nested types

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = )
@IntDef(value = )
public annotation CacheDataSource.CacheIgnoredReason

Reasons the cache may be ignored.

Listener of CacheDataSource events.

public final class CacheDataSource.Factory implements DataSource.Factory

DataSource.Factory for CacheDataSource instances.

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(flag = true, value = )
public annotation CacheDataSource.Flags

Flags controlling the CacheDataSource's behavior.

Constants

static final int

Cache ignored due to a cache related error.

static final int

Cache ignored due to a request with an unset length.

static final int

A flag indicating whether we will block reads if the cache key is locked.

static final int

A flag indicating that the cache should be bypassed for requests whose lengths are unset.

static final int

A flag indicating whether the cache is bypassed following any cache related error.

Public fields

final Cache
final CacheKeyFactory

Public constructors

CacheDataSource(Cache cache, @Nullable DataSource upstreamDataSource)

Constructs an instance with default DataSource and DataSink instances for reading and writing the cache.

CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    @CacheDataSource.Flags int flags
)

Constructs an instance with default DataSource and DataSink instances for reading and writing the cache.

CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    DataSource cacheReadDataSource,
    @Nullable DataSink cacheWriteDataSink,
    @CacheDataSource.Flags int flags,
    @Nullable CacheDataSource.EventListener eventListener
)

Constructs an instance with arbitrary DataSource and DataSink instances for reading and writing the cache.

CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    DataSource cacheReadDataSource,
    @Nullable DataSink cacheWriteDataSink,
    @CacheDataSource.Flags int flags,
    @Nullable CacheDataSource.EventListener eventListener,
    @Nullable CacheKeyFactory cacheKeyFactory
)

Constructs an instance with arbitrary DataSource and DataSink instances for reading and writing the cache.

Public methods

void

Adds a TransferListener to listen to data transfers.

void

Closes the source.

Cache

Returns the Cache used by this instance.

CacheKeyFactory

Returns the CacheKeyFactory used by this instance.

Map<StringList<String>>

When the source is open, returns the response headers associated with the last open call.

@Nullable Uri

When the source is open, returns the Uri from which data is being read.

long
open(DataSpec dataSpec)

Opens the source to read the specified data.

int
read(byte[] buffer, int offset, int length)

Reads up to length bytes of data from the input.

Constants

CACHE_IGNORED_REASON_ERROR

public static final int CACHE_IGNORED_REASON_ERROR = 0

Cache ignored due to a cache related error.

CACHE_IGNORED_REASON_UNSET_LENGTH

public static final int CACHE_IGNORED_REASON_UNSET_LENGTH = 1

Cache ignored due to a request with an unset length.

FLAG_BLOCK_ON_CACHE

public static final int FLAG_BLOCK_ON_CACHE = 1

A flag indicating whether we will block reads if the cache key is locked. If unset then data is read from upstream if the cache key is locked, regardless of whether the data is cached.

FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS

public static final int FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS = 4

A flag indicating that the cache should be bypassed for requests whose lengths are unset. This flag is provided for legacy reasons only.

FLAG_IGNORE_CACHE_ON_ERROR

public static final int FLAG_IGNORE_CACHE_ON_ERROR = 2

A flag indicating whether the cache is bypassed following any cache related error. If set then cache related exceptions may be thrown for one cycle of open, read and close calls. Subsequent cycles of these calls will then bypass the cache.

Public fields

cache

public final Cache cache

cacheKeyFactory

public final CacheKeyFactory cacheKeyFactory

Public constructors

CacheDataSource

public CacheDataSource(Cache cache, @Nullable DataSource upstreamDataSource)

Constructs an instance with default DataSource and DataSink instances for reading and writing the cache.

Parameters
Cache cache

The cache.

@Nullable DataSource upstreamDataSource

A DataSource for reading data not in the cache. If null, reading will fail if a cache miss occurs.

CacheDataSource

public CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    @CacheDataSource.Flags int flags
)

Constructs an instance with default DataSource and DataSink instances for reading and writing the cache.

Parameters
Cache cache

The cache.

@Nullable DataSource upstreamDataSource

A DataSource for reading data not in the cache. If null, reading will fail if a cache miss occurs.

@CacheDataSource.Flags int flags

A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.

CacheDataSource

public CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    DataSource cacheReadDataSource,
    @Nullable DataSink cacheWriteDataSink,
    @CacheDataSource.Flags int flags,
    @Nullable CacheDataSource.EventListener eventListener
)

Constructs an instance with arbitrary DataSource and DataSink instances for reading and writing the cache. One use of this constructor is to allow data to be transformed before it is written to disk.

Parameters
Cache cache

The cache.

@Nullable DataSource upstreamDataSource

A DataSource for reading data not in the cache. If null, reading will fail if a cache miss occurs.

DataSource cacheReadDataSource

A DataSource for reading data from the cache.

@Nullable DataSink cacheWriteDataSink

A DataSink for writing data to the cache. If null, cache is accessed read-only.

@CacheDataSource.Flags int flags

A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.

@Nullable CacheDataSource.EventListener eventListener

An optional EventListener to receive events.

CacheDataSource

public CacheDataSource(
    Cache cache,
    @Nullable DataSource upstreamDataSource,
    DataSource cacheReadDataSource,
    @Nullable DataSink cacheWriteDataSink,
    @CacheDataSource.Flags int flags,
    @Nullable CacheDataSource.EventListener eventListener,
    @Nullable CacheKeyFactory cacheKeyFactory
)

Constructs an instance with arbitrary DataSource and DataSink instances for reading and writing the cache. One use of this constructor is to allow data to be transformed before it is written to disk.

Parameters
Cache cache

The cache.

@Nullable DataSource upstreamDataSource

A DataSource for reading data not in the cache. If null, reading will fail if a cache miss occurs.

DataSource cacheReadDataSource

A DataSource for reading data from the cache.

@Nullable DataSink cacheWriteDataSink

A DataSink for writing data to the cache. If null, cache is accessed read-only.

@CacheDataSource.Flags int flags

A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.

@Nullable CacheDataSource.EventListener eventListener

An optional EventListener to receive events.

@Nullable CacheKeyFactory cacheKeyFactory

An optional factory for cache keys.

Public methods

addTransferListener

public void addTransferListener(TransferListener transferListener)

Adds a TransferListener to listen to data transfers. This method is not thread-safe.

Parameters
TransferListener transferListener

A TransferListener.

close

public void close()

Closes the source. This method must be called even if the corresponding call to open threw an IOException.

Throws
java.io.IOException

If an error occurs closing the source.

getCache

public Cache getCache()

Returns the Cache used by this instance.

getCacheKeyFactory

public CacheKeyFactory getCacheKeyFactory()

Returns the CacheKeyFactory used by this instance.

getResponseHeaders

public Map<StringList<String>> getResponseHeaders()

When the source is open, returns the response headers associated with the last open call. Otherwise, returns an empty map.

Key look-up in the returned map is case-insensitive.

getUri

public @Nullable Uri getUri()

When the source is open, returns the Uri from which data is being read. The returned Uri will be identical to the one passed open in the DataSpec unless redirection has occurred. If redirection has occurred, the Uri after redirection is returned.

Returns
@Nullable Uri

The Uri from which data is being read, or null if the source is not open.

open

public long open(DataSpec dataSpec)

Opens the source to read the specified data. If an IOException is thrown, callers must still call close to ensure that any partial effects of the invocation are cleaned up.

The following edge case behaviors apply:

Parameters
DataSpec dataSpec

Defines the data to be read.

Returns
long

The number of bytes that can be read from the opened source. For unbounded requests (i.e., requests where length equals LENGTH_UNSET) this value is the resolved length of the request, or LENGTH_UNSET if the length is still unresolved. For all other requests, the value returned will be equal to the request's length.

Throws
java.io.IOException

If an error occurs opening the source. DataSourceException can be thrown or used as a cause of the thrown exception to specify the reason of the error.

read

public int read(byte[] buffer, int offset, int length)

Reads up to length bytes of data from the input.

If readLength is zero then 0 is returned. Otherwise, if no data is available because the end of the opened range has been reached, then RESULT_END_OF_INPUT is returned. Otherwise, the call will block until at least one byte of data has been read and the number of bytes read is returned.

Parameters
byte[] buffer

A target array into which data should be written.

int offset

The offset into the target array at which to write.

int length

The maximum number of bytes to read from the input.

Returns
int

The number of bytes read, or RESULT_END_OF_INPUT if the input has ended. This may be less than length because the end of the input (or available data) was reached, the method was interrupted, or the operation was aborted early for another reason.

Throws
java.io.IOException

If an error occurs reading from the input.