Class VisibleBufferedInputStream

java.lang.Object
java.io.InputStream
org.postgresql.core.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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    The buffer.
    private int
    How far is the buffer filled with valid data.
    private int
    Current read position in the buffer.
    private static final int
    If a direct read to byte array is called that would require a smaller read from the wrapped stream that MINIMUM_READ then first fill the buffer and serve the bytes from there.
    private static final int
    In how large spans is the C string zero-byte scanned.
    private boolean
    socket timeout has been requested
    private final InputStream
    The wrapped input stream.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new buffer around the given stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    void
    private void
    Compacts the unread bytes of the buffer to the beginning of the buffer.
    private void
    Doubles the size of the buffer.
    boolean
    ensureBytes(int n)
    Ensures that the buffer contains at least n bytes.
    boolean
    ensureBytes(int n, boolean block)
    Ensures that the buffer contains at least n bytes.
    byte[]
    Returns direct handle to the used buffer.
    int
    Returns the current read position in the buffer.
     
    private void
    moveBufferTo(byte[] dest)
    Moves bytes from the buffer to the beginning of the destination buffer.
    int
    Reads a byte from the buffer without advancing the index pointer.
    int
    int
    read(byte[] to, int off, int len)
    int
    Reads an int2 value from the underlying stream as an unsigned integer (0..65535).
    int
    Reads an int4 value from the underlying stream.
    private boolean
    readMore(int wanted, boolean block)
    Reads more bytes into the buffer.
    byte
    Reads byte from the buffer without any checks.
    int
    Scans the length of the next null terminated string (C-style string) from the stream.
    void
    setTimeoutRequested(boolean timeoutRequested)
     
    long
    skip(long n)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MINIMUM_READ

      private static final int MINIMUM_READ
      If a direct read to byte array is called that would require a smaller read from the wrapped stream that MINIMUM_READ then first fill the buffer and serve the bytes from there. Larger reads are directly done to the provided byte array.
      See Also:
    • STRING_SCAN_SPAN

      private static final int STRING_SCAN_SPAN
      In how large spans is the C string zero-byte scanned.
      See Also:
    • wrapped

      private final InputStream wrapped
      The wrapped input stream.
    • buffer

      private byte[] buffer
      The buffer.
    • index

      private int index
      Current read position in the buffer.
    • endIndex

      private int endIndex
      How far is the buffer filled with valid data.
    • timeoutRequested

      private boolean timeoutRequested
      socket timeout has been requested
  • Constructor Details

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

    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • readInt2

      public int readInt2() throws IOException
      Reads an int2 value from the underlying stream as an unsigned integer (0..65535).
      Returns:
      int2 in the range of 0..65535
      Throws:
      IOException - if an I/ O error occurs.
    • readInt4

      public int readInt4() throws IOException
      Reads an int4 value from the underlying stream.
      Returns:
      int4 value from the underlying stream
      Throws:
      IOException - if an I/ O error occurs.
    • 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 underlying 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.
    • readMore

      private boolean readMore(int wanted, boolean block) throws IOException
      Reads more bytes into the buffer.
      Parameters:
      wanted - How much should be at least read.
      Returns:
      True if at least some bytes were read.
      Throws:
      IOException - If reading of the wrapped stream failed.
    • doubleBuffer

      private void doubleBuffer()
      Doubles the size of the buffer.
    • compact

      private void compact()
      Compacts the unread bytes of the buffer to the beginning of the buffer.
    • moveBufferTo

      private void moveBufferTo(byte[] dest)
      Moves bytes from the buffer to the beginning of the destination buffer. Also sets the index and endIndex variables.
      Parameters:
      dest - The destination buffer.
    • read

      public int read(byte[] to, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • 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 underlying 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