public class StreamCacher
extends java.lang.Object
implements java.lang.AutoCloseable
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 java.io.File |
cacheFile |
private java.io.OutputStream |
cacheOut |
private java.io.InputStream |
master |
private java.io.InputStream |
splitter |
Constructor and Description |
---|
StreamCacher(java.io.InputStream master)
Create a new stream cacher for reading from the given master stream.
|
StreamCacher(java.io.InputStream master,
java.io.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.
|
java.io.InputStream |
getNewStream()
Get a new stream that appears to be reading from the start of the
master stream.
|
private final java.io.InputStream master
private final java.io.InputStream splitter
private final java.io.File cacheFile
private final java.io.OutputStream cacheOut
public StreamCacher(java.io.InputStream master) throws java.io.IOException
master
- The master input streamjava.io.IOException
public StreamCacher(java.io.InputStream master, java.io.File cacheFile) throws java.io.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 directoryjava.io.IOException
public void close()
close
in interface java.lang.AutoCloseable
public java.io.InputStream getNewStream() throws java.io.IOException
java.io.IOException