org.freecompany.util.text

Class ByteBufferWrapper

public class ByteBufferWrapper extends Object implements 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 ByteBufferbuffer
Constructor Summary
ByteBufferWrapper(ByteBuffer buffer)
Method Summary
charcharAt(int index)
Returns the character at the provided index in the backing byte buffer, respective of the current buffer position.
ByteBuffergetBuffer()
intlength()
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.
CharSequencesubSequence(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.
StringtoString()
Creates a UTF-8 decoded string representation of this byte buffer.

Field Detail

buffer

protected final ByteBuffer buffer

Constructor Detail

ByteBufferWrapper

public ByteBufferWrapper(ByteBuffer buffer)

Method Detail

charAt

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

getBuffer

public ByteBuffer getBuffer()

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.

subSequence

public 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.

toString

public 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.