Package org.openhab.core.cache.lru
Class LRUMediaCacheEntry<V>
java.lang.Object
org.openhab.core.cache.lru.LRUMediaCacheEntry<V>
A cached media entry resulting from a call to a supplier or a load from disk
This class also adds the capability to serve multiple InputStream concurrently
without asking already retrieved data to the wrapped stream.
- Author:
- Gwendal Roulleau - Initial contribution
-
Field Summary
-
Constructor Summary
ConstructorDescriptionLRUMediaCacheEntry
(String key) This constructor is used when the file is fully cached on disk.LRUMediaCacheEntry
(String key, InputStream inputStream, @Nullable V metadata) This constructor is used when the file is not yet cached on disk. -
Method Summary
Modifier and TypeMethodDescriptionprotected int
availableFrom
(int offset) Return the number of bytes that we can actually read without calling the underlying streamprotected void
This method is called by a wrapper when it has been closed by a client The file and the inner stream could then be closed, if and only if no other client are accessing it.protected void
Delete the cache file linked to this entryprotected long
Get the current sizeOpen an InputStream wrapped around the file There could be several clients InputStream on the same cache resultprotected String
getKey()
Get the key identifying this cache entry@Nullable V
Get metadata for this cache result.protected long
Get total size of the underlying stream.boolean
isFaulty()
protected byte[]
read
(int start, int sizeToRead) Read from the cached file.protected void
setCacheContext
(Path cacheDirectory, Storage<V> storage) Link this cache entry to the underlying storage facility (disk for data, storage service for metadata)
-
Field Details
-
currentSize
protected long currentSize
-
-
Constructor Details
-
LRUMediaCacheEntry
This constructor is used when the file is fully cached on disk. The file on disk will provide the data, and the storage will provide metadata.- Parameters:
key
- A unique key to identify the produced data
-
LRUMediaCacheEntry
This constructor is used when the file is not yet cached on disk. Data is provided by the arguments- Parameters:
key
- A unique key to identify the produced datainputStream
- The data streammetadata
- optional metadata to store along the stream
-
-
Method Details
-
setCacheContext
Link this cache entry to the underlying storage facility (disk for data, storage service for metadata)- Parameters:
cacheDirectory
-storage
-
-
getTotalSize
protected long getTotalSize()Get total size of the underlying stream. If not already completed, will query the stream inside, or get all the data.- Returns:
-
getCurrentSize
protected long getCurrentSize()Get the current size- Returns:
-
getKey
Get the key identifying this cache entry- Returns:
-
getInputStream
Open an InputStream wrapped around the file There could be several clients InputStream on the same cache result- Returns:
- A new InputStream with data from the cache
- Throws:
IOException
-
closeStreamClient
This method is called by a wrapper when it has been closed by a client The file and the inner stream could then be closed, if and only if no other client are accessing it.- Throws:
IOException
-
getMetadata
Get metadata for this cache result.- Returns:
- metadata
-
read
Read from the cached file. If there is not enough bytes to read in the file, the supplier will be queried.- Parameters:
start
- The offset to read the file fromsizeToRead
- the number of byte to read- Returns:
- A byte array from the file. The size may or may not be the sizeToRead requested
- Throws:
IOException
-
availableFrom
protected int availableFrom(int offset) Return the number of bytes that we can actually read without calling the underlying stream- Parameters:
offset
-- Returns:
-
deleteFile
protected void deleteFile()Delete the cache file linked to this entry -
isFaulty
public boolean isFaulty()
-