Class InspectableFileCachedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- it.unimi.dsi.fastutil.io.MeasurableInputStream
-
- it.unimi.dsi.fastutil.io.InspectableFileCachedInputStream
-
- All Implemented Interfaces:
MeasurableStream
,RepositionableStream
,Closeable
,AutoCloseable
,Channel
,WritableByteChannel
public class InspectableFileCachedInputStream extends MeasurableInputStream implements RepositionableStream, WritableByteChannel
A repositionableMeasurableInputStream
based on cached data received by aWritableByteChannel
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 anOutputStream
usingChannels.newOutputStream(WritableByteChannel)
). The data can be discarded at any time usingclear()
. The firstinspectable
bytes ofbuffer
contains the first bytes written. Whenbuffer
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 implementedMeasurableInputStream
which also implementsRepositionableStream
and supports marking. Note that you must arbitrate carefully write and read accesses, as it is always possible to callwrite(ByteBuffer)
and thus modify the length of theMeasurableInputStream
.The method
close()
makes theMeasurableInputStream
andWritableByteChannel
state-changing methods temporarily throw anIOException
, but does not otherwise modify the state of the stream. You can reopen the stream later, or clear it. The methoddispose()
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 aRandomAccessFile
.
-
-
Field Summary
Fields Modifier and Type Field Description byte[]
buffer
The inspection buffer.static boolean
DEBUG
static int
DEFAULT_BUFFER_SIZE
The default buffer size (64KiB).int
inspectable
The 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, File overflowFile)
Creates a new instance with specified buffer size and overlow-file directory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
clear()
Clears the content of thisInspectableFileCachedInputStream
, zeroing the length of the represented stream.void
close()
Makes the stream unreadable until the nextclear()
.void
dispose()
Disposes this stream, deleting the overflow file.boolean
isOpen()
long
length()
Returns the overall length of this stream (optional operation).void
mark(int readlimit)
boolean
markSupported()
long
position()
Returns the current stream 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 aclose()
.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.-
Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
DEBUG
public static final boolean DEBUG
- See Also:
- Constant Field Values
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size (64KiB).- See Also:
- Constant Field Values
-
buffer
public final byte[] buffer
The inspection buffer. The firstinspectable
bytes contain the first part of the input stream. The buffer is available for inspection, but users should not modify its content.
-
inspectable
public int inspectable
The number of valid bytes currently inbuffer
.
-
-
Constructor Detail
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream(int bufferSize, File overflowFile) throws IOException
Creates 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, ornull
for the default temporary directory.- Throws:
IOException
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream(int bufferSize) throws IOException
Creates a new instance with specified buffer size and default overflow-file directory.- Parameters:
bufferSize
- the buffer size, in bytes.- Throws:
IOException
-
InspectableFileCachedInputStream
public InspectableFileCachedInputStream() throws IOException
Creates a new instance with default buffer size and overflow-file directory.- Throws:
IOException
-
-
Method Detail
-
clear
public void clear() throws IOException
Clears the content of thisInspectableFileCachedInputStream
, zeroing the length of the represented stream.- Throws:
IOException
-
write
public int write(ByteBuffer byteBuffer) throws IOException
Appends the content of a specified buffer to the end of the currently represented stream.- Specified by:
write
in interfaceWritableByteChannel
- Parameters:
byteBuffer
- a byte buffer.- Returns:
- the number of bytes appended (i.e.,
byteBuffer.remaining()
). - Throws:
IOException
-
truncate
public void truncate(long size) throws FileNotFoundException, IOException
Truncates 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:
FileNotFoundException
IOException
-
close
public void close()
Makes the stream unreadable until the nextclear()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- See Also:
reopen()
-
reopen
public void reopen() throws IOException
Makes the stream readable again after aclose()
.- Throws:
IOException
- See Also:
close()
-
dispose
public void dispose() throws IOException
Disposes this stream, deleting the overflow file. After that, the stream is unusable.- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b, int offset, int length) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
length
public long length() throws IOException
Description copied from interface:MeasurableStream
Returns 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:
length
in interfaceMeasurableStream
- Throws:
IOException
-
position
public long position() throws IOException
Description copied from interface:RepositionableStream
Returns the current stream position.- Specified by:
position
in interfaceMeasurableStream
- Specified by:
position
in interfaceRepositionableStream
- Returns:
- the current stream position.
- Throws:
IOException
-
position
public void position(long position) throws IOException
Positions the input stream.- Specified by:
position
in interfaceRepositionableStream
- Parameters:
position
- the new position (will be minimized withlength()
).- Throws:
IOException
-
mark
public void mark(int readlimit)
- Overrides:
mark
in classInputStream
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classInputStream
-
-