Class LazyResource

java.lang.Object
nl.siegmann.epublib.domain.Resource
nl.siegmann.epublib.domain.LazyResource
All Implemented Interfaces:
Serializable

public class LazyResource extends Resource
A Resource that loads its data only on-demand. This way larger books can fit into memory and can be opened faster.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • filename

      private String filename
    • cachedSize

      private long cachedSize
    • LOG

      private static final org.slf4j.Logger LOG
  • Constructor Details

    • LazyResource

      public LazyResource(String filename, long size, String href)
      Creates a Lazy resource, by not actually loading the data for this entry. The data will be loaded on the first call to getData()
      Parameters:
      filename - the file name for the epub we're created from.
      size - the size of this resource.
      href - The resource's href within the epub.
    • LazyResource

      public LazyResource(InputStream in, String filename, int length, String href) throws IOException
      Creates a Resource that tries to load the data, but falls back to lazy loading. If the size of the resource is known ahead of time we can use that to allocate a matching byte[]. If this succeeds we can safely load the data. If it fails we leave the data null for now and it will be lazy-loaded when it is accessed.
      Parameters:
      in -
      length -
      href -
      fileName -
      Throws:
      IOException
  • Method Details

    • getInputStream

      public InputStream getInputStream() throws IOException
      Gets the contents of the Resource as an InputStream.
      Overrides:
      getInputStream in class Resource
      Returns:
      The contents of the Resource.
      Throws:
      IOException
    • initialize

      public void initialize() throws IOException
      Initializes the resource by loading its data into memory.
      Throws:
      IOException
    • getData

      public byte[] getData() throws IOException
      The contents of the resource as a byte[] If this resource was lazy-loaded and the data was not yet loaded, it will be loaded into memory at this point. This included opening the zip file, so expect a first load to be slow.
      Overrides:
      getData in class Resource
      Returns:
      The contents of the resource
      Throws:
      IOException
    • getResourceStream

      private InputStream getResourceStream() throws FileNotFoundException, IOException
      Throws:
      FileNotFoundException
      IOException
    • close

      public void close()
      Tells this resource to release its cached data. If this resource was not lazy-loaded, this is a no-op.
      Overrides:
      close in class Resource
    • isInitialized

      public boolean isInitialized()
      Returns if the data for this resource has been loaded into memory.
      Returns:
      true if data was loaded.
    • getSize

      public long getSize()
      Returns the size of this resource in bytes.
      Overrides:
      getSize in class Resource
      Returns:
      the size.