public class FileCacheInputStream extends SeekableInputStream
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.sbitOffset, byteOrder, flushedPos
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this seekable input stream.
|
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.
|
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
available, markSupported
public FileCacheInputStream(InputStream is) throws IOException
is
- the input stream to cache.IOException
public FileCacheInputStream(InputStream is, File tmpFile, boolean removeOnExitOrClose) throws IOException
null
, a temporary file is created in the default system temp
directory. The read-ahead is configured using a system property.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.IOException
public FileCacheInputStream(InputStream is, File tmpFile, boolean removeOnExitOrClose, int readAhead) throws IOException
null
, a temporary file is created in the default system temp
directory.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)IllegalArgumentException
- if is
is null
IOException
public long getStreamPosition() throws IOException
IOException
ImageInputStream.getStreamPosition()
public long length() throws IOException
SeekableInputStream
length
in interface ImageInputStream
length
in class SeekableInputStream
IOException
- if an I/O error occurs.ImageInputStream.length()
public int read() throws IOException
SeekableInputStream
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.
read
in interface ImageInputStream
read
in class SeekableInputStream
-1
if EOF is reached.IOException
- if the stream has been closed.InputStream.read()
public void seek(long pos) throws IOException
SeekableInputStream
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.
seek
in interface ImageInputStream
seek
in class SeekableInputStream
pos
- a long
containing the desired file pointer position.IOException
- if any other I/O error occurs.SeekableInputStream.seek(long)
public void close() throws IOException
SeekableInputStream
IOException
s. Additional attempts to SeekableInputStream.close()
, however, have no effect.close
in interface Closeable
close
in interface AutoCloseable
close
in interface ImageInputStream
close
in class SeekableInputStream
IOException
InputStream.close()
public int read(byte[] b, int off, int len) throws IOException
SeekableInputStream
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.
read
in interface ImageInputStream
read
in class SeekableInputStream
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.-1
to indicate EOF.IOException
- if an I/O error occurs.InputStream.read(byte[], int, int)
public int read(byte[] b) throws IOException
SeekableInputStream
read(b, 0, b.length)
.
The bit offset within the stream is reset to zero before the read occurs.
read
in interface ImageInputStream
read
in class SeekableInputStream
b
- the byte buffer to fill up-1
to indicate EOF.IOException
- if an I/O error occurs.InputStream.read(byte[])
public boolean isCached()
true
since this ImageInputStream
caches data in order to
allow seeking backwards.isCached
in interface ImageInputStream
isCached
in class SeekableInputStream
true
.isCachedMemory()
,
isCachedFile()
public boolean isCachedFile()
true
since this ImageInputStream
maintains a file cache.isCachedFile
in interface ImageInputStream
isCachedFile
in class SeekableInputStream
true
.isCached()
,
isCachedMemory()
public boolean isCachedMemory()
false
since this ImageInputStream
does not maintain a main
memory cache.isCachedMemory
in interface ImageInputStream
isCachedMemory
in class SeekableInputStream
false
.isCached()
,
isCachedFile()
public long getSizeEstimate()
SeekableInputStream
getSizeEstimate
in class SeekableInputStream
public static void setDeleteStaleCacheFiles(boolean deleteStaleCacheFiles)
true
, stale files are expunged if they are unmodified since
getCacheFileTimeToLiveSeconds()
seconds.public static boolean isDeleteStaleCacheFiles()
public static void setCacheFileTimeToLiveSeconds(int cacheFileTimeToLiveSeconds)
public static int getCacheFileTimeToLiveSeconds()
public static void setDefaultReadAhead(int defaultReadAhead)
public static int getDefaultReadAhead()
public static void setDefaultCachePath(String defaultCachePath)
defaultCachePath
- the path name, or null
if the default temporary path
should be used.public static String getDefaultCachePath()
null
if the default temporary path should be used.Copyright © 2024 levigo holding gmbh. All rights reserved.