Package nl.siegmann.epublib.domain
Class LazyResource
- java.lang.Object
-
- nl.siegmann.epublib.domain.Resource
-
- nl.siegmann.epublib.domain.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
-
-
Field Summary
Fields Modifier and Type Field Description private longcachedSizeprivate java.lang.Stringfilenameprivate static org.slf4j.LoggerLOGprivate static longserialVersionUID-
Fields inherited from class nl.siegmann.epublib.domain.Resource
data, originalHref
-
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Tells this resource to release its cached data.byte[]getData()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.java.io.InputStreamgetInputStream()Gets the contents of the Resource as an InputStream.private java.io.InputStreamgetResourceStream()longgetSize()Returns the size of this resource in bytes.voidinitialize()Initializes the resource by loading its data into memory.booleanisInitialized()Returns if the data for this resource has been loaded into memory.-
Methods inherited from class nl.siegmann.epublib.domain.Resource
equals, getHref, getId, getInputEncoding, getMediaType, getReader, getTitle, hashCode, setData, setHref, setId, setInputEncoding, setMediaType, setTitle, toString
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
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.IOExceptionCreates 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.IOExceptionGets the contents of the Resource as an InputStream.- Overrides:
getInputStreamin classResource- Returns:
- The contents of the Resource.
- Throws:
java.io.IOException
-
initialize
public void initialize() throws java.io.IOExceptionInitializes the resource by loading its data into memory.- Throws:
java.io.IOException
-
getData
public byte[] getData() throws java.io.IOExceptionThe 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.
-
getResourceStream
private java.io.InputStream getResourceStream() throws java.io.FileNotFoundException, java.io.IOException- Throws:
java.io.FileNotFoundExceptionjava.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.
-
isInitialized
public boolean isInitialized()
Returns if the data for this resource has been loaded into memory.- Returns:
- true if data was loaded.
-
-