A
RepositionableStream
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
#mark(int).
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.