Package org.postgresql.core
Class VisibleBufferedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.postgresql.core.VisibleBufferedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class VisibleBufferedInputStream extends java.io.InputStreamA faster version of BufferedInputStream. Does no synchronisation and allows direct access to the used byte[] buffer.- Author:
- Mikko Tiihonen
-
-
Constructor Summary
Constructors Constructor Description VisibleBufferedInputStream(java.io.InputStream in, int bufferSize)Creates a new buffer around the given stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()booleanensureBytes(int n)Ensures that the buffer contains at least n bytes.booleanensureBytes(int n, boolean block)Ensures that the buffer contains at least n bytes.byte[]getBuffer()Returns direct handle to the used buffer.intgetIndex()Returns the current read position in the buffer.java.io.InputStreamgetWrapped()Returns the underlying stream.intpeek()Reads a byte from the buffer without advancing the index pointer.intread()intread(byte[] to, int off, int len)intreadInt2()Reads an int2 value from the underlying stream as an unsigned integer (0..65535).intreadInt4()Reads an int4 value from the underlying stream.bytereadRaw()Reads byte from the buffer without any checks.intscanCStringLength()Scans the length of the next null terminated string (C-style string) from the stream.voidsetTimeoutRequested(boolean timeoutRequested)longskip(long n)
-
-
-
Method Detail
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
readInt2
public int readInt2() throws java.io.IOExceptionReads an int2 value from the underlying stream as an unsigned integer (0..65535).- Returns:
- int2 in the range of 0..65535
- Throws:
java.io.IOException- if an I/ O error occurs.
-
readInt4
public int readInt4() throws java.io.IOExceptionReads an int4 value from the underlying stream.- Returns:
- int4 value from the underlying stream
- Throws:
java.io.IOException- if an I/ O error occurs.
-
peek
public int peek() throws java.io.IOExceptionReads a byte from the buffer without advancing the index pointer.- Returns:
- byte from the buffer without advancing the index pointer
- Throws:
java.io.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:
java.lang.ArrayIndexOutOfBoundsException- If ensureBytes was not called to make sure the buffer contains the byte.
-
ensureBytes
public boolean ensureBytes(int n) throws java.io.IOExceptionEnsures 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:
java.io.IOException- If reading of the wrapped stream failed.
-
ensureBytes
public boolean ensureBytes(int n, boolean block) throws java.io.IOExceptionEnsures 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:
java.io.IOException- If reading of the wrapped stream failed.
-
read
public int read(byte[] to, int off, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.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
public int scanCStringLength() throws java.io.IOExceptionScans the length of the next null terminated string (C-style string) from the stream.- Returns:
- The length of the next null terminated string.
- Throws:
java.io.IOException- If reading of stream fails.java.io.EOFException- If the stream did not contain any null terminators.
-
setTimeoutRequested
public void setTimeoutRequested(boolean timeoutRequested)
-
getWrapped
public java.io.InputStream getWrapped()
Returns the underlying stream.- Returns:
- the underlying stream
-
-