public final class CacheDataSource extends java.lang.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.
By default requests whose length can not be resolved are not cached. This is to prevent
caching of progressive live streams, which should usually not be cached. Caching of this kind of
requests can be enabled per request with DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH.
| Modifier and Type | Class and Description |
|---|---|
static interface |
CacheDataSource.CacheIgnoredReason
Reasons the cache may be ignored.
|
static interface |
CacheDataSource.EventListener
Listener of
CacheDataSource events. |
static interface |
CacheDataSource.Flags
Flags controlling the cache's behavior.
|
DataSource.Factory| Modifier and Type | Field and Description |
|---|---|
static int |
CACHE_IGNORED_REASON_ERROR
Cache ignored due to a cache related error.
|
static int |
CACHE_IGNORED_REASON_UNSET_LENGTH
Cache ignored due to a request with an unset length.
|
static long |
DEFAULT_MAX_CACHE_FILE_SIZE
Default maximum single cache file size.
|
static int |
FLAG_BLOCK_ON_CACHE
A flag indicating whether we will block reads if the cache key is locked.
|
static int |
FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS
A flag indicating that the cache should be bypassed for requests whose lengths are unset.
|
static int |
FLAG_IGNORE_CACHE_ON_ERROR
A flag indicating whether the cache is bypassed following any cache related error.
|
| Constructor and Description |
|---|
CacheDataSource(Cache cache,
DataSource upstream)
Constructs an instance with default
DataSource and DataSink instances for
reading and writing the cache. |
CacheDataSource(Cache cache,
DataSource upstream,
DataSource cacheReadDataSource,
DataSink cacheWriteDataSink,
int flags,
CacheDataSource.EventListener eventListener)
Constructs an instance with arbitrary
DataSource and DataSink instances for
reading and writing the cache. |
CacheDataSource(Cache cache,
DataSource upstream,
int flags)
Constructs an instance with default
DataSource and DataSink instances for
reading and writing the cache. |
CacheDataSource(Cache cache,
DataSource upstream,
int flags,
long maxCacheFileSize)
Constructs an instance with default
DataSource and DataSink instances for
reading and writing the cache. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the source.
|
android.net.Uri |
getUri()
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 readLength)
Reads up to
length bytes of data and stores them into buffer, starting at
index offset. |
public static final long DEFAULT_MAX_CACHE_FILE_SIZE
public static final int FLAG_BLOCK_ON_CACHE
public static final int FLAG_IGNORE_CACHE_ON_ERROR
public static final int FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS
public static final int CACHE_IGNORED_REASON_ERROR
public static final int CACHE_IGNORED_REASON_UNSET_LENGTH
public CacheDataSource(Cache cache, DataSource upstream)
DataSource and DataSink instances for
reading and writing the cache.cache - The cache.upstream - A DataSource for reading data not in the cache.public CacheDataSource(Cache cache, DataSource upstream, int flags)
DataSource and DataSink instances for
reading and writing the cache.cache - The cache.upstream - A DataSource for reading data not in the cache.flags - A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR
and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.public CacheDataSource(Cache cache, DataSource upstream, int flags, 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.cache - The cache.upstream - A DataSource for reading data not in the cache.flags - A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR
and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.maxCacheFileSize - The maximum size of a cache file, in bytes. If the cached data size
exceeds this value, then the data will be fragmented into multiple cache files. The
finer-grained this is the finer-grained the eviction policy can be.public CacheDataSource(Cache cache, DataSource upstream, DataSource cacheReadDataSource, DataSink cacheWriteDataSink, int flags, @Nullable 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. If null, cache is
accessed read-only.flags - A combination of FLAG_BLOCK_ON_CACHE, FLAG_IGNORE_CACHE_ON_ERROR
and FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS, or 0.eventListener - An optional CacheDataSource.EventListener to receive events.public long open(DataSpec dataSpec) throws java.io.IOException
DataSource
Note: If an IOException is thrown, callers must still call DataSource.close() to ensure
that any partial effects of the invocation are cleaned up.
open in interface DataSourcedataSpec - Defines the data to be read.DataSpec.length equals C.LENGTH_UNSET) this value
is the resolved length of the request, or C.LENGTH_UNSET if the length is still
unresolved. For all other requests, the value returned will be equal to the request's
DataSpec.length.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.public int read(byte[] buffer,
int offset,
int readLength)
throws java.io.IOException
DataSourcelength bytes of data and stores them into buffer, starting at
index offset.
If length is zero then 0 is returned. Otherwise, if no data is available because the
end of the opened range has been reached, then C.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.
read in interface DataSourcebuffer - The buffer into which the read data should be stored.offset - The start offset into buffer at which data should be written.readLength - The maximum number of bytes to read.C.RESULT_END_OF_INPUT if no data is available
because the end of the opened range has been reached.java.io.IOException - If an error occurs reading from the source.public android.net.Uri getUri()
DataSourceUri from which data is being read. The returned
Uri will be identical to the one passed DataSource.open(DataSpec) in the DataSpec
unless redirection has occurred. If redirection has occurred, the Uri after redirection
is returned.getUri in interface DataSourceUri from which data is being read, or null if the source is not open.public void close()
throws java.io.IOException
DataSource
Note: This method must be called even if the corresponding call to DataSource.open(DataSpec)
threw an IOException. See DataSource.open(DataSpec) for more details.
close in interface DataSourcejava.io.IOException - If an error occurs closing the source.