public class StreamCacher extends Object
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.
File.getCachedDownloadStream()
Modifier and Type | Field and Description |
---|---|
private File |
cacheFile |
private OutputStream |
cacheOut |
private InputStream |
master |
private InputStream |
splitter |
Constructor and 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.
|
Modifier and Type | Method and 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.
|
private final InputStream master
private final InputStream splitter
private final File cacheFile
private final OutputStream cacheOut
public StreamCacher(InputStream master) throws IOException
master
- The master input streamIOException
public StreamCacher(InputStream master, File cacheFile) throws IOException
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 directoryIOException
public void close()
public InputStream getNewStream() throws IOException
IOException