Class FileCachedInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Channel, WritableByteChannel

    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.

    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, as reads after the buffer has been exhausted will be performed directly on a RandomAccessFile.

    • Field Detail

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        The default buffer size (1024KiB).
        See Also:
        Constant Field Values
      • buffer

        public 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 Detail

      • FileCachedInputStream

        public FileCachedInputStream​(int bufferSize,
                                     File overflowFile)
        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.
      • FileCachedInputStream

        public FileCachedInputStream​(int bufferSize)
        Creates a new instance with specified buffer size and default overflow-file directory.
        Parameters:
        bufferSize - the buffer size, in bytes.
        Throws:
        IOException - if an I/O error occurs.
      • FileCachedInputStream

        public FileCachedInputStream()
        Creates a new instance with default buffer size and overflow-file directory.
        Throws:
        IOException - if an I/O error occurs.