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

java.lang.Object
  extended by com.sun.labs.minion.util.buffer.StdBufferImpl
      extended by com.sun.labs.minion.util.buffer.NIOBuffer
All Implemented Interfaces:
Buffer, ReadableBuffer, WriteableBuffer

public class NIOBuffer
extends StdBufferImpl

A buffer that uses java.nio buffers as the backing store.

This implementation is suitable for use in postings types.

See Also:
Postings

Field Summary
protected  java.nio.ByteBuffer units
          A buffer backing our data.
 
Fields inherited from class com.sun.labs.minion.util.buffer.StdBufferImpl
masks, maxBEValues, maxValues, nBits
 
Constructor Summary
protected NIOBuffer()
          Creates a buffer that can be used when duplicating or slicing.
  NIOBuffer(java.nio.ByteBuffer units)
          Creates a buffer using the given buffer.
  NIOBuffer(int n)
          Creates a buffer with the given initial capacity.
  NIOBuffer(int n, boolean direct)
          Creates a buffer with the given initial capacity.
 
Method Summary
 WriteableBuffer append(ReadableBuffer b, int n)
          Appends a given number of bytes from a readable buffer onto this buffer.
protected  byte[] array()
          Gets an array of bytes from the units.
 int byteDecode()
          Decodes an integer stored using the 7 bit encoding.
 WriteableBuffer capacity(int n)
          Sets the capacity of the buffer to the given amount.
protected  void checkBounds(int p)
          Checks whether the given position is outside our array bounds.
 ReadableBuffer duplicate()
          Duplicates this buffer, so that it can be used safely by other readers.
 byte get()
          Gets a byte from this buffer.
 byte get(int i)
          Gets a byte from this buffer at the given position.
 ReadableBuffer getReadableBuffer()
          Gets a readable buffer from this writeable one.
 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.
static void main(java.lang.String[] args)
          A main program to test encoding and decoding of integers.
 WriteableBuffer or(ReadableBuffer b)
          Computes the logical OR of this buffer and another.
 int position()
          Gets the position in the buffer.
 void position(int position)
          Sets the position in the buffer.
 WriteableBuffer put(byte b)
          Puts a single byte onto this buffer.
 WriteableBuffer put(int p, byte b)
          Puts a single byte onto this buffer at the given position.
 int remaining()
          Gets the amount of space remaining in the buffer.
 ReadableBuffer slice(int p, int l)
          Slices this buffer so that a sub-buffer can be used.
 void write(java.nio.ByteBuffer b)
          Write the buffer to a new IO buffer.
 void write(java.io.DataOutput o)
          Write the buffer to a data output.
 void write(java.io.OutputStream os)
          Write the buffer to a stream.
 void write(java.nio.channels.WritableByteChannel chan)
          Write the buffer to a channel.
 WriteableBuffer xor(ReadableBuffer b)
          Computes the logical XOR of this buffer and another.
 
Methods inherited from class com.sun.labs.minion.util.buffer.StdBufferImpl
append, byteDecode, byteDecode, byteDecodeLong, byteDecodeLong, byteDecodeLong, byteEncode, byteEncode, byteEncode, bytesRequired, byteToBinaryString, clear, countBits, countBits, decodeFloat, encode, encode, getString, set, sizeUTF8, skipByteEncoded, test, toString, toString, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

units

protected java.nio.ByteBuffer units
A buffer backing our data.

Constructor Detail

NIOBuffer

protected NIOBuffer()
Creates a buffer that can be used when duplicating or slicing.


NIOBuffer

public NIOBuffer(int n)
Creates a buffer with the given initial capacity.

Parameters:
n - The initial capacity of the buffer.

NIOBuffer

public NIOBuffer(java.nio.ByteBuffer units)
Creates a buffer using the given buffer. This will duplicate the given buffer.

Parameters:
units - The buffer to duplicate and use as our internal representation.

NIOBuffer

public NIOBuffer(int n,
                 boolean direct)
Creates a buffer with the given initial capacity.

Parameters:
n - The initial capacity.
direct - If true, the buffer should be allocated as a direct buffer.
Method Detail

checkBounds

protected void checkBounds(int p)
Checks whether the given position is outside our array bounds. If it is, we expand so that it is not.

Parameters:
p - The position which we want to check.

position

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

Returns:
The position in the buffer.

position

public void position(int position)
Sets the position in the buffer.

Parameters:
position - The point to which the buffer's position should be set.

remaining

public int remaining()
Gets the amount of space remaining in the buffer.

Returns:
The number of bytes remaining in the buffer.

capacity

public WriteableBuffer capacity(int n)
Sets the capacity of the buffer to the given amount.

Parameters:
n - The number of bytes that the buffer must be able to store.
Returns:
The buffer, for chained invocations.

put

public WriteableBuffer put(byte b)
Puts a single byte onto this buffer.

Parameters:
b - The byte to put on the buffer
Returns:
This buffer, allowing chained invocations.

put

public WriteableBuffer put(int p,
                           byte b)
Puts a single byte onto this buffer at the given position.

Parameters:
p - The position where the byte should be put.
b - The byte to put on the buffer
Returns:
This buffer, allowing chained invocations.

append

public WriteableBuffer append(ReadableBuffer b,
                              int n)
Appends a given number of bytes from a readable buffer onto this buffer.

Specified by:
append in interface WriteableBuffer
Overrides:
append in class StdBufferImpl
Parameters:
b - The buffer that we want to append to this buffer.
n - The number of bytes to append onto this buffer.
Returns:
The buffer, to allow chained invocations.

or

public WriteableBuffer or(ReadableBuffer b)
Computes the logical OR of this buffer and another.

Parameters:
b - The buffer to or with this one.
Returns:
The buffer, to allow chained invocations.

xor

public WriteableBuffer xor(ReadableBuffer b)
Computes the logical XOR of this buffer and another.

Parameters:
b - The buffer to or with this one.
Returns:
The buffer, to allow chained invocations.

write

public void write(java.nio.ByteBuffer b)
Write the buffer to a new IO buffer. This will use the bulk put operation on our internal representation.

Parameters:
b - The buffer to which we will write our data.

write

public void write(java.nio.channels.WritableByteChannel chan)
           throws java.io.IOException
Write the buffer to a channel.

Parameters:
chan - The channel to which the buffer should be written.
Throws:
java.io.IOException - If there is any error during writing.

array

protected byte[] array()
Gets an array of bytes from the units.

Returns:
An array containing the data in the buffer.

write

public void write(java.io.DataOutput o)
           throws java.io.IOException
Write the buffer to a data output.

Parameters:
o - The output to which the buffer should be written.
Throws:
java.io.IOException - If there is any error writing the buffer.

write

public void write(java.io.OutputStream os)
           throws java.io.IOException
Write the buffer to a stream.

Parameters:
os - The stream to which the buffer should be written.
Throws:
java.io.IOException - If there is any error writing the data.

getReadableBuffer

public ReadableBuffer getReadableBuffer()
Gets a readable buffer from this writeable one. The readable buffer will share the representation with the buffer that generated it.

Returns:
A new readable buffer that shares the representation of the data.

duplicate

public ReadableBuffer duplicate()
Duplicates this buffer, so that it can be used safely by other readers.

Returns:
A new buffer that shares the data representation with this buffer, but has an independent position.

slice

public ReadableBuffer slice(int p,
                            int l)
Slices this buffer so that a sub-buffer can be used.

Parameters:
l - The number of bytes in the sliced buffer.
p - The position at which the buffer should be sliced.
Returns:
A new buffer that shares the underlying representation with this buffer. The starting position for the new buffer is the given position and the sliced 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:
This buffer, for chained invocations.

limit

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

Parameters:
l - The limit for the buffer.

get

public byte get()
Gets a byte from this buffer.

Returns:
Gets the next byte in this buffer, advancing the current position by one.

get

public byte get(int i)
Gets a byte from this buffer at the given position.

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

byteDecode

public int byteDecode()
Decodes an integer stored using the 7 bit encoding. This is a specialization of the general method that uses get for efficiency reasons.

Specified by:
byteDecode in interface ReadableBuffer
Overrides:
byteDecode in class StdBufferImpl
Returns:
the decoded int.
See Also:
StdBufferImpl.byteEncode(long, int)

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
A main program to test encoding and decoding of integers.

Parameters:
args - The arguments
Throws:
java.lang.Exception - If there is any problem.