org.apache.catalina.tribes.io

Class XByteBuffer

public class XByteBuffer extends 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.
THIS CLASS IS NOT THREAD SAFE

Transfer package:

Version: $Revision: 467173 $, $Date: 2006-10-24 01:12:17 +0200 (Tue, 24 Oct 2006) $

Author: Filip Hanik

Field Summary
protected byte[]buf
Variable to hold the data
protected intbufSize
Current length of data in the buffer
protected booleandiscard
Flag for discarding invalid packages If this flag is set to true, and append(byte[],...) is called, the data added will be inspected, and if it doesn't start with START_DATA it will be thrown away.
static byte[]END_DATA
This is the package footer, 7 bytes (TLF2003)
static intinvokecount
static Loglog
static byte[]START_DATA
This is a package header, 7 bytes (FLT2002)
Constructor Summary
XByteBuffer(int size, boolean discard)
Constructs a new XByteBuffer
XByteBuffer(byte[] data, boolean discard)
XByteBuffer(byte[] data, int size, boolean discard)
Method Summary
booleanappend(ByteBuffer b, int len)
Appends the data to the buffer.
booleanappend(byte i)
booleanappend(boolean i)
booleanappend(long i)
booleanappend(int i)
booleanappend(byte[] b, int off, int len)
voidclear()
Resets the buffer
intcountPackages()
Internal mechanism to make a check if a complete package exists within the buffer
intcountPackages(boolean first)
static byte[]createDataPackage(ChannelData cdata)
Creates a complete data package
static byte[]createDataPackage(byte[] data, int doff, int dlength, byte[] buffer, int bufoff)
static byte[]createDataPackage(byte[] data)
static Serializabledeserialize(byte[] data)
static Serializabledeserialize(byte[] data, int offset, int length)
static Serializabledeserialize(byte[] data, int offset, int length, ClassLoader[] cls)
booleandoesPackageExist()
Method to check if a package exists in this byte buffer.
voidexpand(int newcount)
XByteBufferextractDataPackage(boolean clearFromBuffer)
Extracts the message bytes from a package.
ChannelDataextractPackage(boolean clearFromBuffer)
static intfirstIndexOf(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
byte[]getBytesDirect()
intgetCapacity()
static intgetDataPackageLength(int datalength)
booleangetDiscard()
intgetLength()
voidreset()
static byte[]serialize(Serializable msg)
Serializes a message into cluster data
voidsetDiscard(boolean discard)
voidsetLength(int size)
static booleantoBoolean(byte[] b, int offset)
static byte[]toBytes(boolean bool)
Converts an integer to four bytes
static byte[]toBytes(boolean bool, byte[] data, int offset)
static byte[]toBytes(int n)
Converts an integer to four bytes
static byte[]toBytes(int n, byte[] b, int offset)
static byte[]toBytes(long n)
Converts an long to eight bytes
static byte[]toBytes(long n, byte[] b, int offset)
static inttoInt(byte[] b, int off)
Convert four bytes to an int
static longtoLong(byte[] b, int off)
Convert eight bytes to a long
voidtrim(int length)

Field Detail

buf

protected byte[] buf
Variable to hold the data

bufSize

protected int bufSize
Current length of data in the buffer

discard

protected boolean discard
Flag for discarding invalid packages If this flag is set to true, and append(byte[],...) is called, the data added will be inspected, and if it doesn't start with START_DATA it will be thrown away.

END_DATA

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

invokecount

public static int invokecount

log

public static Log log

START_DATA

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

Constructor Detail

XByteBuffer

public XByteBuffer(int size, boolean discard)
Constructs a new XByteBuffer

Parameters: size - the initial size of the byte buffer

UNKNOWN: use a pool of byte[] for performance

XByteBuffer

public XByteBuffer(byte[] data, boolean discard)

XByteBuffer

public XByteBuffer(byte[] data, int size, boolean discard)

Method Detail

append

public boolean append(ByteBuffer b, 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

append

public boolean append(byte i)

append

public boolean append(boolean i)

append

public boolean append(long i)

append

public boolean append(int i)

append

public boolean append(byte[] b, int off, int len)

clear

public void clear()
Resets the buffer

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

countPackages

public int countPackages(boolean first)

createDataPackage

public static byte[] createDataPackage(ChannelData cdata)
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,size,data,footer)

createDataPackage

public static byte[] createDataPackage(byte[] data, int doff, int dlength, byte[] buffer, int bufoff)

createDataPackage

public static byte[] createDataPackage(byte[] data)

deserialize

public static Serializable deserialize(byte[] data)

deserialize

public static Serializable deserialize(byte[] data, int offset, int length)

deserialize

public static Serializable deserialize(byte[] data, int offset, int length, ClassLoader[] cls)

doesPackageExist

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

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

expand

public void expand(int newcount)

extractDataPackage

public XByteBuffer extractDataPackage(boolean clearFromBuffer)
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).

extractPackage

public ChannelData extractPackage(boolean clearFromBuffer)

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

getBytes

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

getBytesDirect

public byte[] getBytesDirect()

getCapacity

public int getCapacity()

getDataPackageLength

public static int getDataPackageLength(int datalength)

getDiscard

public boolean getDiscard()

getLength

public int getLength()

reset

public void reset()

serialize

public static byte[] serialize(Serializable msg)
Serializes a message into cluster data

Parameters: msg ClusterMessage compress boolean

Returns:

Throws: IOException

setDiscard

public void setDiscard(boolean discard)

setLength

public void setLength(int size)

toBoolean

public static boolean toBoolean(byte[] b, int offset)

Parameters: long

Returns: use

toBytes

public static byte[] toBytes(boolean bool)

Deprecated: use toBytes(boolean,byte[],int)

Converts an integer to four bytes

Parameters: n - the integer

Returns: - four bytes in an array

toBytes

public static byte[] toBytes(boolean bool, byte[] data, int offset)

toBytes

public static byte[] toBytes(int n)

Deprecated: use toBytes(int,byte[],int)

Converts an integer to four bytes

Parameters: n - the integer

Returns: - four bytes in an array

toBytes

public static byte[] toBytes(int n, byte[] b, int offset)

toBytes

public static byte[] toBytes(long n)

Deprecated: use toBytes(long,byte[],int)

Converts an long to eight bytes

Parameters: n - the long

Returns: - eight bytes in an array

toBytes

public static byte[] toBytes(long n, byte[] b, int offset)

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

trim

public void trim(int length)
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.