org.jnetpcap.nio
Class JBuffer

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
Direct Known Subclasses:
Http.Chunk, JHeader, JPacket

public class JBuffer
extends JMemory

A direct buffer stored in native memory.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
JBuffer(byte[] data)
          Instantiates a new j buffer.
JBuffer(java.nio.ByteBuffer peer)
          Instantiates a new j buffer.
JBuffer(int size)
          Instantiates a new j buffer.
JBuffer(JMemory.Type type)
          Creates a.
JBuffer(JMemory peer)
          Instantiates a new j buffer.
 
Method Summary
 int findUTF8String(int index, char... delimeter)
          Find the delimiter array of chars within the buffer.
 byte getByte(int index)
          Gets a signed 8-bit value.
 byte[] getByteArray(int index, byte[] array)
          Gets byte data from buffer and stores it in supplied array buffer.
 byte[] getByteArray(int index, byte[] array, int offset, int length)
          Reads data from JBuffer into user supplied array.
 byte[] getByteArray(int index, int size)
          Gets the byte data from buffer and stores into newly allocated byte array.
 double getDouble(int index)
          Gets the java double value out of the buffer.
 float getFloat(int index)
          Gets the java float value out of the buffer.
 int getInt(int index)
          Gets the java signed integer value from the buffer.
 long getLong(int index)
          Gets the java signed long value from the buffer.
 short getShort(int index)
          Gets the java signed short value from the buffer.
 int getUByte(int index)
          Gets the java usigned byte value.
 long getUInt(int index)
          Gets the java usigned int value.
 int getUShort(int index)
          Gets the java usigned short value.
 char getUTF8Char(int index)
          Converts a single byte to a java char.
 java.lang.String getUTF8String(int index, char... delimeter)
          Retrieves all the characters from the buffer upto the delimiter char sequence.
 java.lang.String getUTF8String(int index, int length)
          Gets the specified number of characters as a string.
 java.lang.StringBuilder getUTF8String(int index, java.lang.StringBuilder buf, char... delimeter)
          Retrieves all the characters from the buffer upto the delimiter char sequence.
 java.lang.StringBuilder getUTF8String(int index, java.lang.StringBuilder buf, int length)
          Converts raw bytes to a java string.
 boolean isReadonly()
          Checks if this buffer is readonly.
 java.nio.ByteOrder order()
          Gets the byte-order of this buffer.
 void order(java.nio.ByteOrder order)
          Sets the byte ordering of integers for this buffer.
 int peer(java.nio.ByteBuffer peer)
          Peers this buffer with a new buffer.
 int peer(JBuffer peer)
          Peers this buffer with the new buffer.
 int peer(JBuffer peer, int offset, int length)
          Peers this buffer with a new buffer.
 int peer(JMemory src)
          Peers this object with the supplied object.
 void setByte(int index, byte value)
          Sets a value in the buffer.
 void setByteArray(int index, byte[] array)
          Sets a value in the buffer.
 void setByteBuffer(int index, java.nio.ByteBuffer data)
          Sets data within this buffer.
 void setDouble(int index, double value)
          Sets a value in the buffer.
 void setFloat(int index, float value)
          Sets a value in the buffer.
 void setInt(int index, int value)
          Sets a value in the buffer.
 void setLong(int index, long value)
          Sets a value in the buffer.
 void setShort(int index, short value)
          Sets a value in the buffer.
static void setShort0(long address, boolean order, int index, short value)
          Sets the short0.
 void setUByte(int index, int value)
          Sets a value in the buffer.
 void setUInt(int index, long value)
          Sets a value in the buffer.
 void setUShort(int index, int value)
          Sets a value in the buffer.
 int transferFrom(byte[] buffer)
          Copies contents of the supplied buffer into this buffer.
 int transferFrom(java.nio.ByteBuffer src, int dstOffset)
          Copies contents of the supplied buffer into this buffer.
 int transferFrom(JBuffer buffer)
          Copies contents of the supplied buffer into this buffer.
 int transferTo(java.nio.ByteBuffer dst, int srcOffset, int length)
          Copies contents of this buffer into supplied buffer.
 int transferTo(JBuffer dst)
          Copies the contents of this buffer into the supplied buffer.
 int transferTo(JBuffer dst, int srcOffset, int length, int dstOffset)
          Copies the contents of thsi buffer into the supplied buffer.
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, transferTo, transferTo, transferTo, transferTo0
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JBuffer

public JBuffer(JMemory.Type type)
Creates a.

Parameters:
type - memory model

JBuffer

public JBuffer(java.nio.ByteBuffer peer)
Instantiates a new j buffer.

Parameters:
peer - the peer

JBuffer

public JBuffer(int size)
Instantiates a new j buffer.

Parameters:
size - the size

JBuffer

public JBuffer(JMemory peer)
Instantiates a new j buffer.

Parameters:
peer - the peer

JBuffer

public JBuffer(byte[] data)
Instantiates a new j buffer.

Parameters:
data - the data
Method Detail

getByte

public byte getByte(int index)
Gets a signed 8-bit value.

Parameters:
index - offset into the buffer
Returns:
value from the buffer

getByteArray

public byte[] getByteArray(int index,
                           byte[] array)
Gets byte data from buffer and stores it in supplied array buffer.

Parameters:
index - offset into the buffer
array - byte array used to store the result where the length of the byte array determines the number of bytes to be copied from the buffer
Returns:
same array object passed in

getByteArray

public byte[] getByteArray(int index,
                           int size)
Gets the byte data from buffer and stores into newly allocated byte array.

Parameters:
index - offset into the buffer
size - number of bytes to copy and the size of the newly allocated byte array
Returns:
reference to new byte array containing the copied data

getByteArray

public byte[] getByteArray(int index,
                           byte[] array,
                           int offset,
                           int length)
Reads data from JBuffer into user supplied array.

Parameters:
index - starting position in the JBuffer
array - destination array
offset - starting position in the destination array
length - maximum number of bytes to copy
Returns:
the actual number of bytes copied which could be less then requested due to size of the JBuffer

getDouble

public double getDouble(int index)
Gets the java double value out of the buffer.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer

getFloat

public float getFloat(int index)
Gets the java float value out of the buffer.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer

getInt

public int getInt(int index)
Gets the java signed integer value from the buffer.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer

getLong

public long getLong(int index)
Gets the java signed long value from the buffer.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer

getShort

public short getShort(int index)
Gets the java signed short value from the buffer.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer

getUByte

public int getUByte(int index)
Gets the java usigned byte value.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer as next bigger java primitive type so that the sign of the value can be preserved since java does not allow unsigned primitives

getUInt

public long getUInt(int index)
Gets the java usigned int value.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer as next bigger java primitive type so that the sign of the value can be preserved since java does not allow unsigned primitives

getUShort

public int getUShort(int index)
Gets the java usigned short value.

Parameters:
index - offset into the buffer
Returns:
value read from the buffer as next bigger java primitive type so that the sign of the value can be preserved since java does not allow unsigned primitives

findUTF8String

public int findUTF8String(int index,
                          char... delimeter)
Find the delimiter array of chars within the buffer.

Parameters:
index - starting offset into the buffer
delimeter - array of chars to search for
Returns:
number of delimeter chars matched

getUTF8String

public java.lang.StringBuilder getUTF8String(int index,
                                             java.lang.StringBuilder buf,
                                             char... delimeter)
Retrieves all the characters from the buffer upto the delimiter char sequence.

Parameters:
index - offset into the buffer
buf - string buffer where to store the string retrieved from the buffer
delimeter - array of chars which will mark the end of the string
Returns:
the string buffer containing the retrieved string

getUTF8String

public java.lang.String getUTF8String(int index,
                                      char... delimeter)
Retrieves all the characters from the buffer upto the delimiter char sequence.

Parameters:
index - offset into the buffer
delimeter - array of chars which will mark the end of the string
Returns:
the string retrieved from the buffer

getUTF8String

public java.lang.StringBuilder getUTF8String(int index,
                                             java.lang.StringBuilder buf,
                                             int length)
Converts raw bytes to a java string. The length is the maximum length of the string to return.

Parameters:
index - byte index into the buffer to start
buf - string buffer where the retrieved string is stored
length - number of bytes to convert
Returns:
buffer containing the retrieved string

getUTF8String

public java.lang.String getUTF8String(int index,
                                      int length)
Gets the specified number of characters as a string.

Parameters:
index - offset into the buffer
length - number of UTF8 characters to retrieve
Returns:
retrived string

getUTF8Char

public char getUTF8Char(int index)
Converts a single byte to a java char.

Parameters:
index - index into the buffer
Returns:
converted UTF8 char

isReadonly

public boolean isReadonly()
Checks if this buffer is readonly. Read only buffers do not allow any mutable operations to be performed on the buffer.

Returns:
true if this buffer is read-only, otherwise false

order

public java.nio.ByteOrder order()
Gets the byte-order of this buffer. The buffer allows big and little endian byte ordering of the integer values accessed by this buffer.

Returns:
byte order of this buffer

order

public void order(java.nio.ByteOrder order)
Sets the byte ordering of integers for this buffer.

Parameters:
order - the new byte order for this integer

peer

public int peer(java.nio.ByteBuffer peer)
         throws PeeringException
Peers this buffer with a new buffer. The peer buffer's properties position and limit are used as starting and ending offsets for the peer operation.

Overrides:
peer in class JMemory
Parameters:
peer - the buffer to peer with
Returns:
number of byte peered
Throws:
PeeringException - the peering exception
See Also:
ByteBuffer.isDirect()

peer

public int peer(JBuffer peer)
Peers this buffer with the new buffer. The entire range of the buffer are peered.

Parameters:
peer - the buffer to peer with
Returns:
number of bytes peered

peer

public int peer(JBuffer peer,
                int offset,
                int length)
         throws java.lang.IndexOutOfBoundsException
Peers this buffer with a new buffer.

Parameters:
peer - buffer to peer with
offset - offset into the new peer buffer
length - number of bytes to peer
Returns:
number of bytes peered
Throws:
java.lang.IndexOutOfBoundsException - if offset and/or length are out of bounds

setByte

public void setByte(int index,
                    byte value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new value to be stored in the buffer

setByteArray

public void setByteArray(int index,
                         byte[] array)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
array - Array containing data to be set within the buffer. The length of the buffer determines the number of bytes to be copied into the buffer.

setDouble

public void setDouble(int index,
                      double value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new double value to be stored within the buffer

setFloat

public void setFloat(int index,
                     float value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new float value to be stored within the buffer

setInt

public void setInt(int index,
                   int value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new int value to be stored within the buffer

setLong

public void setLong(int index,
                    long value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new long value to be stored within the buffer

setShort

public void setShort(int index,
                     short value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new short value to be stored within the buffer

setShort0

public static void setShort0(long address,
                             boolean order,
                             int index,
                             short value)
Sets the short0.

Parameters:
address - the address
order - the order
index - the index
value - the value

setUByte

public void setUByte(int index,
                     int value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new usigned byte value to be stored within the buffer

setUInt

public void setUInt(int index,
                    long value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new usigned int value to be stored within the buffer

setUShort

public void setUShort(int index,
                      int value)
Sets a value in the buffer.

Parameters:
index - offset into the buffer
value - new unsigned short value to be stored within the buffer

transferFrom

public int transferFrom(byte[] buffer)
Copies contents of the supplied buffer into this buffer.

Overrides:
transferFrom in class JMemory
Parameters:
buffer - Source buffer to copy from. The array length determines the number of bytes to copy.
Returns:
number of bytes copied

transferFrom

public int transferFrom(java.nio.ByteBuffer src,
                        int dstOffset)
Copies contents of the supplied buffer into this buffer.

Overrides:
transferFrom in class JMemory
Parameters:
src - Source buffer to copy from. The position and limit properties of the buffer determine the bounds of the copy.
dstOffset - offset into this buffer where to start the copy
Returns:
number of bytes copied

transferFrom

public int transferFrom(JBuffer buffer)
Copies contents of the supplied buffer into this buffer.

Parameters:
buffer - Source buffer. The length of the source buffer determines the number of bytes to be copied.
Returns:
number of bytes copied

transferTo

public int transferTo(java.nio.ByteBuffer dst,
                      int srcOffset,
                      int length)
Copies contents of this buffer into supplied buffer.

Overrides:
transferTo in class JMemory
Parameters:
dst - destination buffer where to copy data to
srcOffset - offset into this buffer where to start the copy
length - number of bytes to copy
Returns:
number of bytes copied

transferTo

public int transferTo(JBuffer dst)
Copies the contents of this buffer into the supplied buffer.

Parameters:
dst - Destination buffer where to copy to. The number of bytes copied is determined by the size of source buffer.
Returns:
number of bytes copied

transferTo

public int transferTo(JBuffer dst,
                      int srcOffset,
                      int length,
                      int dstOffset)
Copies the contents of thsi buffer into the supplied buffer.

Overrides:
transferTo in class JMemory
Parameters:
dst - destination buffer where to copy to
srcOffset - offset into the source buffer where to start copy from
length - number of bytes to copy
dstOffset - offset into the destination buffer where to start copy to
Returns:
number of bytes copied

setByteBuffer

public void setByteBuffer(int index,
                          java.nio.ByteBuffer data)
Sets data within this buffer.

Parameters:
index - offset into this buffer
data - data to copy into this buffer. The position and limit of the data buffer set the bounds of the copy

peer

public int peer(JMemory src)
Peers this object with the supplied object. This object will be pointing at the same memory as the supplied object.

Overrides:
peer in class JMemory
Parameters:
src - source object that holds the memory location and size this object will point to
Returns:
size of the src and this object