Package nl.siegmann.epublib.domain
Class LazyResource
java.lang.Object
nl.siegmann.epublib.domain.Resource
nl.siegmann.epublib.domain.LazyResource
- All Implemented Interfaces:
Serializable
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 Summary
FieldsModifier and TypeFieldDescriptionprivate longprivate Stringprivate static final org.slf4j.Loggerprivate static final longFields inherited from class nl.siegmann.epublib.domain.Resource
data, originalHref -
Constructor Summary
ConstructorsConstructorDescriptionLazyResource(InputStream in, String filename, int length, String href) Creates a Resource that tries to load the data, but falls back to lazy loading.LazyResource(String filename, long size, String href) Creates a Lazy resource, by not actually loading the data for this entry. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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.Gets the contents of the Resource as an InputStream.private InputStreamlonggetSize()Returns the size of this resource in bytes.voidInitializes the resource by loading its data into memory.booleanReturns 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 Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
filename
-
cachedSize
private long cachedSize -
LOG
private static final org.slf4j.Logger LOG
-
-
Constructor Details
-
LazyResource
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
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
Gets the contents of the Resource as an InputStream.- Overrides:
getInputStreamin classResource- Returns:
- The contents of the Resource.
- Throws:
IOException
-
initialize
Initializes the resource by loading its data into memory.- Throws:
IOException
-
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. This included opening the zip file, so expect a first load to be slow.- Overrides:
getDatain classResource- Returns:
- The contents of the resource
- Throws:
IOException
-
getResourceStream
- Throws:
FileNotFoundExceptionIOException
-
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.
-
getSize
public long getSize()Returns the size of this resource in bytes.
-