jut.io
Class InputStreamCache

java.lang.Object
  extended by java.io.InputStream
      extended by jut.io.InputStreamCache
All Implemented Interfaces:
Closeable

public class InputStreamCache
extends InputStream

InputStream caching the data read and providing them as new InputStream on request. caching is done in memory and/or in file.

Author:
Georg Dietrich

Nested Class Summary
static interface InputStreamCache.ByteArrayCache
          provider interface for large byte arrays that can be used as memory cache.
 
Field Summary
protected  byte[] cache
          the current memory cache or null if none.
protected  File cacheDir
          cache file directory, null for no file caching.
protected  File cacheFile
          cache file to use (null if none).
protected  OutputStream cacheFileOut
          cache file output stream (null if none).
protected  boolean cacheOK
          cache state of the stream.
protected  int cachePtr
          the current memory cache pointer. if cache is null, contains size of the last byte array cache used.
protected  ArrayList<byte[]> caches
          the memory caches used.
protected  InputStreamCache.ByteArrayCache cacheSource
          the source for memory cache arrays.
protected  boolean closed
          current state of the stream.
static InputStreamCache.ByteArrayCache defCacheSource
          default source for memory cache arrays, returning 1MB blocks if more than 10MB free memory is available.
protected  ParamRunnable<Iterable<byte[]>> overflowHandler
          overflow handler for memory-only stream caches or null if none.
protected  InputStream source
          the source input stream.
 
Constructor Summary
InputStreamCache(InputStream source)
          create a new InputStreamCache that only uses in-memory caching.
InputStreamCache(InputStream source, boolean fileCache)
          create a new InputStreamCache. as cache directory, the directory from the system property java.io.tmpdir is used.
InputStreamCache(InputStream source, File cacheDir)
          create a new InputStreamCache.
 
Method Summary
 int available()
          
 void close()
          
protected  void finalize()
          
 InputStream getData()
          returns an InputStream for the data cached. this method may only be called after calling close(). if the data was discarded because it didn't fit into the cache, returns null.
 int read()
          
 int read(byte[] buffer)
          
 int read(byte[] buffer, int off, int len)
          
 void release()
          release resources, i.e., release memory buffers and delete temp cache file. if the temp file exists but can not be deleted, a message is written to stderr. this may occur due to a couple of reasons - search the Internet e.g. for "java delete file system.gc".
 void setOverflowHandler(ParamRunnable<Iterable<byte[]>> overflowHandler)
          set the overflow handler to run if a memory-only stream cache surpasses its size limit.
protected  void write(byte[] data, int off, int len)
          write data into the internal cache if applicable.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defCacheSource

public static InputStreamCache.ByteArrayCache defCacheSource
default source for memory cache arrays, returning 1MB blocks if more than 10MB free memory is available.


cacheSource

protected final InputStreamCache.ByteArrayCache cacheSource
the source for memory cache arrays.


source

protected final InputStream source
the source input stream.


caches

protected final ArrayList<byte[]> caches
the memory caches used.


cache

protected byte[] cache
the current memory cache or null if none.


cachePtr

protected int cachePtr
the current memory cache pointer. if cache is null, contains size of the last byte array cache used.


closed

protected boolean closed
current state of the stream.


cacheOK

protected boolean cacheOK
cache state of the stream. false if data do not fit into cache.


cacheDir

protected File cacheDir
cache file directory, null for no file caching.


cacheFile

protected File cacheFile
cache file to use (null if none).


cacheFileOut

protected OutputStream cacheFileOut
cache file output stream (null if none).


overflowHandler

protected ParamRunnable<Iterable<byte[]>> overflowHandler
overflow handler for memory-only stream caches or null if none.

Constructor Detail

InputStreamCache

public InputStreamCache(InputStream source)
create a new InputStreamCache that only uses in-memory caching.

Parameters:
source - - the source input stream to use.

InputStreamCache

public InputStreamCache(InputStream source,
                        boolean fileCache)
create a new InputStreamCache. as cache directory, the directory from the system property java.io.tmpdir is used.

Parameters:
source - - the source input stream to use.
fileCache - - true if to use a file cache if necessary.

InputStreamCache

public InputStreamCache(InputStream source,
                        File cacheDir)
create a new InputStreamCache.

Parameters:
source - - the source input stream to use.
cacheDir - - the directory to store the cache temp file in.
Method Detail

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

read

public int read()
         throws IOException

Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] buffer)
         throws IOException

Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] buffer,
                int off,
                int len)
         throws IOException

Overrides:
read in class InputStream
Throws:
IOException

write

protected void write(byte[] data,
                     int off,
                     int len)
              throws IOException
write data into the internal cache if applicable.

Parameters:
data - - the data byte array.
off - - the data offset within the byte array.
len - - the length of the data in bytes.
Throws:
IOException - if problems occur while writing to the cache file.

setOverflowHandler

public void setOverflowHandler(ParamRunnable<Iterable<byte[]>> overflowHandler)
set the overflow handler to run if a memory-only stream cache surpasses its size limit.

Parameters:
overflowHandler - - the overflow handler to run if a memory-only stream cache surpasses its size limit.

release

public void release()
release resources, i.e., release memory buffers and delete temp cache file. if the temp file exists but can not be deleted, a message is written to stderr. this may occur due to a couple of reasons - search the Internet e.g. for "java delete file system.gc".


getData

public InputStream getData()
                    throws IOException,
                           IllegalStateException
returns an InputStream for the data cached. this method may only be called after calling close(). if the data was discarded because it didn't fit into the cache, returns null.

Returns:
an InputStream for the data cached. if the data was discarded because it didn't fit into the cache, returns null.
Throws:
IOException - if an I/O error occurs when opening the cache file.
IllegalStateException - if called for a stream that is not closed.

finalize

protected void finalize()
                 throws Throwable

Overrides:
finalize in class Object
Throws:
Throwable