2.17.2: 2011-06-17

net.sf.basedb.util
Class StreamCacher

java.lang.Object
  extended by net.sf.basedb.util.StreamCacher

public class StreamCacher
extends Object

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: 2010-05-27 10:54:05 +0200 (Thu, 27 May 2010) $

Field Summary
private  File cacheFile
           
private  OutputStream cacheOut
           
private  InputStream master
           
private  InputStream splitter
           
 
Constructor Summary
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
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

master

private final InputStream master

splitter

private final InputStream splitter

cacheFile

private final File cacheFile

cacheOut

private final OutputStream cacheOut
Constructor Detail

StreamCacher

public StreamCacher(InputStream master)
             throws IOException
Create a new stream cacher for reading from the given master stream.

Parameters:
master - The master input stream
Throws:
IOException

StreamCacher

public StreamCacher(InputStream master,
                    File cacheFile)
             throws IOException
Create a new stream cacher for reading from the given master stream.

Parameters:
master - The master input stream
cacheFile - 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 Detail

close

public void close()
Close the master stream and remove the cached file data.


getNewStream

public InputStream getNewStream()
                         throws IOException
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

2.17.2: 2011-06-17