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

java.lang.Object
  extended by com.sun.labs.minion.util.buffer.StdReadableImpl
      extended by com.sun.labs.minion.util.buffer.ChannelReadableBuffer
All Implemented Interfaces:
Buffer, ReadableBuffer

public class ChannelReadableBuffer
extends StdReadableImpl

A readable buffer that's backed by a channel and an in-memory buffer.


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  java.nio.ByteBuffer buff
          The in-memory buffer.
protected  java.nio.channels.FileChannel chan
          The channel containing the 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.
 
Constructor Summary
ChannelReadableBuffer(java.nio.channels.FileChannel chan, long offset, int limit, int buffSize)
          Creates a buffer backed by a channel.
ChannelReadableBuffer(java.io.RandomAccessFile raf, long offset, int limit)
          Creates a buffer backed by a channel.
ChannelReadableBuffer(java.io.RandomAccessFile raf, long offset, int limit, int buffSize)
          Creates a buffer backed by a channel.
 
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 channel.
 int remaining()
          Gets the number of bytes remaining to be read.
 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

chan

protected java.nio.channels.FileChannel chan
The channel 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 java.nio.ByteBuffer 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

ChannelReadableBuffer

public ChannelReadableBuffer(java.io.RandomAccessFile raf,
                             long offset,
                             int limit)
Creates a buffer backed by a channel.

Parameters:
raf - The file that will provide the channel backing this buffer.
offset - The offset in the file where our buffer is located.
limit - The number of bytes in our buffer.

ChannelReadableBuffer

public ChannelReadableBuffer(java.io.RandomAccessFile raf,
                             long offset,
                             int limit,
                             int buffSize)
Creates a buffer backed by a channel.

Parameters:
buffSize - The size of the in-memory buffer to use.
raf - The file that will provide the channel backing this buffer.
offset - The offset in the file where our buffer is located.
limit - The number of bytes in our buffer.

ChannelReadableBuffer

public ChannelReadableBuffer(java.nio.channels.FileChannel chan,
                             long offset,
                             int limit,
                             int buffSize)
Creates a buffer backed by a channel.

Parameters:
chan - The channel backing our buffer.
buffSize - The size of the in-memory buffer to use.
offset - The offset in the file where our buffer is located.
limit - The number of bytes in our buffer.
Method Detail

read

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

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

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()
Gets the number of bytes remaining to be read.

Returns:
The number of bytes remaining to be read.

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 that is backed by the same channel. The in-memory buffer and position are independent in the new buffer.

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 backed by the same channel. The first position in the new buffer will be the given position and the new buffer will contain the given number of bytes.

limit

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

Returns:
The last readable position in the buffer.

limit

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

Parameters:
l - The limit to set.

get

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

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

get

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

Returns:
Gets the byte at the current position and advances the current position.

position

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

Returns:
The current position of the buffer.

position

public void position(int i)
Positions the buffer.

Parameters:
i - The position to which we want to 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.