@UnstableApi
public final class CacheDataSink implements DataSink


Writes data into a cache.

If the DataSpec passed to open has the length field set to LENGTH_UNSET and FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN set, then write calls are ignored.

Summary

Nested types

Thrown when an IOException is encountered when writing data to the sink.

public final class CacheDataSink.Factory implements DataSink.Factory

DataSink.Factory for CacheDataSink instances.

Constants

static final int

Default buffer size in bytes.

static final long

Default fragmentSize recommended for caching use cases.

Public constructors

CacheDataSink(Cache cache, long fragmentSize)

Constructs an instance using DEFAULT_BUFFER_SIZE.

CacheDataSink(Cache cache, long fragmentSize, int bufferSize)

Public methods

void

Closes the sink.

void
open(DataSpec dataSpec)

Opens the sink to consume the specified data.

void
write(byte[] buffer, int offset, int length)

Consumes the provided data.

Constants

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE = 20480

Default buffer size in bytes.

DEFAULT_FRAGMENT_SIZE

public static final long DEFAULT_FRAGMENT_SIZE = 5242880

Default fragmentSize recommended for caching use cases.

Public constructors

CacheDataSink

public CacheDataSink(Cache cache, long fragmentSize)

Constructs an instance using DEFAULT_BUFFER_SIZE.

Parameters
Cache cache

The cache into which data should be written.

long fragmentSize

For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to LENGTH_UNSET then no fragmentation will occur. Using a small value allows for finer-grained cache eviction policies, at the cost of increased overhead both on the cache implementation and the file system. Values under (2 * 1024 * 1024) are not recommended.

CacheDataSink

public CacheDataSink(Cache cache, long fragmentSize, int bufferSize)
Parameters
Cache cache

The cache into which data should be written.

long fragmentSize

For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to LENGTH_UNSET then no fragmentation will occur. Using a small value allows for finer-grained cache eviction policies, at the cost of increased overhead both on the cache implementation and the file system. Values under (2 * 1024 * 1024) are not recommended.

int bufferSize

The buffer size in bytes for writing to a cache file. A zero or negative value disables buffering.

Public methods

close

public void close()

Closes the sink.

Note: This method must be called even if the corresponding call to open threw an IOException. See open for more details.

open

public void open(DataSpec dataSpec)

Opens the sink to consume the specified data.

Note: If an IOException is thrown, callers must still call close to ensure that any partial effects of the invocation are cleaned up.

Parameters
DataSpec dataSpec

Defines the data to be consumed.

write

public void write(byte[] buffer, int offset, int length)

Consumes the provided data.

Parameters
byte[] buffer

The buffer from which data should be consumed.

int offset

The offset of the data to consume in buffer.

int length

The length of the data to consume, in bytes.