Cache.CacheException, Cache.Listener
Constructor and Description |
---|
SimpleCache(File cacheDir,
CacheEvictor evictor)
Constructs the cache.
|
SimpleCache(File cacheDir,
CacheEvictor evictor,
byte[] secretKey)
Constructs the cache.
|
SimpleCache(File cacheDir,
CacheEvictor evictor,
byte[] secretKey,
boolean encrypt)
Constructs the cache.
|
Modifier and Type | Method and Description |
---|---|
NavigableSet<CacheSpan> |
addListener(String key,
Cache.Listener listener)
Registers a listener to listen for changes to a given key.
|
void |
commitFile(File file)
Commits a file into the cache.
|
long |
getCachedBytes(String key,
long position,
long length)
Returns the length of the cached data block starting from the
position to the block end
up to length bytes. |
NavigableSet<CacheSpan> |
getCachedSpans(String key)
Returns the cached spans for a given cache key.
|
long |
getCacheSpace()
Returns the total disk space in bytes used by the cache.
|
long |
getContentLength(String key)
Returns the content length for the given key if one set, or
C.LENGTH_UNSET otherwise. |
Set<String> |
getKeys()
Returns all keys in the cache.
|
boolean |
isCached(String key,
long position,
long length)
Queries if a range is entirely available in the cache.
|
void |
releaseHoleSpan(CacheSpan holeSpan)
Releases a
CacheSpan obtained from Cache.startReadWrite(String, long) which
corresponded to a hole in the cache. |
void |
removeListener(String key,
Cache.Listener listener)
Unregisters a listener.
|
void |
removeSpan(CacheSpan span)
Removes a cached
CacheSpan from the cache, deleting the underlying file. |
void |
setContentLength(String key,
long length)
Sets the content length for the given key.
|
File |
startFile(String key,
long position,
long maxLength)
Obtains a cache file into which data can be written.
|
com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan |
startReadWrite(String key,
long position)
A caller should invoke this method when they require data from a given position for a given
key.
|
com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan |
startReadWriteNonBlocking(String key,
long position)
Same as
Cache.startReadWrite(String, long) . |
public SimpleCache(File cacheDir, CacheEvictor evictor)
cacheDir
- A dedicated cache directory.evictor
- The evictor to be used.public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey)
cacheDir
- A dedicated cache directory.evictor
- The evictor to be used.secretKey
- If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
The key must be 16 bytes long.public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey, boolean encrypt)
cacheDir
- A dedicated cache directory.evictor
- The evictor to be used.secretKey
- If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
The key must be 16 bytes long.encrypt
- When false, a plaintext index will be written.public NavigableSet<CacheSpan> addListener(String key, Cache.Listener listener)
Cache
No guarantees are made about the thread or threads on which the listener is called, but it is guaranteed that listener methods will be called in a serial fashion (i.e. one at a time) and in the same order as events occurred.
addListener
in interface Cache
key
- The key to listen to.listener
- The listener to add.public void removeListener(String key, Cache.Listener listener)
Cache
removeListener
in interface Cache
key
- The key to stop listening to.listener
- The listener to remove.public NavigableSet<CacheSpan> getCachedSpans(String key)
Cache
getCachedSpans
in interface Cache
key
- The key for which spans should be returned.public Set<String> getKeys()
Cache
public long getCacheSpace()
Cache
getCacheSpace
in interface Cache
public com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan startReadWrite(String key, long position) throws InterruptedException, Cache.CacheException
Cache
If there is a cache entry that overlaps the position, then the returned CacheSpan
defines the file in which the data is stored. CacheSpan.isCached
is true. The caller
may read from the cache file, but does not acquire any locks.
If there is no cache entry overlapping offset
, then the returned CacheSpan
defines a hole in the cache starting at position
into which the caller may write as it
obtains the data from some other source. The returned CacheSpan
serves as a lock.
Whilst the caller holds the lock it may write data into the hole. It may split data into
multiple files. When the caller has finished writing a file it should commit it to the cache
by calling Cache.commitFile(File)
. When the caller has finished writing, it must release
the lock by calling Cache.releaseHoleSpan(com.google.android.exoplayer2.upstream.cache.CacheSpan)
.
startReadWrite
in interface Cache
key
- The key of the data being requested.position
- The position of the data being requested.CacheSpan
.InterruptedException
Cache.CacheException
public com.google.android.exoplayer2.upstream.cache.SimpleCacheSpan startReadWriteNonBlocking(String key, long position) throws Cache.CacheException
Cache
Cache.startReadWrite(String, long)
. However, if the cache entry is locked, then
instead of blocking, this method will return null as the CacheSpan
.startReadWriteNonBlocking
in interface Cache
key
- The key of the data being requested.position
- The position of the data being requested.CacheSpan
. Or null if the cache entry is locked.Cache.CacheException
public File startFile(String key, long position, long maxLength) throws Cache.CacheException
Cache
CacheSpan
obtained from Cache.startReadWrite(String, long)
.startFile
in interface Cache
key
- The cache key for the data.position
- The starting position of the data.maxLength
- The maximum length of the data to be written. Used only to ensure that there
is enough space in the cache.Cache.CacheException
public void commitFile(File file) throws Cache.CacheException
Cache
CacheSpan
obtained from Cache.startReadWrite(String, long)
commitFile
in interface Cache
file
- A newly written cache file.Cache.CacheException
public void releaseHoleSpan(CacheSpan holeSpan)
Cache
CacheSpan
obtained from Cache.startReadWrite(String, long)
which
corresponded to a hole in the cache.releaseHoleSpan
in interface Cache
holeSpan
- The CacheSpan
being released.public void removeSpan(CacheSpan span) throws Cache.CacheException
Cache
CacheSpan
from the cache, deleting the underlying file.removeSpan
in interface Cache
span
- The CacheSpan
to remove.Cache.CacheException
public boolean isCached(String key, long position, long length)
Cache
public long getCachedBytes(String key, long position, long length)
Cache
position
to the block end
up to length
bytes. If the position
isn't cached then -(the length of the gap
to the next cached data up to length
bytes) is returned.getCachedBytes
in interface Cache
key
- The cache key for the data.position
- The starting position of the data.length
- The maximum length of the data to be returned.public void setContentLength(String key, long length) throws Cache.CacheException
Cache
setContentLength
in interface Cache
key
- The cache key for the data.length
- The length of the data.Cache.CacheException
public long getContentLength(String key)
Cache
C.LENGTH_UNSET
otherwise.getContentLength
in interface Cache
key
- The cache key for the data.