com.sun.labs.minion.util.buffer
Class FileReadableBuffer

java.lang.Object
  extended by com.sun.labs.minion.util.buffer.StdReadableImpl
      extended by com.sun.labs.minion.util.buffer.FileReadableBuffer
All Implemented Interfaces:
Buffer, ReadableBuffer
Direct Known Subclasses:
FileBackedPostingsInput

public class FileReadableBuffer
extends StdReadableImpl

A buffer that can be used to read data from a file, keeping minimal amounts of data in memory.

This implementation is mainly used by the DiskDictionary so that only a small amount of dictionary information needs to be in main memory at any given time.


Field Summary
protected  long be
          The offset in the file of the end of the buffer that we represent.
protected  long bs
          The offset in the file of the buffer that we represent.
protected  byte[] buff
          The in-memory buffer.
protected static int DEFAULT_BUFF_SIZE
          The default buffer size, 1KB.
protected static java.lang.String logTag
          A tag for our log entries.
protected  long me
          The offset in the file of the end of our in-memory buffer.
protected  long ms
          The offset in the file of the start of our in-memory buffer.
protected  long pos
          The offset of the current position in the buffer we're representing.
protected  java.io.RandomAccessFile raf
          The file containing the buffer.
 
Constructor Summary
FileReadableBuffer(java.io.RandomAccessFile raf, long offset, int limit)
          Creates a readable buffer that is backed by the given file.
FileReadableBuffer(java.io.RandomAccessFile raf, long offset, int limit, int buffSize)
          Creates a readable buffer that is backed by the given file.
 
Method Summary
protected  int checkBounds(long p)
          Checks whether the given position is within the bounds of our in memory buffer.
 ReadableBuffer duplicate()
          Duplicates this buffer, so that it can be used safely by other readers.
 byte get()
          Gets the next byte in the buffer.
 byte get(int i)
          Gets the byte at the given position in the buffer.
 int limit()
          Gets the limit of this buffer, i.e., the last readable position.
 void limit(int l)
          Sets the limit of this buffer, i.e., the last readable position.
 int position()
          Gets the position of the buffer.
 void position(int i)
          Positions the buffer.
protected  int read(long off)
          Reads a given number of bytes from the file.
 int remaining()
          Returns the number of bytes remaining to be read in the buffer.
 ReadableBuffer slice(int p, int s)
          Slices this buffer so that a sub-buffer can be used.
 java.lang.String toString()
          Gets a string representation of the buffer.
 
Methods inherited from class com.sun.labs.minion.util.buffer.StdReadableImpl
byteDecode, byteDecode, byteDecode, byteDecodeLong, byteDecodeLong, byteDecodeLong, countBits, decodeFloat, getString, skipByteEncoded, test, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

raf

protected java.io.RandomAccessFile raf
The file containing the buffer.


ms

protected long ms
The offset in the file of the start of our in-memory buffer.


me

protected long me
The offset in the file of the end of our in-memory buffer.


bs

protected long bs
The offset in the file of the buffer that we represent.


be

protected long be
The offset in the file of the end of the buffer that we represent.


pos

protected long pos
The offset of the current position in the buffer we're representing.


buff

protected byte[] buff
The in-memory buffer.


logTag

protected static java.lang.String logTag
A tag for our log entries.


DEFAULT_BUFF_SIZE

protected static final int DEFAULT_BUFF_SIZE
The default buffer size, 1KB.

See Also:
Constant Field Values
Constructor Detail

FileReadableBuffer

public FileReadableBuffer(java.io.RandomAccessFile raf,
                          long offset,
                          int limit)
Creates a readable buffer that is backed by the given file. The buffer starts at the given offset in the file and extends for the given number of bytes. The buffer will use the default buffer size for the in-memory buffer.

Parameters:
raf - The file containing the data for our buffer.
offset - The offset in the file where the data for our buffer can be found.
limit - The number of bytes of data in our buffer.

FileReadableBuffer

public FileReadableBuffer(java.io.RandomAccessFile raf,
                          long offset,
                          int limit,
                          int buffSize)
Creates a readable buffer that is backed by the given file. The buffer starts at the given offset in the file and extends for the given number of bytes. The given buffer size will be used for the in-memory buffer.

Parameters:
raf - The file containing the data for our buffer.
offset - The offset in the file where the data for our buffer can be found.
limit - The number of bytes of data in our buffer.
buffSize - The size of the in-memory buffer to use.
Method Detail

read

protected int read(long off)
Reads a given number of bytes from the file.

Parameters:
off - The offset in the file from which the bytes should be read.
Returns:
The number of bytes actually read from the file.

checkBounds

protected int checkBounds(long p)
Checks whether the given position is within the bounds of our in memory buffer. If it's not, data will be read into the in-memory buffer.

Parameters:
p - The position that we want to check
Returns:
The index in the in-memory buffer that can be read for this position.

remaining

public int remaining()
Returns the number of bytes remaining to be read in the buffer.

Returns:
The number of bytes remaining in the buffer.

duplicate

public ReadableBuffer duplicate()
Duplicates this buffer, so that it can be used safely by other readers. Note that this duplicates the data in the buffer, since it must not be changed by subsequent reads.

Returns:
A new buffer duplicating the contents of this buffer. The buffers are backed by the same underlying file, but they have different in-memory buffers and positions.

slice

public ReadableBuffer slice(int p,
                            int s)
Slices this buffer so that a sub-buffer can be used. The buffer is sliced from the current position. Note that this actually duplicates the data in the buffer, since it must not be changed by subsequent reads.

Parameters:
p - The position at which the buffer should be sliced.
s - The number of bytes that should be in the sliced buffer.
Returns:
A new buffer containing a slice of this buffer. The new buffer shares the underlying file, but has it's own in-memory buffer. The first position in the sliced buffer is the given position, and the limit on the sliced buffer is the given size.

limit

public int limit()
Gets the limit of this buffer, i.e., the last readable position.

Returns:
The last readable position in this buffer.

limit

public void limit(int l)
Sets the limit of this buffer, i.e., the last readable position.

Parameters:
l - The limit that we wish to set for the buffer.

get

public byte get(int i)
Gets the byte at the given position in the buffer.

Parameters:
i - The position from which we wish to get a byte.
Returns:
The byte at the given position.

get

public byte get()
Gets the next byte in the buffer.

Returns:
The byte at the current buffer position. This will advance the current position.

position

public int position()
Gets the position of the buffer.

Returns:
The current position in the buffer.

position

public void position(int i)
Positions the buffer.

Parameters:
i - The position to which we should set the buffer.

toString

public java.lang.String toString()
Gets a string representation of the buffer.

Overrides:
toString in class StdReadableImpl
Returns:
A string representation of the buffer.