org.postgresql.core
Class VisibleBufferedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.postgresql.core.VisibleBufferedInputStream
All Implemented Interfaces:
java.io.Closeable

public class VisibleBufferedInputStream
extends java.io.InputStream

A faster version of BufferedInputStream. Does no synchronisation and allows direct access to the used byte[] buffer.

Author:
Mikko Tiihonen

Constructor Summary
VisibleBufferedInputStream(java.io.InputStream in, int bufferSize)
          Creates a new buffer around the given stream.
 
Method Summary
 int available()
          
 void close()
          
 boolean ensureBytes(int n)
          Ensures that the buffer contains at least n bytes.
 byte[] getBuffer()
          Returns direct handle to the used buffer.
 int getIndex()
          Returns the current read position in the buffer.
 int read()
          
 int read(byte[] to, int off, int len)
          
 byte readRaw()
          Reads byte from the buffer without any checks.
 int scanCStringLength()
          Scans the length of the next null terminated string (C-style string) from the stream.
 long skip(long n)
          
 
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VisibleBufferedInputStream

public VisibleBufferedInputStream(java.io.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

read

public int read()
         throws java.io.IOException

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

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:
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.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:
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:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException

Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.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 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 java.io.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:
java.io.IOException - If reading of stream fails.
EOFxception - If the stream did not contain any null terminators.