public final class CacheDataSource extends Object implements DataSource
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.Modifier and Type | Class and Description |
---|---|
static interface |
CacheDataSource.EventListener
Interface definition for a callback to be notified of
CacheDataSource events. |
Constructor and Description |
---|
CacheDataSource(Cache cache,
DataSource upstream,
boolean blockOnCache,
boolean ignoreCacheOnError)
Constructs an instance with default
DataSource and DataSink instances for
reading and writing the cache. |
CacheDataSource(Cache cache,
DataSource upstream,
boolean blockOnCache,
boolean ignoreCacheOnError,
long maxCacheFileSize)
Constructs an instance with default
DataSource and DataSink instances for
reading and writing the cache. |
CacheDataSource(Cache cache,
DataSource upstream,
DataSource cacheReadDataSource,
DataSink cacheWriteDataSink,
boolean blockOnCache,
boolean ignoreCacheOnError,
CacheDataSource.EventListener eventListener)
Constructs an instance with arbitrary
DataSource and DataSink instances for
reading and writing the cache. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the
DataSource . |
long |
open(DataSpec dataSpec)
Opens the
DataSource to read the specified data. |
int |
read(byte[] buffer,
int offset,
int max)
Reads up to
length bytes of data and stores them into buffer , starting at
index offset . |
public CacheDataSource(Cache cache, DataSource upstream, boolean blockOnCache, boolean ignoreCacheOnError)
DataSource
and DataSink
instances for
reading and writing the cache.public CacheDataSource(Cache cache, DataSource upstream, boolean blockOnCache, boolean ignoreCacheOnError, long maxCacheFileSize)
DataSource
and DataSink
instances for
reading and writing the cache. The sink is configured to fragment data such that no single
cache file is greater than maxCacheFileSize bytes.public CacheDataSource(Cache cache, DataSource upstream, DataSource cacheReadDataSource, DataSink cacheWriteDataSink, boolean blockOnCache, boolean ignoreCacheOnError, CacheDataSource.EventListener eventListener)
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.cache
- The cache.upstream
- A DataSource
for reading data not in the cache.cacheReadDataSource
- A DataSource
for reading data from the cache.cacheWriteDataSink
- A DataSink
for writing data to the cache.blockOnCache
- A flag indicating whether we will block reads if the cache key is locked.
If this flag is false, then we will read from upstream if the cache key is locked.ignoreCacheOnError
- Whether the cache is bypassed following any cache related error. If
true, 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.eventListener
- An optional CacheDataSource.EventListener
to receive events.public long open(DataSpec dataSpec) throws IOException
DataSource
DataSource
to read the specified data. Calls to DataSource.open(DataSpec)
and
DataSource.close()
must be balanced.
Note: If DataSource.open(DataSpec)
throws an IOException
, callers must still call
DataSource.close()
to ensure that any partial effects of the DataSource.open(DataSpec)
invocation
are cleaned up. Implementations of this class can assume that callers will call
DataSource.close()
in this case.
open
in interface DataSource
dataSpec
- Defines the data to be read.DataSpec.length
equals C.LENGTH_UNBOUNDED
) this value
is the resolved length of the request, or C.LENGTH_UNBOUNDED
if the length is still
unresolved. For all other requests, the value returned will be equal to the request's
DataSpec.length
.IOException
- If an error occurs opening the source.public int read(byte[] buffer, int offset, int max) throws IOException
DataSource
length
bytes of data and stores them into buffer
, starting at
index offset
.
This method blocks until at least one byte of data can be read, the end of the opened range is detected, or an exception is thrown.
read
in interface DataSource
buffer
- The buffer into which the read data should be stored.offset
- The start offset into buffer
at which data should be written.max
- The maximum number of bytes to read.C.RESULT_END_OF_INPUT
if the end of the opened
range is reached.IOException
- If an error occurs reading from the source.public void close() throws IOException
DataSource
DataSource
.
Note: This method will be called even if the corresponding call to DataSource.open(DataSpec)
threw an IOException
. See DataSource.open(DataSpec)
for more details.
close
in interface DataSource
IOException
- If an error occurs closing the source.