Package org.postgresql.core
Class VisibleBufferedInputStream
java.lang.Object
java.io.InputStream
org.postgresql.core.VisibleBufferedInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
A faster version of BufferedInputStream. Does no synchronisation and allows direct access to the
used byte[] buffer.
- Author:
- Mikko Tiihonen
-
Constructor Summary
ConstructorsConstructorDescriptionVisibleBufferedInputStream
(InputStream in, int bufferSize) Creates a new buffer around the given stream. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
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
getIndex()
Returns the current read position in the buffer.Returns the underlying stream.int
peek()
Reads a byte from the buffer without advancing the index pointer.int
read()
int
read
(byte[] to, int off, int len) int
readInt2()
Reads an int2 value from the underlying stream as an unsigned integer (0..65535).int
readInt4()
Reads an int4 value from the underlying stream.byte
readRaw()
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.io.InputStream
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skipNBytes, transferTo
-
Constructor Details
-
VisibleBufferedInputStream
Creates a new buffer around the given stream.- Parameters:
in
- The stream to buffer.bufferSize
- The initial size of the buffer.
-
-
Method Details
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
readInt2
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
Reads an int4 value from the underlying stream.- Returns:
- int4 value from the underlying stream
- Throws:
IOException
- if an I/ O error occurs.
-
peek
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 theensureBytes(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
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
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 bufferblock
- 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.
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
getBuffer
public byte[] getBuffer()Returns direct handle to the used buffer. Use theensureBytes(int)
to prefill required bytes the buffer andgetIndex()
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
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
Returns the underlying stream.- Returns:
- the underlying stream
-