org.apache.catalina.cluster.io
Class XByteBuffer

java.lang.Object
  extended byorg.apache.catalina.cluster.io.XByteBuffer

public class XByteBuffer
extends java.lang.Object

The XByteBuffer provides a dual functionality. One, it stores message bytes and automatically extends the byte buffer if needed.
Two, it can encode and decode packages so that they can be defined and identified as they come in on a socket.
Transfer package:

FIXME: Why we not use a list of byte buffers? FIXME: Used a pool of buffers instead, every time new generation

Version:
$Id: XByteBuffer.java 939539 2010-04-30 01:31:33Z kkolinko $
Author:
Filip Hanik, Peter Rossbach

Field Summary
protected  byte[] buf
          Variable to hold the data
protected  int bufSize
          Current length of data in the buffer
static byte[] END_DATA
          This is the package footer, 7 bytes (TLF2003)
static org.apache.commons.logging.Log log
           
static byte[] START_DATA
          This is a package header, 7 bytes (FLT2002)
 
Constructor Summary
XByteBuffer()
          Constructs a new XByteBuffer with an initial size of 1024 bytes
XByteBuffer(int size)
          Constructs a new XByteBuffer
 
Method Summary
 boolean append(byte[] b, int off, int len)
          Appends the data to the buffer.
 void clear()
          Resets the buffer
 int countPackages()
          Internal mechanism to make a check if a complete package exists within the buffer
static byte[] createDataPackage(byte[] indata, int compressed)
          Creates a complete data package
 boolean doesPackageExist()
          Method to check if a package exists in this byte buffer.
 ClusterData extractPackage(boolean clearFromBuffer)
          Extracts the message bytes from a package.
static int firstIndexOf(byte[] src, int srcOff, byte[] find)
          Similar to a String.IndexOf, but uses pure bytes
 byte[] getBytes()
          Returns the bytes in the buffer, in its exact length
static byte[] toBytes(int n)
          Converts an integer to four bytes
static byte[] toBytes(long n)
          Converts an long to eight bytes
static int toInt(byte[] b, int off)
          Convert four bytes to an int
static long toLong(byte[] b, int off)
          Convert eight bytes to a long
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public static org.apache.commons.logging.Log log

START_DATA

public static final byte[] START_DATA
This is a package header, 7 bytes (FLT2002)


END_DATA

public static final byte[] END_DATA
This is the package footer, 7 bytes (TLF2003)


buf

protected byte[] buf
Variable to hold the data


bufSize

protected int bufSize
Current length of data in the buffer

Constructor Detail

XByteBuffer

public XByteBuffer(int size)
Constructs a new XByteBuffer

Parameters:
size - - the initial size of the byte buffer

XByteBuffer

public XByteBuffer()
Constructs a new XByteBuffer with an initial size of 1024 bytes

Method Detail

getBytes

public byte[] getBytes()
Returns the bytes in the buffer, in its exact length


clear

public void clear()
Resets the buffer


append

public boolean append(byte[] b,
                      int off,
                      int len)
Appends the data to the buffer. If the data is incorrectly formatted, ie, the data should always start with the header, false will be returned and the data will be discarded.

Parameters:
b - - bytes to be appended
off - - the offset to extract data from
len - - the number of bytes to append.
Returns:
true if the data was appended correctly. Returns false if the package is incorrect, ie missing header or something, or the length of data is 0

countPackages

public int countPackages()
Internal mechanism to make a check if a complete package exists within the buffer

Returns:
- true if a complete package (header,compress,size,data,footer) exists within the buffer

doesPackageExist

public boolean doesPackageExist()
Method to check if a package exists in this byte buffer.

Returns:
- true if a complete package (header,compress,size,data,footer) exists within the buffer

extractPackage

public ClusterData extractPackage(boolean clearFromBuffer)
                           throws java.io.IOException
Extracts the message bytes from a package. If no package exists, a IllegalStateException will be thrown.

Parameters:
clearFromBuffer - - if true, the package will be removed from the byte buffer
Returns:
- returns the actual message bytes (header, compress,size and footer not included).
Throws:
java.io.IOException

toInt

public static int toInt(byte[] b,
                        int off)
Convert four bytes to an int

Parameters:
b - - the byte array containing the four bytes
off - - the offset
Returns:
the integer value constructed from the four bytes
Throws:
java.lang.ArrayIndexOutOfBoundsException

toLong

public static long toLong(byte[] b,
                          int off)
Convert eight bytes to a long

Parameters:
b - - the byte array containing the four bytes
off - - the offset
Returns:
the long value constructed from the eight bytes
Throws:
java.lang.ArrayIndexOutOfBoundsException

toBytes

public static byte[] toBytes(int n)
Converts an integer to four bytes

Parameters:
n - - the integer
Returns:
- four bytes in an array

toBytes

public static byte[] toBytes(long n)
Converts an long to eight bytes

Parameters:
n - - the long
Returns:
- eight bytes in an array

firstIndexOf

public static int firstIndexOf(byte[] src,
                               int srcOff,
                               byte[] find)
Similar to a String.IndexOf, but uses pure bytes

Parameters:
src - - the source bytes to be searched
srcOff - - offset on the source buffer
find - - the string to be found within src
Returns:
- the index of the first matching byte. -1 if the find array is not found

createDataPackage

public static byte[] createDataPackage(byte[] indata,
                                       int compressed)
                                throws java.io.IOException
Creates a complete data package

Parameters:
indata - - the message data to be contained within the package
compressed - - compression flag for the indata buffer
Returns:
- a full package (header,compress,size,data,footer)
Throws:
java.io.IOException


Copyright © 2000-2012 Apache Software Foundation. All Rights Reserved.