Class LRUMediaCacheEntry<V>

java.lang.Object
org.openhab.core.cache.lru.LRUMediaCacheEntry<V>

@NonNullByDefault public class LRUMediaCacheEntry<V> extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    protected long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    protected int
    availableFrom(int offset)
    Return the number of bytes that we can actually read without calling the underlying stream
    protected 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 entry
    protected long
    Get the current size
    Open an InputStream wrapped around the file There could be several clients InputStream on the same cache result
    protected String
    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
     
    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)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • currentSize

      protected long currentSize
  • Constructor Details

    • LRUMediaCacheEntry

      public LRUMediaCacheEntry(String key)
      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

      public LRUMediaCacheEntry(String key, InputStream inputStream, @Nullable V metadata)
      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 data
      inputStream - The data stream
      metadata - optional metadata to store along the stream
  • Method Details

    • setCacheContext

      protected void setCacheContext(Path cacheDirectory, Storage<V> storage)
      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

      protected String getKey()
      Get the key identifying this cache entry
      Returns:
    • getInputStream

      public InputStream getInputStream() throws IOException
      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

      protected void closeStreamClient() throws IOException
      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

      public @Nullable V getMetadata()
      Get metadata for this cache result.
      Returns:
      metadata
    • read

      protected byte[] read(int start, int sizeToRead) throws IOException
      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 from
      sizeToRead - 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()