Class LazyResource

  • All Implemented Interfaces:
    java.io.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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      LazyResource​(java.io.InputStream in, java.lang.String filename, int length, java.lang.String href)
      Creates a Resource that tries to load the data, but falls back to lazy loading.
      LazyResource​(java.lang.String filename, long size, java.lang.String href)
      Creates a Lazy resource, by not actually loading the data for this entry.
    • Field Detail

      • filename

        private java.lang.String filename
      • cachedSize

        private long cachedSize
      • LOG

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

      • LazyResource

        public LazyResource​(java.lang.String filename,
                            long size,
                            java.lang.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​(java.io.InputStream in,
                            java.lang.String filename,
                            int length,
                            java.lang.String href)
                     throws java.io.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 -
        fileName -
        length -
        href -
        Throws:
        java.io.IOException
    • Method Detail

      • getInputStream

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

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

        public byte[] getData()
                       throws java.io.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:
        java.io.IOException
      • getResourceStream

        private java.io.InputStream getResourceStream()
                                               throws java.io.FileNotFoundException,
                                                      java.io.IOException
        Throws:
        java.io.FileNotFoundException
        java.io.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.