Class InspectableFileCachedInputStream
- All Implemented Interfaces:
MeasurableStream,RepositionableStream,java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.WritableByteChannel
public class InspectableFileCachedInputStream extends MeasurableInputStream implements RepositionableStream, java.nio.channels.WritableByteChannel
MeasurableInputStream based on
cached data received by a WritableByteChannel whose first bytes can be inspected directly.
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.
At any time, the stream of bytes written since creation (or since the last clear())
are available as a fully implemented MeasurableInputStream which also implements
RepositionableStream and supports marking.
Note that you must arbitrate carefully write and read accesses,
as it is always possible to call write(ByteBuffer)
and thus modify the length of the MeasurableInputStream.
The method close() makes the MeasurableInputStream and WritableByteChannel state-changing methods temporarily throw an IOException, but
does not otherwise modify the state of the stream. You can reopen the stream
later, or clear it.
The method dispose() can be used to release
the resources associated with the stream.
Buffering
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 with a
FastBufferedInputStream, as reads after the buffer has been exhausted will be performed
directly on a RandomAccessFile.
-
Field Summary
Fields Modifier and Type Field Description byte[]bufferThe inspection buffer.static booleanDEBUGstatic intDEFAULT_BUFFER_SIZEThe default buffer size (64KiB).intinspectableThe number of valid bytes currently inbuffer. -
Constructor Summary
Constructors Constructor Description InspectableFileCachedInputStream()Creates a new instance with default buffer size and overflow-file directory.InspectableFileCachedInputStream(int bufferSize)Creates a new instance with specified buffer size and default overflow-file directory.InspectableFileCachedInputStream(int bufferSize, java.io.File overflowFile)Creates a new instance with specified buffer size and overlow-file directory. -
Method Summary
Modifier and Type Method Description intavailable()voidclear()Clears the content of thisInspectableFileCachedInputStream, zeroing the length of the represented stream.voidclose()Makes the stream unreadable until the nextclear().voiddispose()Disposes this stream, deleting the overflow file.booleanisOpen()longlength()Returns the overall length of this stream (optional operation).voidmark(int readlimit)booleanmarkSupported()longposition()Returns the current stream position.voidposition(long position)Positions the input stream.intread()intread(byte[] b)intread(byte[] b, int offset, int length)voidreopen()Makes the stream readable again after aclose().voidreset()longskip(long n)voidtruncate(long size)Truncates the overflow file to a given size if possible.intwrite(java.nio.ByteBuffer byteBuffer)Appends the content of a specified buffer to the end of the currently represented stream.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferToMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
DEBUG
public static final boolean DEBUG- See Also:
- Constant Field Values
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEThe default buffer size (64KiB).- See Also:
- Constant Field Values
-
buffer
public final byte[] bufferThe inspection buffer. The firstinspectablebytes contain the first part of the input stream. The buffer is available for inspection, but users should not modify its content. -
inspectable
public int inspectableThe number of valid bytes currently inbuffer.
-
-
Constructor Details
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream(int bufferSize, java.io.File overflowFile) throws java.io.IOExceptionCreates a new instance with specified buffer size and overlow-file directory.- Parameters:
bufferSize- the buffer size, in bytes.overflowFile- the directory where the overflow file should be created, ornullfor the default temporary directory.- Throws:
java.io.IOException
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream(int bufferSize) throws java.io.IOExceptionCreates a new instance with specified buffer size and default overflow-file directory.- Parameters:
bufferSize- the buffer size, in bytes.- Throws:
java.io.IOException
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream() throws java.io.IOExceptionCreates a new instance with default buffer size and overflow-file directory.- Throws:
java.io.IOException
-
-
Method Details
-
clear
public void clear() throws java.io.IOExceptionClears the content of thisInspectableFileCachedInputStream, zeroing the length of the represented stream.- Throws:
java.io.IOException
-
write
public int write(java.nio.ByteBuffer byteBuffer) throws java.io.IOExceptionAppends the content of a specified buffer to the end of the currently represented stream.- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Parameters:
byteBuffer- a byte buffer.- Returns:
- the number of bytes appended (i.e.,
byteBuffer.remaining()). - Throws:
java.io.IOException
-
truncate
public void truncate(long size) throws java.io.FileNotFoundException, java.io.IOExceptionTruncates the overflow file to a given size if possible.- Parameters:
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.- Throws:
java.io.FileNotFoundExceptionjava.io.IOException
-
close
public void close()Makes the stream unreadable until the nextclear().- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- See Also:
reopen()
-
reopen
public void reopen() throws java.io.IOExceptionMakes the stream readable again after aclose().- Throws:
java.io.IOException- See Also:
close()
-
dispose
public void dispose() throws java.io.IOExceptionDisposes this stream, deleting the overflow file. After that, the stream is unusable.- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int offset, int length) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
length
public long length() throws java.io.IOExceptionDescription copied from interface:MeasurableStreamReturns the overall length of this stream (optional operation). In most cases, this will require the stream to perform some extra action, possibly changing the state of the input stream itself (typically, reading all the bytes up to the end, or flushing on output stream). Implementing classes should always document what state will the input stream be in after calling this method, and which kind of exception could be thrown.- Specified by:
lengthin interfaceMeasurableStream- Throws:
java.io.IOException
-
position
public long position() throws java.io.IOExceptionDescription copied from interface:RepositionableStreamReturns the current stream position.- Specified by:
positionin interfaceMeasurableStream- Specified by:
positionin interfaceRepositionableStream- Returns:
- the current stream position.
- Throws:
java.io.IOException
-
position
public void position(long position) throws java.io.IOExceptionPositions the input stream.- Specified by:
positionin interfaceRepositionableStream- Parameters:
position- the new position (will be minimized withlength()).- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()- Specified by:
isOpenin interfacejava.nio.channels.Channel
-
mark
public void mark(int readlimit)- Overrides:
markin classjava.io.InputStream
-
reset
public void reset() throws java.io.IOException- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException
-
markSupported
public boolean markSupported()- Overrides:
markSupportedin classjava.io.InputStream
-