Class VisibleBufferedInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class VisibleBufferedInputStream
    extends InputStream
    A faster version of BufferedInputStream. Does no synchronisation and allows direct access to the used byte[] buffer.
    Author:
    Mikko Tiihonen
    • Constructor Detail

      • VisibleBufferedInputStream

        public VisibleBufferedInputStream​(InputStream in,
                                          int bufferSize)
        Creates a new buffer around the given stream.
        Parameters:
        in - The stream to buffer.
        bufferSize - The initial size of the buffer.
    • Method Detail

      • peek

        public int peek()
                 throws IOException
        Reads a byte from the buffer without advancing the index pointer.
        Returns:
        byte from the buffer without advancing the index pointer
        Throws:
        IOException - if something wrong happens
      • readRaw

        public byte readRaw()
        Reads byte from the buffer without any checks. This method never reads from the underlaying stream. Before calling this method the ensureBytes(int) method must have been called.
        Returns:
        The next byte from the buffer.
        Throws:
        ArrayIndexOutOfBoundsException - If ensureBytes was not called to make sure the buffer contains the byte.
      • ensureBytes

        public boolean ensureBytes​(int n)
                            throws IOException
        Ensures that the buffer contains at least n bytes. This method invalidates the buffer and index fields.
        Parameters:
        n - The amount of bytes to ensure exists in buffer
        Returns:
        true if required bytes are available and false if EOF
        Throws:
        IOException - If reading of the wrapped stream failed.
      • ensureBytes

        public boolean ensureBytes​(int n,
                                   boolean block)
                            throws IOException
        Ensures that the buffer contains at least n bytes. This method invalidates the buffer and index fields.
        Parameters:
        n - The amount of bytes to ensure exists in buffer
        block - whether or not to block the IO
        Returns:
        true if required bytes are available and false if EOF or the parameter block was false and socket timeout occurred.
        Throws:
        IOException - If reading of the wrapped stream failed.
      • getBuffer

        public byte[] getBuffer()
        Returns direct handle to the used buffer. Use the ensureBytes(int) to prefill required bytes the buffer and getIndex() to fetch the current position of the buffer.
        Returns:
        The underlaying buffer.
      • getIndex

        public int getIndex()
        Returns the current read position in the buffer.
        Returns:
        the current read position in the buffer.
      • scanCStringLength

        public int scanCStringLength()
                              throws IOException
        Scans the length of the next null terminated string (C-style string) from the stream.
        Returns:
        The length of the next null terminated string.
        Throws:
        IOException - If reading of stream fails.
        EOFException - If the stream did not contain any null terminators.
      • setTimeoutRequested

        public void setTimeoutRequested​(boolean timeoutRequested)
      • getWrapped

        public InputStream getWrapped()
        Returns:
        the wrapped stream