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 repositionable 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 Details

    • DEBUG

      public static final boolean DEBUG
      See Also:
    • DEFAULT_BUFFER_SIZE

      public static final int DEFAULT_BUFFER_SIZE
      The default buffer size (64KiB).
      See Also:
    • buffer

      public final byte[] buffer
      The inspection buffer. The first inspectable 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 in buffer.
  • Constructor Details

    • 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, or null 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 Details