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:
Last modified
$Date: 2018-12-12 15:41:10 +0100 (on, 12 dec 2018) $
  • Field Details

  • Constructor Details

    • 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 Details

    • close

      public void close()
      Close the master stream and remove the cached file data.
      Specified by:
      close in interface AutoCloseable
    • 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