com.sun.faban.harness.logging
Class FlexBuffer

java.lang.Object
  extended by com.sun.faban.harness.logging.FlexBuffer

public class FlexBuffer
extends Object

FlexBuffer is an expandable byte buffer. It uses 1 or more byte arrays internally and expands with an array of the initial size at a time. This byte buffer is intended for reuse. The clear() method resets all pointers and the buffer to 0. It does not free up any memory.

Author:
Akara Sucharitakul

Nested Class Summary
 class FlexBuffer.Tokenizer
          A tokenizer for the FlexBuffer.
 
Field Summary
static byte CR
          Carriage return.
static byte LF
          Line feed.
static byte WS
          White space.
 
Constructor Summary
FlexBuffer(byte[] backingArray)
          Constructs a FlexBuffer with an initial backing array.
FlexBuffer(byte[] origin, int start, int length)
          Constructs a FlexBuffer with initial content.
FlexBuffer(int initial)
          FlexBuffer constructor.
 
Method Summary
 void append(byte b)
          Appends one byte to the buffer.
 void append(byte[] b)
          Appends the content from the whole byte array to the buffer.
 void append(byte[] b, int offset, int length)
          Appends content from byte array to the buffer.
 void append(ByteBuffer b, int length)
          Appends content of the nio ByteBuffer of a certain length.
 void appendDirect(ByteBuffer b, int length)
          Appends content of the nio direct ByteBuffer of a certain length to the FlexBuffer.
 void clear()
          Clears the FlexBuffer.
 boolean endsWith(String s)
          Checks if the string ends with provided string.
 int getByte(int pos)
          Obtains the byte a a certain position in the buffer.
 byte[] getBytes()
          Gets a byte array resembling the whole buffer.
 int getBytes(int pos, byte[] b, int off, int length)
          Copies buffer content into byte array.
 byte[] getBytes(int pos, int length)
          Gets a byte array resembling part of the buffer.
 String getString(int pos, int length)
          Obtains a String representation of all or part of the buffer.
 FlexBuffer.Tokenizer getTokenizer()
          Returns Tokenizer instance.
 String getTrimmedString(int pos, int length)
          Trims the string based on the position and the length.
 int indexOf(String s)
          Searches the byte buffer for the first occurrence of the input string, converted to bytes.
 int indexOf(String s, int start)
          Searches the byte buffer for the first occurrence of the input string from a starting index, converted to bytes.
 int indexOfBOL(int eol)
          Obtains the index of the next line begin, given the EOL marker.
 int indexOfEOL(int start)
          Searches the buffer for an end-of-line.
protected  void nextBuffer()
          Switches the buffer to the next one.
 int size()
          Obtains the size of this buffer.
 String toString()
          Provides a String representation of the buffer content.
 int writeBytes(int pos, int length, OutputStream stream)
          Writes buffer content into an OutputStream.
 int writeBytes(OutputStream stream)
          Writes buffer content into an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CR

public static final byte CR
Carriage return.

See Also:
Constant Field Values

LF

public static final byte LF
Line feed.

See Also:
Constant Field Values

WS

public static final byte WS
White space.

See Also:
Constant Field Values
Constructor Detail

FlexBuffer

public FlexBuffer(int initial)
FlexBuffer constructor.

Parameters:
initial - The initial size and expansion block

FlexBuffer

public FlexBuffer(byte[] origin,
                  int start,
                  int length)
Constructs a FlexBuffer with initial content. The initial size will be the length of the content.

Parameters:
origin - the byte array to take content from
start - the starting offset in the byte array
length - the content length to copy

FlexBuffer

public FlexBuffer(byte[] backingArray)
Constructs a FlexBuffer with an initial backing array. This array will be used directly and any data copy is avoided. As the array can be written from whomever else has a reference to it, this constructor is not very safe. However, it prevents another copy and is of very high performance.

Parameters:
backingArray - The byte array to be used as a backing array.
Method Detail

clear

public void clear()
Clears the FlexBuffer. Resets pointers. Does not free any of the allocated arrays.


nextBuffer

protected void nextBuffer()
Switches the buffer to the next one.


append

public void append(byte b)
Appends one byte to the buffer.

Parameters:
b - byte to append

append

public void append(byte[] b,
                   int offset,
                   int length)
Appends content from byte array to the buffer.

Parameters:
b - byte array to append
offset - the offset to start appending
length - the length to start appending

append

public void append(byte[] b)
Appends the content from the whole byte array to the buffer.

Parameters:
b - byte array to append

append

public void append(ByteBuffer b,
                   int length)
Appends content of the nio ByteBuffer of a certain length. This method will access the buffer's backing array if possible and call appendDirect if there is no backing array.

Parameters:
b - The buffer
length - append

appendDirect

public void appendDirect(ByteBuffer b,
                         int length)
Appends content of the nio direct ByteBuffer of a certain length to the FlexBuffer.

Parameters:
b - The buffer
length - The length to append

size

public int size()
Obtains the size of this buffer.

Returns:
the current buffer size

getBytes

public byte[] getBytes()
Gets a byte array resembling the whole buffer.

Returns:
a copy of the whole buffer.

getBytes

public byte[] getBytes(int pos,
                       int length)
Gets a byte array resembling part of the buffer.

Parameters:
pos - starting position in the buffer
length - content length
Returns:
a copy of the whole buffer.

getBytes

public int getBytes(int pos,
                    byte[] b,
                    int off,
                    int length)
Copies buffer content into byte array. If the length exceeds the end of the buffer, only bytes till the buffer end will be copied. Therefore the return value may not be the same as length.

Parameters:
pos - position to start copy in the buffer
b - byte array to copy into
off - offset in the byte array to start copy
length - content length to copy
Returns:
the content length actually copied

writeBytes

public int writeBytes(int pos,
                      int length,
                      OutputStream stream)
               throws IOException
Writes buffer content into an OutputStream. This method will do a direct write from the buffer into the stream.

Parameters:
pos - position to start copy in the buffer
length - content length to copy
stream - the output stream to write to
Returns:
the content length actually written
Throws:
IOException - write error

writeBytes

public int writeBytes(OutputStream stream)
               throws IOException
Writes buffer content into an OutputStream. This method will do a direct write from the buffer into the stream.

Parameters:
stream - the output stream to write to
Returns:
the content length actually written
Throws:
IOException - write error

getString

public String getString(int pos,
                        int length)
Obtains a String representation of all or part of the buffer. It is semantically equal to new String(getBytes(pos, length) but contains further optimizations to avoid copies.

Parameters:
pos - The starting position
length - The length
Returns:
The string representing the requested part of the buffer.

getTrimmedString

public String getTrimmedString(int pos,
                               int length)
Trims the string based on the position and the length.

Parameters:
pos - The starting position
length - The length
Returns:
the resulting substring

getByte

public int getByte(int pos)
Obtains the byte a a certain position in the buffer.

Parameters:
pos - the position of the byte
Returns:
the value of the byte at the given position.

indexOf

public int indexOf(String s)
Searches the byte buffer for the first occurrence of the input string, converted to bytes.

Parameters:
s - the string to match
Returns:
the location of the match

endsWith

public boolean endsWith(String s)
Checks if the string ends with provided string.

Parameters:
s - The string to check the buffer against
Returns:
true if the buffer ends with the provided string, false otherwise

indexOf

public int indexOf(String s,
                   int start)
Searches the byte buffer for the first occurrence of the input string from a starting index, converted to bytes.

Parameters:
s - the string to match
start - the search starting point
Returns:
the location of the match

indexOfEOL

public int indexOfEOL(int start)
Searches the buffer for an end-of-line. This can be marked by the characters "\r", "\n", or "\r\n." The index of the first EOL marker is returned.

Parameters:
start - The offset to start searching
Returns:
The index of the first EOL marker or -1 if not found

indexOfBOL

public int indexOfBOL(int eol)
Obtains the index of the next line begin, given the EOL marker. This can be eol + 1 or eol + 2 dependent of whether the EOL marker is "\r", "\n", or "\r\n."

Note: the method does not actually check that

Parameters:
eol - The EOL index
Returns:
The beginning of the following line.

toString

public String toString()
Provides a String representation of the buffer content. The content is demarcated with buffer ends for debugging purposes.

Overrides:
toString in class Object
Returns:
the string representation

getTokenizer

public FlexBuffer.Tokenizer getTokenizer()
Returns Tokenizer instance.

Returns:
Tokenizer