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

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

public abstract class StdBufferImpl
extends java.lang.Object
implements WriteableBuffer, ReadableBuffer

A abstract class that implements most of the WriteableBuffer and ReadableBuffer interfaces in terms of the two implemented interfaces' get and put methods. These can be overidden in subclasses if the performance is unacceptable.

See Also:
WriteableBuffer, ReadableBuffer

Field Summary
protected static int[] masks
          Bitmasks to add a single bit to a byte.
protected static long[] maxBEValues
          The number of bytes that are required for encoding a number given our 7 bit encoding strategy.
protected static long[] maxValues
          The maximum values that can be encoded using a given number of bytes.
protected static int[] nBits
          The number of 1 bits in a byte, as a lookup table.
 
Constructor Summary
StdBufferImpl()
           
 
Method Summary
 WriteableBuffer append(ReadableBuffer b)
          Appends a readable buffer onto this buffer.
 WriteableBuffer append(ReadableBuffer b, int n)
          Appends a given number of bytes from a readable buffer onto this buffer.
 int byteDecode()
          Decodes an integer stored using the minimal number of bytes.
 int byteDecode(int nBytes)
          Decodes a postive integer that was coded using a specific number of bytes.
 int byteDecode(int pos, int nBytes)
          Decodes a postive integer that was coded using a specific number of bytes from a specific position in the buffer.
 long byteDecodeLong()
          Decodes a long stored using the 7 bit encoding.
 long byteDecodeLong(int nBytes)
          Decodes a postive long that was coded using a specific number of bytes.
 long byteDecodeLong(int pos, int nBytes)
          Decodes a postive long that was coded using a specific number of bytes from a given position.
 WriteableBuffer byteEncode(int pos, long n, int nBytes)
          Encodes a positive long directly, using a given number of bytes, starting at the given position in the units.
 int byteEncode(long n)
          Encodes an integer in a byte-aligned fashion, using the minimal number of bytes.
 WriteableBuffer byteEncode(long n, int nBytes)
          Encodes a positive long onto a writeable in a given number of bytes.
static int bytesRequired(long n)
          Gets the number of bytes required to directly encode a given number.
static java.lang.String byteToBinaryString(byte n)
          Build a string representation of a byte with the bits in the right order.
 WriteableBuffer clear()
          Clears the buffer.
 int countBits()
          Counts the number of bits that are set in a buffer.
protected  int countBits(int start, int end)
           
 float decodeFloat()
          Decode a float stored in 4 bytes.
 WriteableBuffer encode(java.lang.CharSequence s)
          Encodes a character sequence onto the buffer.
 WriteableBuffer encode(float f)
          Encodes a floating point value in 4 bytes.
 java.lang.String getString()
          Gets a string from this buffer.
 WriteableBuffer set(int bitIndex)
          Sets the given bit to 1 in the given buffer.
protected static int sizeUTF8(java.lang.CharSequence s)
          Determines the size of the UTF-8 encoding of a character sequence.
 int skipByteEncoded()
          Skips an integer encoded using our 7 bit encoding without actually decoding it.
 boolean test(int bitIndex)
          Tests whether a given bit is true or false.
 java.lang.String toString()
          Gets a string representation of the bytes in this buffer.
 java.lang.String toString(int mode)
          Print the bits in the buffer in the order in which they actually occur.
 java.lang.String toString(int start, int end)
          Print the bits in the buffer in the order in which they actually occur.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.sun.labs.minion.util.buffer.WriteableBuffer
capacity, getReadableBuffer, or, put, put, write, write, write, write, xor
 
Methods inherited from interface com.sun.labs.minion.util.buffer.ReadableBuffer
duplicate, get, get, slice
 
Methods inherited from interface com.sun.labs.minion.util.buffer.Buffer
limit, limit, position, position, remaining
 

Field Detail

maxValues

protected static long[] maxValues
The maximum values that can be encoded using a given number of bytes.


maxBEValues

protected static long[] maxBEValues
The number of bytes that are required for encoding a number given our 7 bit encoding strategy.


nBits

protected static int[] nBits
The number of 1 bits in a byte, as a lookup table.


masks

protected static int[] masks
Bitmasks to add a single bit to a byte.

Constructor Detail

StdBufferImpl

public StdBufferImpl()
Method Detail

bytesRequired

public static int bytesRequired(long n)
Gets the number of bytes required to directly encode a given number.

Parameters:
n - The number that we want to encode.
Returns:
The number of bytes required to directly encode the number.

byteEncode

public WriteableBuffer byteEncode(long n,
                                  int nBytes)
Encodes a positive long onto a writeable in a given number of bytes.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
n - The number to encode.
nBytes - The number of bytes to use in the encoding.
Returns:
The buffer, to allow chained invocations.

byteEncode

public WriteableBuffer byteEncode(int pos,
                                  long n,
                                  int nBytes)
Encodes a positive long directly, using a given number of bytes, starting at the given position in the units.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
pos - The position to start encoding.
n - The number to encode.
nBytes - The number of bytes to use in the encoding.
Returns:
The buffer, to allow chained invocations.

byteEncode

public int byteEncode(long n)
Encodes an integer in a byte-aligned fashion, using the minimal number of bytes. The basic idea: use the 7 lower order bits of a byte to encode a number. If the 8th bit is 0, then there are no further bytes in this number. If the 8th bit is one, then the next byte continues the number. Note that this means that a number that would completly fill an integer will take 5 bytes to encode.

Specified by:
byteEncode in interface WriteableBuffer
Parameters:
n - The number to encode.
Returns:
the number of bytes used to encode the number.

encode

public WriteableBuffer encode(float f)
Encodes a floating point value in 4 bytes.

Specified by:
encode in interface WriteableBuffer
Parameters:
f - the floating point number to encode
Returns:
the number of bytes used to encode the float, which is always 4.

sizeUTF8

protected static int sizeUTF8(java.lang.CharSequence s)
Determines the size of the UTF-8 encoding of a character sequence.

Parameters:
s - The sequence that we wish to encode.
Returns:
The number of bytes in the UTF-8 encoding of the sequence.

append

public WriteableBuffer append(ReadableBuffer b)
Appends a readable buffer onto this buffer.

Specified by:
append in interface WriteableBuffer
Parameters:
b - The buffer that we wish to append onto this buffer.
Returns:
The buffer, to allow 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
Parameters:
b - The buffer that we wish to append onto this buffer.
n - The number of bytes from the given buffer to append onto this buffer.
Returns:
The buffer, to allow chained invocations.

encode

public WriteableBuffer encode(java.lang.CharSequence s)
Encodes a character sequence onto the buffer. The sequence is encoded in the following way: first, the number of bytes used to encode the string is encoded using the byteEncode method. Then, the characters in the sequence are encoded using a UTF-8 encoding.

Specified by:
encode in interface WriteableBuffer
Parameters:
s - The sequence that we wish to encode.
Returns:
This buffer, for chained invocations.

set

public WriteableBuffer set(int bitIndex)
Sets the given bit to 1 in the given buffer.

Specified by:
set in interface WriteableBuffer
Parameters:
bitIndex - the index of the bit to set to 1.
Returns:
This buffer, for chained invocations.

clear

public WriteableBuffer clear()
Clears the buffer. This default implementation simply sets the position to 0.

Specified by:
clear in interface WriteableBuffer
Returns:
This buffer, for chained invocations.

byteDecode

public int byteDecode(int nBytes)
Decodes a postive integer that was coded using a specific number of bytes.

Specified by:
byteDecode in interface ReadableBuffer
Parameters:
nBytes - The number of bytes to use.
Returns:
the decoded number.

byteDecode

public int byteDecode(int pos,
                      int nBytes)
Decodes a postive integer that was coded using a specific number of bytes from a specific position in the buffer.

Specified by:
byteDecode in interface ReadableBuffer
Parameters:
pos - The position to decode from.
nBytes - The number of bytes to use.
Returns:
the decoded number.

byteDecodeLong

public long byteDecodeLong(int nBytes)
Decodes a postive long that was coded using a specific number of bytes.

Specified by:
byteDecodeLong in interface ReadableBuffer
Parameters:
nBytes - The number of bytes to use.
Returns:
the decoded number.

byteDecodeLong

public long byteDecodeLong(int pos,
                           int nBytes)
Decodes a postive long that was coded using a specific number of bytes from a given position.

Specified by:
byteDecodeLong in interface ReadableBuffer
Parameters:
pos - The position to decode from.
nBytes - The number of bytes to use.
Returns:
the decoded number.

byteDecode

public int byteDecode()
Decodes an integer stored using the minimal number of bytes.

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

byteDecodeLong

public long byteDecodeLong()
Decodes a long stored using the 7 bit encoding.

Specified by:
byteDecodeLong in interface ReadableBuffer
Returns:
the decoded long.
See Also:
byteEncode(long, int)

decodeFloat

public float decodeFloat()
Decode a float stored in 4 bytes.

Specified by:
decodeFloat in interface ReadableBuffer
Returns:
the decoded float.

skipByteEncoded

public int skipByteEncoded()
Skips an integer encoded using our 7 bit encoding without actually decoding it.

Specified by:
skipByteEncoded in interface ReadableBuffer
Returns:
the number of bytes skipped.
See Also:
WriteableBuffer.byteEncode(long, int)

test

public boolean test(int bitIndex)
Tests whether a given bit is true or false.

Specified by:
test in interface ReadableBuffer
Parameters:
bitIndex - the index of the bit to test.
Returns:
true if the bit is 1, false if it is 0

countBits

public int countBits()
Counts the number of bits that are set in a buffer.

Specified by:
countBits in interface ReadableBuffer
Returns:
The number of 1 bits in the buffer.

countBits

protected int countBits(int start,
                        int end)

getString

public java.lang.String getString()
Gets a string from this buffer.

Specified by:
getString in interface ReadableBuffer
Returns:
The decoded string.
See Also:
WriteableBuffer.encode(CharSequence)

toString

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

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the buffer.

toString

public java.lang.String toString(int mode)
Print the bits in the buffer in the order in which they actually occur.

Parameters:
mode - The type of print out required.
Returns:
A string representation of the buffer.

toString

public java.lang.String toString(int start,
                                 int end)
Print the bits in the buffer in the order in which they actually occur.

Parameters:
start - The starting position in the buffer from which to display the bytes.
end - The (exclusive) ending position in the buffer.
Returns:
A string representation of the buffer.

byteToBinaryString

public static java.lang.String byteToBinaryString(byte n)
Build a string representation of a byte with the bits in the right order.

Parameters:
n - The number to represent
Returns:
A string containing a representation of the bits in this byte.