org.freecompany.util.text
Class ByteBufferWrapper

java.lang.Object
  extended by org.freecompany.util.text.ByteBufferWrapper
All Implemented Interfaces:
java.lang.CharSequence

public class ByteBufferWrapper
extends java.lang.Object
implements java.lang.CharSequence

This class provides a wrapper view of a buffer of bytes as a sequence of US-ASCII characters. This is appropriate for treating a buffer of bytes like a buffer of characters when it is known that it contains only 7 bit characters.


Field Summary
protected  java.nio.ByteBuffer buffer
           
 
Constructor Summary
ByteBufferWrapper(java.nio.ByteBuffer buffer)
           
 
Method Summary
 char charAt(int index)
          Returns the character at the provided index in the backing byte buffer, respective of the current buffer position.
 java.nio.ByteBuffer getBuffer()
           
 int length()
          Returns the perceived length of this character sequence, which in this case is the number of bytes between the position and limit of the backing buffer.
 java.lang.CharSequence subSequence(int start, int end)
          Creates a new wrapped slice of the buffer given position and limit values derived from the provided start and end indexes.
 java.lang.String toString()
          Creates a UTF-8 decoded string representation of this byte buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buffer

protected final java.nio.ByteBuffer buffer
Constructor Detail

ByteBufferWrapper

public ByteBufferWrapper(java.nio.ByteBuffer buffer)
Method Detail

getBuffer

public java.nio.ByteBuffer getBuffer()

charAt

public char charAt(int index)
Returns the character at the provided index in the backing byte buffer, respective of the current buffer position.

Specified by:
charAt in interface java.lang.CharSequence

length

public int length()
Returns the perceived length of this character sequence, which in this case is the number of bytes between the position and limit of the backing buffer.

Specified by:
length in interface java.lang.CharSequence

subSequence

public java.lang.CharSequence subSequence(int start,
                                          int end)
Creates a new wrapped slice of the buffer given position and limit values derived from the provided start and end indexes. After slicing, the underlying buffer is reset to it's original position and limit, but as the temporary changes are not atomic care must be taken not to depend on the state of the backing buffer being consistent during this call. In other words, the buffer is not safe for access by other threads while this method is executing.

Specified by:
subSequence in interface java.lang.CharSequence

toString

public java.lang.String toString()
Creates a UTF-8 decoded string representation of this byte buffer. Teh behavior is undefined if the backing buffer contains any non US-ASCII characters. This method is implemented to build a string one character at a time and so may be prohibitively slow for large buffers.

Specified by:
toString in interface java.lang.CharSequence
Overrides:
toString in class java.lang.Object