it.unimi.dsi.mg4j.io
Class FastBufferedInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byit.unimi.dsi.mg4j.io.FastBufferedInputStream
All Implemented Interfaces:
RepositionableStream

public class FastBufferedInputStream
extends InputStream
implements RepositionableStream

Lightweight, unsynchronised, aligned input stream buffering class.

This class provides buffering for input streams, but it does so with purposes and an internal logic that are radically different from the ones adopted in BufferedInputStream.

There is no support for marking. All methods are unsychronised. Moreover, it is guaranteed that all reads performed by this class will be multiples of the given buffer size. If, for instance, you use the default buffer size, reads will be performed on the underlying input stream in multiples of 16384 bytes. This is very important on operating systems that optimise disk reads on disk block boundaries.

As an additional feature, this class implements the RepositionableStream interface. An instance of this class will try to cast the underlying byte stream to a RepositionableStream and to fetch by reflection the FileChannel underlying the given output stream, in this order. If either reference can be successfully fetched, you can use position(long) to reposition the stream. Note that even in this case, it is still guaranteed that all reads will be performed on buffer boundaries, that is, as if the stream was divided in blocks of the size of the buffer.


Field Summary
protected  int avail
          The number of buffer bytes available starting from pos.
protected  byte[] buffer
          The internal buffer.
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes (16Ki).
protected  InputStream is
          The underlying input stream.
protected  int pos
          The current position in the buffer.
 
Constructor Summary
FastBufferedInputStream(InputStream is)
          Creates a new fast buffered input stream by wrapping a given input stream with a buffer of DEFAULT_BUFFER_SIZE bytes.
FastBufferedInputStream(InputStream is, int bufSize)
          Creates a new fast buffered input stream by wrapping a given input stream with a given buffer size.
 
Method Summary
 int available()
           
 void close()
           
 long position()
          Returns the current stream position.
 void position(long newPosition)
          Sets the current stream position.
 int read()
           
 int read(byte[] b, int offset, int length)
           
 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
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytes (16Ki).

See Also:
Constant Field Values

is

protected InputStream is
The underlying input stream.


buffer

protected byte[] buffer
The internal buffer.


pos

protected int pos
The current position in the buffer.


avail

protected int avail
The number of buffer bytes available starting from pos.

Constructor Detail

FastBufferedInputStream

public FastBufferedInputStream(InputStream is,
                               int bufSize)
Creates a new fast buffered input stream by wrapping a given input stream with a given buffer size.

Parameters:
is - an input stream to wrap.
bufSize - the size in bytes of the internal buffer.

FastBufferedInputStream

public FastBufferedInputStream(InputStream is)
Creates a new fast buffered input stream by wrapping a given input stream with a buffer of DEFAULT_BUFFER_SIZE bytes.

Parameters:
is - an input stream to wrap.
Method Detail

read

public int read()
         throws IOException
Throws:
IOException

read

public int read(byte[] b,
                int offset,
                int length)
         throws IOException
Throws:
IOException

position

public void position(long newPosition)
              throws IOException
Description copied from interface: RepositionableStream
Sets the current stream position.

Specified by:
position in interface RepositionableStream
Parameters:
newPosition - the new stream position.
Throws:
IOException

position

public long position()
              throws IOException
Description copied from interface: RepositionableStream
Returns the current stream position.

Specified by:
position in interface RepositionableStream
Returns:
the current stream position.
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Throws:
IOException

available

public int available()
              throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Throws:
IOException