Package net.sf.basedb.util
Class StreamCacher
java.lang.Object
net.sf.basedb.util.StreamCacher
- All Implemented Interfaces:
AutoCloseable
public class StreamCacher extends Object implements 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:
File.getCachedDownloadStream()
- Last modified
- $Date: 2018-12-12 15:41:10 +0100 (on, 12 dec 2018) $
-
Field Summary
Fields Modifier and Type Field Description private File
cacheFile
private OutputStream
cacheOut
private InputStream
master
private InputStream
splitter
-
Constructor Summary
Constructors Constructor Description StreamCacher(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 Type Method Description void
close()
Close the master stream and remove the cached file data.InputStream
getNewStream()
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
-