org.jnetpcap.nio
Class JBuffer

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
All Implemented Interfaces:
JByteBuffer
Direct Known Subclasses:
JHeader, JObjectBuffer, JPacket

public class JBuffer
extends JMemory
implements JByteBuffer

A direct ByteBuffer 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
 
Constructor Summary
JBuffer(byte[] data)
           
JBuffer(java.nio.ByteBuffer peer)
           
JBuffer(int size)
           
JBuffer(JMemory.Type type)
           
JBuffer(JMemory peer)
           
 
Method Summary
 int findUTF8String(int index, char... delimeter)
           
 byte getByte(int index)
           
 byte[] getByteArray(int index, byte[] array)
           
 byte[] getByteArray(int index, int size)
           
 double getDouble(int index)
           
 float getFloat(int index)
           
 int getInt(int index)
           
 long getLong(int index)
           
 short getShort(int index)
           
 int getUByte(int index)
           
 long getUInt(int index)
           
 int getUShort(int index)
           
 char getUTF8Char(int index)
          Converts a single byte to a java char.
 java.lang.String getUTF8String(int index, char... delimeter)
          Converts raw bytes to a java string.
 java.lang.String getUTF8String(int index, int length)
           
 java.lang.StringBuilder getUTF8String(int index, java.lang.StringBuilder buf, char... delimeter)
           
 java.lang.StringBuilder getUTF8String(int index, java.lang.StringBuilder buf, int length)
          Converts raw bytes to a java string.
 boolean isReadonly()
           
 java.nio.ByteOrder order()
           
 void order(java.nio.ByteOrder order)
           
 int peer(java.nio.ByteBuffer peer)
          Peers the src structure with this instance.
 int peer(JBuffer peer)
           
 int peer(JBuffer peer, int offset, int length)
           
 int peer(JMemory src)
          Peers this object with the supplied object.
 void setByte(int index, byte value)
           
 void setByteArray(int index, byte[] array)
           
 void setByteBuffer(int i, java.nio.ByteBuffer data)
           
 void setDouble(int index, double value)
           
 void setFloat(int index, float value)
           
 void setInt(int index, int value)
           
 void setLong(int index, long value)
           
 void setShort(int index, short value)
           
 void setUByte(int index, int value)
           
 void setUInt(int index, long value)
           
 void setUShort(int index, int value)
           
 int transferFrom(byte[] buffer)
          Copies contents of byte array to memory
 int transferFrom(java.nio.ByteBuffer src, int dstOffset)
          Copies data from memory from direct byte buffer to this memory
 int transferFrom(JBuffer buffer)
           
 int transferTo(java.nio.ByteBuffer dst, int srcOffset, int length)
          Copies teh contents of this memory to buffer
 int transferTo(JBuffer dst)
           
 int transferTo(JBuffer dst, int srcOffset, int length, int dstOffset)
          Transfers the contents of this memory to buffer.
 
Methods inherited from class org.jnetpcap.nio.JMemory
check, cleanup, finalize, isInitialized, isJMemoryBasedOwner, isOwner, peer, setSize, size, toDebugString, toHexdump, toHexdump, totalActiveAllocated, totalAllocateCalls, totalAllocated, totalAllocatedSegments0To255Bytes, totalAllocatedSegments256OrAbove, totalDeAllocateCalls, totalDeAllocated, transferFrom, transferFrom, transferFromDirect, transferOwnership, transferTo, transferTo, transferTo, transferTo, transferTo
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jnetpcap.nio.JByteBuffer
size
 

Constructor Detail

JBuffer

public JBuffer(JMemory.Type type)
Parameters:
type - TODO

JBuffer

public JBuffer(java.nio.ByteBuffer peer)
Parameters:
peer -

JBuffer

public JBuffer(int size)
Parameters:
size -

JBuffer

public JBuffer(JMemory peer)
Parameters:
peer -

JBuffer

public JBuffer(byte[] data)
Parameters:
data -
Method Detail

getByte

public byte getByte(int index)
Specified by:
getByte in interface JByteBuffer

getByteArray

public byte[] getByteArray(int index,
                           byte[] array)
Specified by:
getByteArray in interface JByteBuffer

getByteArray

public byte[] getByteArray(int index,
                           int size)
Specified by:
getByteArray in interface JByteBuffer

getDouble

public double getDouble(int index)
Specified by:
getDouble in interface JByteBuffer

getFloat

public float getFloat(int index)
Specified by:
getFloat in interface JByteBuffer

getInt

public int getInt(int index)
Specified by:
getInt in interface JByteBuffer

getLong

public long getLong(int index)
Specified by:
getLong in interface JByteBuffer

getShort

public short getShort(int index)
Specified by:
getShort in interface JByteBuffer

getUByte

public int getUByte(int index)
Specified by:
getUByte in interface JByteBuffer

getUInt

public long getUInt(int index)
Specified by:
getUInt in interface JByteBuffer

getUShort

public int getUShort(int index)
Specified by:
getUShort in interface JByteBuffer

findUTF8String

public int findUTF8String(int index,
                          char... delimeter)

getUTF8String

public java.lang.StringBuilder getUTF8String(int index,
                                             java.lang.StringBuilder buf,
                                             char... delimeter)

getUTF8String

public java.lang.String getUTF8String(int index,
                                      char... delimeter)
Converts raw bytes to a java string. The delimeter is used to end the string or the end of the buffer is used. The delimiter is included in the returned string.

Parameters:
index - byte index into the buffer to start
delimiter - delimiter series of chars to search for
Returns:
string which includes the delimiter

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
length - number of bytes to convert
Returns:
string of at most length bytes

getUTF8String

public java.lang.String getUTF8String(int index,
                                      int length)

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()

order

public java.nio.ByteOrder order()
Specified by:
order in interface JByteBuffer

order

public void order(java.nio.ByteOrder order)
Specified by:
order in interface JByteBuffer

peer

public int peer(java.nio.ByteBuffer peer)
         throws PeeringException
Description copied from class: JMemory
Peers the src structure with this instance. The physical memory that the src peered object points to is set to this instance. The owner flag is not copied and src remains at the same state as it did before. This instance does not become the owner of the memory.

Further more, since we are peering with a ByteBuffer, the actual memory that is peered is between ByteBuffer's position and limit properties. Those 2 properties determine which portion of the memory that will be peered. This allows a larger ByteBuffer to be peered with different objects providing rudimentary memory allocation mechanism.

Lastly care must be taken, to ensure that the lifespans do not conflict. The memory that we are peering to must not be deallocated prior the termination of the lifespan of this object or at minimum calling JMemory.cleanup() method to ensure that this object no longer references memory which may have been or become deallocated.

Overrides:
peer in class JMemory
Parameters:
peer - The ByteBuffer whose allocated native memory we want to peer with. The ByteByffer must be if direct buffer type which can be checked using ByteBuffer.isDirect() call.
Throws:
PeeringException
See Also:
ByteBuffer.isDirect()

peer

public int peer(JBuffer peer)

peer

public int peer(JBuffer peer,
                int offset,
                int length)
         throws java.lang.IndexOutOfBoundsException
Throws:
java.lang.IndexOutOfBoundsException

setByte

public void setByte(int index,
                    byte value)
Specified by:
setByte in interface JByteBuffer

setByteArray

public void setByteArray(int index,
                         byte[] array)
Specified by:
setByteArray in interface JByteBuffer

setDouble

public void setDouble(int index,
                      double value)
Specified by:
setDouble in interface JByteBuffer

setFloat

public void setFloat(int index,
                     float value)
Specified by:
setFloat in interface JByteBuffer

setInt

public void setInt(int index,
                   int value)
Specified by:
setInt in interface JByteBuffer

setLong

public void setLong(int index,
                    long value)
Specified by:
setLong in interface JByteBuffer

setShort

public void setShort(int index,
                     short value)
Specified by:
setShort in interface JByteBuffer

setUByte

public void setUByte(int index,
                     int value)
Specified by:
setUByte in interface JByteBuffer

setUInt

public void setUInt(int index,
                    long value)
Specified by:
setUInt in interface JByteBuffer

setUShort

public void setUShort(int index,
                      int value)
Specified by:
setUShort in interface JByteBuffer

transferFrom

public int transferFrom(byte[] buffer)
Description copied from class: JMemory
Copies contents of byte array to memory

Overrides:
transferFrom in class JMemory
Parameters:
buffer - source buffer
Returns:
number of bytes copied

transferFrom

public int transferFrom(java.nio.ByteBuffer src,
                        int dstOffset)
Description copied from class: JMemory
Copies data from memory from direct byte buffer to this memory

Overrides:
transferFrom in class JMemory
Parameters:
src - source buffer
dstOffset - offset into our memory location
Returns:
actual number of bytes that was copied

transferFrom

public int transferFrom(JBuffer buffer)

transferTo

public int transferTo(java.nio.ByteBuffer dst,
                      int srcOffset,
                      int length)
Description copied from class: JMemory
Copies teh contents of this memory to buffer

Overrides:
transferTo in class JMemory
Parameters:
dst - destination buffer
srcOffset - offset in source
length - number of bytes to copy
Returns:
number of bytes copied

transferTo

public int transferTo(JBuffer dst)

transferTo

public int transferTo(JBuffer dst,
                      int srcOffset,
                      int length,
                      int dstOffset)
Description copied from class: JMemory
Transfers the contents of this memory to buffer.

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

setByteBuffer

public void setByteBuffer(int i,
                          java.nio.ByteBuffer data)
Parameters:
i -
data -

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