public class FileCachedInputStream extends InputStream implements WritableByteChannel
An instance of this class acts as a buffer holding the bytes written through
its WritableByteChannel interface (which can be easily turned into an
OutputStream using
Channels#newOutputStream(WritableByteChannel)). The data can be
discarded at any time using clear(). The first inspectable
bytes of buffer contains the first bytes written. When
buffer is full, the bytes are written to an overflow
file.
Note that you must arbitrate carefully write and read accesses, as it is
always possible to call write(ByteBuffer) and thus modify the
length
The method dispose() can be used to
release the resources associated with the stream.
This class provides no form of buffering except for the memory buffer
described above, both when reading and when writing. Users should consider
wrapping instances of this class, as reads after the buffer has been exhausted
will be performed directly on a RandomAccessFile.
| Modifier and Type | Field and Description |
|---|---|
byte[] |
buffer
The inspection buffer.
|
static boolean |
DEBUG |
static int |
DEFAULT_BUFFER_SIZE
The default buffer size (1024KiB).
|
int |
inspectable
The number of valid bytes currently in
buffer. |
| Constructor and Description |
|---|
FileCachedInputStream()
Creates a new instance with default buffer size and overflow-file
directory.
|
FileCachedInputStream(int bufferSize)
Creates a new instance with specified buffer size and default
overflow-file directory.
|
FileCachedInputStream(int bufferSize,
File overflowFile)
Creates a new instance with specified buffer size and overlow-file
directory.
|
| Modifier and Type | Method and Description |
|---|---|
int |
available() |
void |
clear()
Clears the content of this
FileCachedInputStream,
zeroing the length of the represented stream. |
void |
close()
Makes the stream unreadable until the next
clear(). |
void |
dispose()
Disposes this stream, deleting the overflow file.
|
boolean |
isOpen() |
long |
length() |
void |
mark(int readlimit) |
boolean |
markSupported() |
long |
position() |
void |
position(long position)
Positions the input stream.
|
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int offset,
int length) |
void |
reopen()
Makes the stream readable again after a
close(). |
void |
reset() |
long |
skip(long n) |
void |
truncate(long size)
Truncates the overflow file to a given size if possible.
|
int |
write(ByteBuffer byteBuffer)
Appends the content of a specified buffer to the end of the currently
represented stream.
|
public static final boolean DEBUG
public static final int DEFAULT_BUFFER_SIZE
public byte[] buffer
inspectable bytes contain the
first part of the input stream. The buffer is available for inspection,
but users should not modify its content.public int inspectable
buffer.public FileCachedInputStream(int bufferSize,
File overflowFile)
bufferSize - the buffer size, in bytes.overflowFile - the directory where the overflow file should be created, or
null for the default temporary directory.public FileCachedInputStream(int bufferSize)
bufferSize - the buffer size, in bytes.IOException - if an I/O error occurs.public FileCachedInputStream()
IOException - if an I/O error occurs.public void clear()
throws IOException
FileCachedInputStream,
zeroing the length of the represented stream.IOException - if an I/O error occurs.public int write(ByteBuffer byteBuffer) throws IOException
write in interface WritableByteChannelbyteBuffer - a byte buffer.byteBuffer.remaining()).IOExceptionpublic void truncate(long size)
throws FileNotFoundException,
IOException
size - the new size; the final size is the maximum between the
current write position (i.e., the length of the represented
stream minus the length of the inspection buffer) and this
value.FileNotFoundException - Tif the file cannot be found.IOException - if an I/O error occurs.public void close()
clear().close in interface Closeableclose in interface AutoCloseableclose in interface Channelclose in class InputStreamreopen()public void reopen()
throws IOException
close().IOException - if an I/O error occurs.close()public void dispose()
throws IOException
IOException - if an I/O error occurs.public int available()
throws IOException
available in class InputStreamIOExceptionpublic int read(byte[] b,
int offset,
int length)
throws IOException
read in class InputStreamIOExceptionpublic int read(byte[] b)
throws IOException
read in class InputStreamIOExceptionpublic long skip(long n)
throws IOException
skip in class InputStreamIOExceptionpublic int read()
throws IOException
read in class InputStreamIOExceptionpublic long length()
throws IOException
IOExceptionpublic long position()
throws IOException
IOExceptionpublic void position(long position)
throws IOException
position - the new position (will be minimized with length()).IOException - if an I/O error occurs.public void mark(int readlimit)
mark in class InputStreampublic void reset()
throws IOException
reset in class InputStreamIOExceptionpublic boolean markSupported()
markSupported in class InputStreamCopyright © 2021. All rights reserved.