edu.illinois.cs.cogcomp.lbj.coref.util.io
Class RandomAccessByteBuffer

java.lang.Object
  extended by edu.illinois.cs.cogcomp.lbj.coref.util.io.RandomAccessByteBuffer
All Implemented Interfaces:
RandomAccessData, RandomAccessInput

public class RandomAccessByteBuffer
extends java.lang.Object
implements RandomAccessInput

Loads and stores a buffer of bytes from a source such as a InputStream, and provides methods to read in a random access fashion. Suitable as a drop in replacement for a read-only RandomAccessFile, with any data source.


Field Summary
protected  byte[] bytes
           
protected  long cursor
           
 
Constructor Summary
RandomAccessByteBuffer(java.io.InputStream in)
          Creates a data-buffer by reading all input from the input stream, and storing it in a byte buffer.
 
Method Summary
 long getFilePointer()
          Returns the current offset in the data, in bytes.
 long length()
          Gets the number of bytes of data.
 int read()
          Reads a byte of data from the cursor position.
 void seek(long pos)
          Sets the offset in the data, in bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cursor

protected long cursor

bytes

protected byte[] bytes
Constructor Detail

RandomAccessByteBuffer

public RandomAccessByteBuffer(java.io.InputStream in)
Creates a data-buffer by reading all input from the input stream, and storing it in a byte buffer. Data is read until -1, the end of the stream, or an IOException is reached. This constructor blocks until all data has been read from the stream.

Parameters:
in - The stream from which to read the data.
Method Detail

getFilePointer

public long getFilePointer()
                    throws java.io.IOException
Description copied from interface: RandomAccessData
Returns the current offset in the data, in bytes.

Specified by:
getFilePointer in interface RandomAccessData
Returns:
The offset in bytes relative to the beginning of the data.
Throws:
java.io.IOException - if an I/O error is encountered.

seek

public void seek(long pos)
          throws java.io.IOException
Description copied from interface: RandomAccessData
Sets the offset in the data, in bytes. The offset may be set beyond the end of the data, and this does not change the data length. Only writing after setting the pointer beyond the end of the data will change the data size.

Specified by:
seek in interface RandomAccessData
Parameters:
pos - The desired offset, in bytes.
Throws:
java.io.IOException - if pos is negative, or if an I/O error is encountered.

length

public long length()
            throws java.io.IOException
Description copied from interface: RandomAccessData
Gets the number of bytes of data.

Specified by:
length in interface RandomAccessData
Returns:
The number of bytes of data.
Throws:
java.io.IOException - if an I/O error is encountered.

read

public int read()
Description copied from interface: RandomAccessInput
Reads a byte of data from the cursor position. The byte is returned as an integer in the inclusive range 0-255. This method blocks if no input is available. This method behaves just as InputStream's read method.

Specified by:
read in interface RandomAccessInput
Returns:
The next byte of data, or -1 if the end of file has been reached.