Package net.sf.basedb.util
Class StreamCacher
java.lang.Object
net.sf.basedb.util.StreamCacher
- All Implemented Interfaces:
AutoCloseable
Caches the contents of a stream to a local temporary file. The
getNewStream()
method will create a stream that appears
to be reading from the start of the master stream, except that
it will read cached data to begin with and continue with the master
stream only if needed.
This class is useful when you know (or suspect) that you need to read the same stream multiple times, but the stream may be "expensive" to retrieve, either in terms of network capacity or cpu processing.
- Since:
- 2.16
- Author:
- Nicklas
- See Also:
- Last modified
- $Date: 2018-12-12 15:41:10 +0100 (on, 12 dec 2018) $
-
Field Summary
Modifier and TypeFieldDescriptionprivate final File
private final OutputStream
private final InputStream
private final InputStream
-
Constructor Summary
ConstructorDescriptionStreamCacher
(InputStream master) Create a new stream cacher for reading from the given master stream.StreamCacher
(InputStream master, File cacheFile) Create a new stream cacher for reading from the given master stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the master stream and remove the cached file data.Get a new stream that appears to be reading from the start of the master stream.
-
Field Details
-
master
-
splitter
-
cacheFile
-
cacheOut
-
-
Constructor Details
-
StreamCacher
Create a new stream cacher for reading from the given master stream.- Parameters:
master
- The master input stream- Throws:
IOException
-
StreamCacher
Create a new stream cacher for reading from the given master stream.- Parameters:
master
- The master input streamcacheFile
- Use this file for the cached data, if the file already exist it will be overwritten, if this parameter is null a random file will be created in the default temporary directory- Throws:
IOException
-
-
Method Details
-
close
public void close()Close the master stream and remove the cached file data.- Specified by:
close
in interfaceAutoCloseable
-
getNewStream
Get a new stream that appears to be reading from the start of the master stream. The new stream will read cached data (if any), and only when the end of that data is reached it will start reading from the master stream. The data from the master stream is automatically copied to the cache.- Returns:
- An Input stream
- Throws:
IOException
-