com.levigo.jadice.document.io
Class FileCacheInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.levigo.jadice.document.io.SeekableInputStream
          extended by com.levigo.jadice.document.io.FileCacheInputStream
All Implemented Interfaces:
Closeable, DataInput, ImageInputStream

public class FileCacheInputStream
extends SeekableInputStream

An concrete SeekableInputStream which works by caching the data in a temporary file. Configurable: jadice.io.fileCacheInputStream.read-ahead integer 2048 Read ahead specifies the number of bytes that are read, in excess of the number of bytes that would need to be read during fill operations. Read ahead significantly reduces the IO overhead.s


Field Summary
 
Fields inherited from class com.levigo.jadice.document.io.SeekableInputStream
bitOffset, byteOrder, flushedPos
 
Constructor Summary
FileCacheInputStream(InputStream is)
          Create a new FileCacheInputStream which caches the given stream's data in a temporary file which is created in the default system temp directory.
FileCacheInputStream(InputStream is, File tmpFile, boolean removeOnExitOrClose)
          Create a new FileCacheInputStream which caches the given stream's data in the given temporary file.
FileCacheInputStream(InputStream is, File tmpFile, boolean removeOnExitOrClose, int readAhead)
          Create a new FileCacheInputStream which caches the given stream's data in the given temporary file.
 
Method Summary
 void close()
          Closes this seekable input stream.
protected  void finalize()
          Finalizes this object prior to garbage collection.
static int getCacheFileTimeToLiveSeconds()
          Get the time in seconds after which stale cache files are expunged.
static String getDefaultCachePath()
          Return the default cache path to use, of no explicit path is given.
static int getDefaultReadAhead()
          Return the default read-ahead
 long getSizeEstimate()
          A rough estimated size for this stream instance, which is used for internal cache management.
 long getStreamPosition()
           
 boolean isCached()
          Returns true since this ImageInputStream caches data in order to allow seeking backwards.
 boolean isCachedFile()
          Returns true since this ImageInputStream maintains a file cache.
 boolean isCachedMemory()
          Returns false since this ImageInputStream does not maintain a main memory cache.
static boolean isDeleteStaleCacheFiles()
          Return whether stale cache files are automatically expunged.
 long length()
          Returns the length of this stream contents, if available.
 int read()
          Reads a single byte from the stream and returns it as an int between 0 and 255.
 int read(byte[] b)
          A convenience method that calls read(b, 0, b.length).
 int read(byte[] b, int off, int len)
          Reads up to len bytes from the stream, and stores them into b starting at index off.
 void seek(long pos)
          Sets the current stream position, measured from the beginning of this data stream, at which the next read occurs.
static void setCacheFileTimeToLiveSeconds(int cacheFileTimeToLiveSeconds)
          Set the time in seconds after which stale cache files are expunged.
static void setDefaultCachePath(String defaultCachePath)
          Set the default cache path to use, of no explicit path is given.
static void setDefaultReadAhead(int defaultReadAhead)
          Set the default read-ahead in bytes.
static void setDeleteStaleCacheFiles(boolean deleteStaleCacheFiles)
          Set whether to automatically delete stale cache files left over by earlier instances of this class.
 
Methods inherited from class com.levigo.jadice.document.io.SeekableInputStream
checkClosed, flush, flushBefore, getBitOffset, getByteOrder, getFlushedPosition, mark, mark, readBit, readBits, readBoolean, readByte, readBytes, readChar, readDouble, readFloat, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readInt, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedInt, readUnsignedShort, readUTF, reset, resetToInitialState, setBitOffset, setByteOrder, skip, skipBytes, skipBytes
 
Methods inherited from class java.io.InputStream
available, markSupported
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileCacheInputStream

public FileCacheInputStream(InputStream is)
                     throws IOException
Create a new FileCacheInputStream which caches the given stream's data in a temporary file which is created in the default system temp directory. The file is removed if the application exits or the stream is closed.

Parameters:
is - the input stream to cache.
Throws:
IOException

FileCacheInputStream

public FileCacheInputStream(InputStream is,
                            File tmpFile,
                            boolean removeOnExitOrClose)
                     throws IOException
Create a new FileCacheInputStream which caches the given stream's data in the given temporary file. If the file points to a directory, a temporary file is created inside the directory. If the file is null, a temporary file is created in the default system temp directory. The read-ahead is configured using a system property.

Parameters:
is - the input stream to cache.
tmpFile - the file/directory to in which to hold the data.
removeOnExitOrClose - whether the file sould be removed on the application's exit or stream close.
Throws:
IOException

FileCacheInputStream

public FileCacheInputStream(InputStream is,
                            File tmpFile,
                            boolean removeOnExitOrClose,
                            int readAhead)
                     throws IOException
Create a new FileCacheInputStream which caches the given stream's data in the given temporary file. If the file points to a directory, a temporary file is created inside the directory. If the file is null, a temporary file is created in the default system temp directory.

Parameters:
is - the input stream to cache.
tmpFile - the file/directory in which to hold the data. If this is null, a temp file is automatically generated.
removeOnExitOrClose - whether the file sould be removed on the application's exit or stream close.
readAhead - The read-ahead to use (in bytes)
Throws:
IllegalArgumentException - if is is null
IOException
Method Detail

getStreamPosition

public long getStreamPosition()
                       throws IOException
Throws:
IOException
See Also:
ImageInputStream.getStreamPosition()

length

public long length()
            throws IOException
Description copied from class: SeekableInputStream
Returns the length of this stream contents, if available. Otherwise -1L will be returned.

Specified by:
length in interface ImageInputStream
Specified by:
length in class SeekableInputStream
Returns:
the length, measured in bytes or -1L if unknown.
Throws:
IOException - if an I/O error occurs.
See Also:
ImageInputStream.length()

read

public int read()
         throws IOException
Description copied from class: SeekableInputStream
Reads a single byte from the stream and returns it as an int between 0 and 255. If EOF is reached, -1 is returned.

Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

The bit offset within the stream must be reset to zero before the read occurs.

Specified by:
read in interface ImageInputStream
Specified by:
read in class SeekableInputStream
Returns:
the value of the next byte in the stream, or -1 if EOF is reached.
Throws:
IOException - if the stream has been closed.
See Also:
InputStream.read()

seek

public void seek(long pos)
          throws IOException
Description copied from class: SeekableInputStream
Sets the current stream position, measured from the beginning of this data stream, at which the next read occurs. The offset may be set beyond the end of this data stream. Setting the offset beyond the end does not change the data length, an EOFException will be thrown only if a read is performed. The bit offset is set to 0.

An IndexOutOfBoundsException will be thrown if pos is smaller than the flushed position (as returned by getflushedPosition).

It is legal to seek past the end of the file; an EOFException will be thrown only if a read is performed.

Specified by:
seek in interface ImageInputStream
Specified by:
seek in class SeekableInputStream
Parameters:
pos - a long containing the desired file pointer position.
Throws:
IOException - if any other I/O error occurs.
See Also:
SeekableInputStream.seek(long)

close

public void close()
           throws IOException
Description copied from class: SeekableInputStream
Closes this seekable input stream. Further attempts to read from it will throw IOExceptions. Additional attempts to SeekableInputStream.close(), however, have no effect.

Specified by:
close in interface Closeable
Specified by:
close in interface ImageInputStream
Overrides:
close in class SeekableInputStream
Throws:
IOException
See Also:
InputStream.close()

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Description copied from class: SeekableInputStream
Reads up to len bytes from the stream, and stores them into b starting at index off. If no bytes can be read because the end of the stream has been reached, -1 is returned.

The bit offset within the stream must be reset to zero before the read occurs.

Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

Specified by:
read in interface ImageInputStream
Specified by:
read in class SeekableInputStream
Parameters:
b - an array of bytes to be written to.
off - the starting position within b to write to.
len - the maximum number of bytes to read.
Returns:
the number of bytes actually read, or -1 to indicate EOF.
Throws:
IOException - if an I/O error occurs.
See Also:
InputStream.read(byte[], int, int)

read

public int read(byte[] b)
         throws IOException
Description copied from class: SeekableInputStream
A convenience method that calls read(b, 0, b.length).

The bit offset within the stream is reset to zero before the read occurs.

Specified by:
read in interface ImageInputStream
Overrides:
read in class SeekableInputStream
Parameters:
b - the byte buffer to fill up
Returns:
the number of bytes actually read, or -1 to indicate EOF.
Throws:
IOException - if an I/O error occurs.
See Also:
InputStream.read(byte[])

finalize

protected void finalize()
                 throws Throwable
Description copied from class: SeekableInputStream
Finalizes this object prior to garbage collection. The close method is called to close any open input source. This method should not be called from application code.

Overrides:
finalize in class SeekableInputStream
Throws:
Throwable - if an error occurs during superclass finalization.
See Also:
Object.finalize()

isCached

public boolean isCached()
Returns true since this ImageInputStream caches data in order to allow seeking backwards.

Specified by:
isCached in interface ImageInputStream
Overrides:
isCached in class SeekableInputStream
Returns:
true.
See Also:
isCachedMemory(), isCachedFile()

isCachedFile

public boolean isCachedFile()
Returns true since this ImageInputStream maintains a file cache.

Specified by:
isCachedFile in interface ImageInputStream
Overrides:
isCachedFile in class SeekableInputStream
Returns:
true.
See Also:
isCached(), isCachedMemory()

isCachedMemory

public boolean isCachedMemory()
Returns false since this ImageInputStream does not maintain a main memory cache.

Specified by:
isCachedMemory in interface ImageInputStream
Overrides:
isCachedMemory in class SeekableInputStream
Returns:
false.
See Also:
isCached(), isCachedFile()

getSizeEstimate

public long getSizeEstimate()
Description copied from class: SeekableInputStream
A rough estimated size for this stream instance, which is used for internal cache management.

Specified by:
getSizeEstimate in class SeekableInputStream
Returns:
an estimated size

setDeleteStaleCacheFiles

public static void setDeleteStaleCacheFiles(boolean deleteStaleCacheFiles)
Set whether to automatically delete stale cache files left over by earlier instances of this class. If true, stale files are expunged if they are unmodified since getCacheFileTimeToLiveSeconds() seconds.

Parameters:
deleteStaleCacheFiles -

isDeleteStaleCacheFiles

public static boolean isDeleteStaleCacheFiles()
Return whether stale cache files are automatically expunged.

Returns:
true if the automatic expunge is active

setCacheFileTimeToLiveSeconds

public static void setCacheFileTimeToLiveSeconds(int cacheFileTimeToLiveSeconds)
Set the time in seconds after which stale cache files are expunged.

Parameters:
cacheFileTimeToLiveSeconds -

getCacheFileTimeToLiveSeconds

public static int getCacheFileTimeToLiveSeconds()
Get the time in seconds after which stale cache files are expunged.

Returns:
the time to live in seconds

setDefaultReadAhead

public static void setDefaultReadAhead(int defaultReadAhead)
Set the default read-ahead in bytes.

Parameters:
defaultReadAhead -

getDefaultReadAhead

public static int getDefaultReadAhead()
Return the default read-ahead

Returns:
the number of bytes to read-ahead

setDefaultCachePath

public static void setDefaultCachePath(String defaultCachePath)
Set the default cache path to use, of no explicit path is given.

Parameters:
defaultCachePath - the path name, or null if the default temporary path should be used.

getDefaultCachePath

public static String getDefaultCachePath()
Return the default cache path to use, of no explicit path is given.

Returns:
the path name, or null if the default temporary path should be used.


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.