Package org.apache.catalina.tribes.io
Class XByteBuffer
- java.lang.Object
- 
- org.apache.catalina.tribes.io.XByteBuffer
 
- 
- All Implemented Interfaces:
- java.io.Serializable
 
 public class XByteBuffer extends java.lang.Object implements java.io.SerializableThe 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:- START_DATA- 7 bytes - FLT2002
- SIZE - 4 bytes - size of the data package
- DATA - should be as many bytes as the prev SIZE
- END_DATA - 7 bytes - TLF2003
 - See Also:
- Serialized Form
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected byte[]bufVariable to hold the dataprotected intbufSizeCurrent length of data in the bufferprotected booleandiscardFlag 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 withSTART_DATAit will be thrown away.protected static StringManagersm
 - 
Constructor SummaryConstructors Constructor Description XByteBuffer(byte[] data, boolean discard)XByteBuffer(byte[] data, int size, boolean discard)XByteBuffer(int size, boolean discard)Constructs a new XByteBuffer.
 TODO use a pool of byte[] for performance
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanappend(boolean i)booleanappend(byte i)booleanappend(byte[] b, int off, int len)booleanappend(int i)booleanappend(long i)booleanappend(java.nio.ByteBuffer b, int len)Appends the data to the buffer.voidclear()Resets the bufferintcountPackages()Internal mechanism to make a check if a complete package exists within the bufferintcountPackages(boolean first)static byte[]createDataPackage(byte[] data)static byte[]createDataPackage(byte[] data, int doff, int dlength, byte[] buffer, int bufoff)static byte[]createDataPackage(ChannelData cdata)Creates a complete data packagestatic java.io.Serializabledeserialize(byte[] data)static java.io.Serializabledeserialize(byte[] data, int offset, int length)static java.io.Serializabledeserialize(byte[] data, int offset, int length, java.lang.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()byte[]getBytesDirect()intgetCapacity()static intgetDataPackageLength(int datalength)booleangetDiscard()intgetLength()voidreset()static byte[]serialize(java.io.Serializable msg)Serializes a message into cluster datavoidsetDiscard(boolean discard)voidsetLength(int size)static booleantoBoolean(byte[] b, int offset)Converts a byte array entry to boolean.static byte[]toBytes(boolean bool, byte[] data, int offset)Converts a boolean and put it in a byte array.static byte[]toBytes(int n, byte[] b, int offset)Converts an integer to four bytes.static byte[]toBytes(long n, byte[] b, int offset)Converts a long to eight bytes.static inttoInt(byte[] b, int off)Convert four bytes to an intstatic longtoLong(byte[] b, int off)Convert eight bytes to a longvoidtrim(int length)
 
- 
- 
- 
Field Detail- 
smprotected static final StringManager sm 
 - 
bufprotected byte[] buf Variable to hold the data
 - 
bufSizeprotected int bufSize Current length of data in the buffer
 - 
discardprotected 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 withSTART_DATAit will be thrown away.
 
- 
 - 
Constructor Detail- 
XByteBufferpublic XByteBuffer(int size, boolean discard)Constructs a new XByteBuffer.
 TODO use a pool of byte[] for performance- Parameters:
- size- the initial size of the byte buffer
- discard- Flag for discarding invalid packages
 
 - 
XByteBufferpublic XByteBuffer(byte[] data, boolean discard)
 - 
XByteBufferpublic XByteBuffer(byte[] data, int size, boolean discard)
 
- 
 - 
Method Detail- 
getLengthpublic int getLength() 
 - 
setLengthpublic void setLength(int size) 
 - 
trimpublic void trim(int length) 
 - 
resetpublic void reset() 
 - 
getBytesDirectpublic byte[] getBytesDirect() 
 - 
getBytespublic byte[] getBytes() - Returns:
- the bytes in the buffer, in its exact length
 
 - 
clearpublic void clear() Resets the buffer
 - 
appendpublic boolean append(java.nio.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
- 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
 
 - 
appendpublic boolean append(byte i) 
 - 
appendpublic boolean append(boolean i) 
 - 
appendpublic boolean append(long i) 
 - 
appendpublic boolean append(int i) 
 - 
appendpublic boolean append(byte[] b, int off, int len)
 - 
expandpublic void expand(int newcount) 
 - 
getCapacitypublic int getCapacity() 
 - 
countPackagespublic 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
 
 - 
countPackagespublic int countPackages(boolean first) 
 - 
doesPackageExistpublic 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
 
 - 
extractDataPackagepublic 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).
 
 - 
extractPackagepublic ChannelData extractPackage(boolean clearFromBuffer) 
 - 
createDataPackagepublic static byte[] createDataPackage(ChannelData cdata) Creates a complete data package- Parameters:
- cdata- - the message data to be contained within the package
- Returns:
- - a full package (header,size,data,footer)
 
 - 
createDataPackagepublic static byte[] createDataPackage(byte[] data, int doff, int dlength, byte[] buffer, int bufoff)
 - 
getDataPackageLengthpublic static int getDataPackageLength(int datalength) 
 - 
createDataPackagepublic static byte[] createDataPackage(byte[] data) 
 - 
toIntpublic 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
 
 - 
toLongpublic 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
 
 - 
toBytespublic static byte[] toBytes(boolean bool, byte[] data, int offset)Converts a boolean and put it in a byte array.- Parameters:
- bool- the integer
- data- the byte buffer in which the boolean will be placed
- offset- the offset in the byte array
- Returns:
- the byte array
 
 - 
toBooleanpublic static boolean toBoolean(byte[] b, int offset)Converts a byte array entry to boolean.- Parameters:
- b- byte array
- offset- within byte array
- Returns:
- true if byte array entry is non-zero, false otherwise
 
 - 
toBytespublic static byte[] toBytes(int n, byte[] b, int offset)Converts an integer to four bytes.- Parameters:
- n- the integer
- b- the byte buffer in which the integer will be placed
- offset- the offset in the byte array
- Returns:
- four bytes in an array
 
 - 
toBytespublic static byte[] toBytes(long n, byte[] b, int offset)Converts a long to eight bytes.- Parameters:
- n- the long
- b- the byte buffer in which the integer will be placed
- offset- the offset in the byte array
- Returns:
- eight bytes in an array
 
 - 
firstIndexOfpublic 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
 
 - 
deserializepublic static java.io.Serializable deserialize(byte[] data) throws java.io.IOException, java.lang.ClassNotFoundException, java.lang.ClassCastException- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
- java.lang.ClassCastException
 
 - 
deserializepublic static java.io.Serializable deserialize(byte[] data, int offset, int length) throws java.io.IOException, java.lang.ClassNotFoundException, java.lang.ClassCastException- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
- java.lang.ClassCastException
 
 - 
deserializepublic static java.io.Serializable deserialize(byte[] data, int offset, int length, java.lang.ClassLoader[] cls) throws java.io.IOException, java.lang.ClassNotFoundException, java.lang.ClassCastException- Throws:
- java.io.IOException
- java.lang.ClassNotFoundException
- java.lang.ClassCastException
 
 - 
serializepublic static byte[] serialize(java.io.Serializable msg) throws java.io.IOExceptionSerializes a message into cluster data- Parameters:
- msg- ClusterMessage
- Returns:
- serialized content as byte[] array
- Throws:
- java.io.IOException- Serialization error
 
 - 
setDiscardpublic void setDiscard(boolean discard) 
 - 
getDiscardpublic boolean getDiscard() 
 
- 
 
-