org.jnetpcap.packet
Class JMemoryPacket

java.lang.Object
  extended by org.jnetpcap.nio.JMemory
      extended by org.jnetpcap.nio.JBuffer
          extended by org.jnetpcap.packet.JPacket
              extended by org.jnetpcap.packet.JMemoryPacket
All Implemented Interfaces:
JHeaderAccessor

public class JMemoryPacket
extends JPacket

A heap based packet. This is a heap (native memory) based packet that can be instantiated without having to supply PcapHeader.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static class JMemoryPacket.JMemoryHeader
          A capture header that stores information about the creation of the packet.
 
Nested classes/interfaces inherited from class org.jnetpcap.packet.JPacket
JPacket.State
 
Nested classes/interfaces inherited from class org.jnetpcap.nio.JMemory
JMemory.Type
 
Field Summary
 
Fields inherited from class org.jnetpcap.packet.JPacket
DEFAULT_STATE_HEADER_COUNT, defaultScanner, memory, pool, state
 
Fields inherited from class org.jnetpcap.nio.JMemory
JNETPCAP_LIBRARY_NAME, MAX_DIRECT_MEMORY_DEFAULT, POINTER
 
Constructor Summary
JMemoryPacket(byte[] buffer)
          Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
JMemoryPacket(java.nio.ByteBuffer buffer)
          Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
JMemoryPacket(int size)
          Preallocates a packet with internal buffer of the supplied size.
JMemoryPacket(int id, byte[] buffer)
          Creates a new fully decoded packet from data provides in the buffer.
JMemoryPacket(int id, java.nio.ByteBuffer buffer)
          Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
JMemoryPacket(int id, JBuffer buffer)
          Creates a new fully decoded packet from data provides in the buffer.
JMemoryPacket(int id, java.lang.String hexdump)
          Creates a new fully decoded packet from the hexdump data provided.
JMemoryPacket(JBuffer buffer)
          Initializes the packet's state and data by doing a deep copy of the contents of the buffer.
JMemoryPacket(JMemory.Type type)
          Creates a potentially uninitialized packet with the specified memory type.
JMemoryPacket(JMemoryPacket packet)
          Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
JMemoryPacket(JPacket packet)
          Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 
Method Summary
 JMemoryPacket.JMemoryHeader getCaptureHeader()
          Retrieves this packets capture header.
 int getTotalSize()
          Calculates the total size of this packet which includes the size of the state structures and packet data.
 int peerStateAndData(java.nio.ByteBuffer buffer)
          Peers the contents of the buffer directly with this packet.
 int peerStateAndData(JBuffer buffer)
          Peers the contents of the buffer directly with this packet.
 int peerStateAndData(JBuffer buffer, int offset)
          Peers the contents of the buffer directly with this packet.
 void setWirelen(int wirelen)
          Changes the wirelen of this packet.
 int transferStateAndDataFrom(byte[] buffer)
          Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 int transferStateAndDataFrom(java.nio.ByteBuffer buffer)
          Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 int transferStateAndDataFrom(JBuffer buffer)
          Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 int transferStateAndDataFrom(JMemoryPacket packet)
          Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 int transferStateAndDataFrom(JPacket packet)
          Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 int transferStateAndDataTo(JBuffer buffer, int offset)
          Copies contents of this packet to buffer.
 int transferStateAndDataTo(JMemoryPacket packet)
          Copies both state and data to the supplied packet from this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated.
 
Methods inherited from class org.jnetpcap.packet.JPacket
allocate, getAllocatedMemorySize, getDefaultScanner, getFormatter, getFrameNumber, getHeader, getHeader, getHeaderByIndex, getHeaderCount, getHeaderIdByIndex, getHeaderInstanceCount, getMemoryBuffer, getMemoryBuffer, getMemoryBuffer, getMemoryBuffer, getMemoryPool, getPacketWirelen, getScanner, getState, hasHeader, hasHeader, hasHeader, hasHeader, remaining, remaining, scan, setFormatter, setMemoryPool, shutdown, toHexdump, toString
 
Methods inherited from class org.jnetpcap.nio.JBuffer
findUTF8String, getByte, getByteArray, getByteArray, getByteArray, getDouble, getFloat, getInt, getLong, getShort, getUByte, getUInt, getUShort, getUTF8Char, getUTF8String, getUTF8String, getUTF8String, getUTF8String, isReadonly, order, order, peer, peer, peer, peer, setByte, setByteArray, setByteBuffer, setDouble, setFloat, setInt, setLong, setShort, setShort0, setUByte, setUInt, setUShort, transferFrom, transferFrom, transferFrom, transferTo, transferTo, transferTo
 
Methods inherited from class org.jnetpcap.nio.JMemory
availableDirectMemory, check, cleanup, createReference, isInitialized, isJMemoryBasedOwner, isOwner, maxDirectMemory, peer, reservedDirectMemory, setSize, size, softDirectMemory, toDebugString, 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, wait, wait, wait
 

Constructor Detail

JMemoryPacket

public JMemoryPacket(byte[] buffer)
Initializes the packet's state and data by doing a deep copy of the contents of the buffer.

Parameters:
buffer - buffer containing both state and data in the form
 +--------------+-------------+
 | packet state | packet data |
 +--------------+-------------+
 

JMemoryPacket

public JMemoryPacket(java.nio.ByteBuffer buffer)
              throws PeeringException
Initializes the packet's state and data by doing a deep copy of the contents of the buffer.

Parameters:
buffer - buffer containing both state and data in the form
 +--------------+-------------+
 | packet state | packet data |
 +--------------+-------------+
 
Throws:
PeeringException - if there is a problem peering with the buffer

JMemoryPacket

public JMemoryPacket(int size)
Preallocates a packet with internal buffer of the supplied size.

Parameters:
size - number of bytes to pre allocate

JMemoryPacket

public JMemoryPacket(int id,
                     byte[] buffer)
Creates a new fully decoded packet from data provides in the buffer. The buffer contains raw packet data. The packet is peered with the buffer, allocating new memory if neccessary, and scanned using internal scanner.

Parameters:
id - numerical id of first protocol (DLT)
buffer - buffer containing raw packet data

JMemoryPacket

public JMemoryPacket(int id,
                     java.nio.ByteBuffer buffer)
              throws PeeringException
Initializes the packet's state and data by doing a deep copy of the contents of the buffer. This constructor also performs a scan of the packet.

Parameters:
id - ID of the DLT protocol
buffer - buffer containing both state and data in the form
 +--------------+-------------+
 | packet state | packet data |
 +--------------+-------------+
 
Throws:
PeeringException - the peering exception

JMemoryPacket

public JMemoryPacket(int id,
                     JBuffer buffer)
Creates a new fully decoded packet from data provides in the buffer. The buffer contains raw packet data. The packet is peered with the buffer, allocating new memory if neccessary, and scanned using internal scanner.

Parameters:
id - numerical id of first protocol (DLT)
buffer - buffer containing raw packet data

JMemoryPacket

public JMemoryPacket(int id,
                     java.lang.String hexdump)
Creates a new fully decoded packet from the hexdump data provided.

Parameters:
id - numerical id of first protocol (DLT)
hexdump - hexdump of the packet contents which will loaded into the raw data buffer

JMemoryPacket

public JMemoryPacket(JBuffer buffer)
Initializes the packet's state and data by doing a deep copy of the contents of the buffer.

Parameters:
buffer - buffer containing both state and data in the form
 +--------------+-------------+
 | packet state | packet data |
 +--------------+-------------+
 

JMemoryPacket

public JMemoryPacket(JMemoryPacket packet)
Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
packet - source packet

JMemoryPacket

public JMemoryPacket(JPacket packet)
Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
packet - source packet

JMemoryPacket

public JMemoryPacket(JMemory.Type type)
Creates a potentially uninitialized packet with the specified memory type.

Parameters:
type - type of memory model to use
Method Detail

getCaptureHeader

public JMemoryPacket.JMemoryHeader getCaptureHeader()
Retrieves this packets capture header.

Specified by:
getCaptureHeader in class JPacket
Returns:
capture header
See Also:
JPacket.getCaptureHeader()

getTotalSize

public int getTotalSize()
Calculates the total size of this packet which includes the size of the state structures and packet data.

Specified by:
getTotalSize in class JPacket
Returns:
total packet length in bytes
See Also:
JPacket.getTotalSize()

peerStateAndData

public int peerStateAndData(java.nio.ByteBuffer buffer)
                     throws PeeringException
Peers the contents of the buffer directly with this packet. No copies are performed but the packet state and data are expected to be contained within the buffer with a certain layout as described below:

Supplied buffer layout expected:

 +-----+----+
 |State|Data|
 +-----+----+
 

Parameters:
buffer - Buffer containing packet header, state and data. Position property specifies that start within the buffer where to peer the first byte.
Returns:
number of bytes peered
Throws:
PeeringException - thrown if ByteBuffer is not direct byte buffer type

peerStateAndData

public int peerStateAndData(JBuffer buffer)
Peers the contents of the buffer directly with this packet. No copies are performed but the packet state and data are expected to be contained within the buffer with a certain layout as described below:

Supplied buffer layout expected:

 +-----+----+
 |State|Data|
 +-----+----+
 

Parameters:
buffer - buffer containing packet header, state and data
Returns:
number of bytes peered

peerStateAndData

public int peerStateAndData(JBuffer buffer,
                            int offset)
Peers the contents of the buffer directly with this packet. No copies are performed but the packet state and data are expected to be contained within the buffer with a certain layout as described below:

Supplied buffer layout expected:

 +-----+----+
 |State|Data|
 +-----+----+
 

Parameters:
buffer - buffer containing packet header, state and data
offset - starting offset into the buffer
Returns:
number of bytes peered

setWirelen

public void setWirelen(int wirelen)
Changes the wirelen of this packet.

Parameters:
wirelen - new wirelen for this packet

transferStateAndDataFrom

public int transferStateAndDataFrom(byte[] buffer)
Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
buffer - buffer containing both state and data in the form
          +--------------+-------------+
          | packet state | packet data |
          +--------------+-------------+
 
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(java.nio.ByteBuffer buffer)
Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
buffer - buffer containing both state and data in the form
          +--------------+-------------+
          | packet state | packet data |
          +--------------+-------------+
 
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(JBuffer buffer)
Performs a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
buffer - buffer containing both state and data in the form
          +--------------+-------------+
          | packet state | packet data |
          +--------------+-------------+
 
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(JMemoryPacket packet)
Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
packet - source packet
Returns:
number of bytes copied

transferStateAndDataFrom

public int transferStateAndDataFrom(JPacket packet)
Copies both state and data from supplied packet to this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
packet - source packet
Returns:
number of bytes copied

transferStateAndDataTo

public int transferStateAndDataTo(JBuffer buffer,
                                  int offset)
Copies contents of this packet to buffer. The packets capture state and packet data are copied to new buffer. After completion of this operation the complete contents and state of the packet will be transfered to the buffer. The layout of the buffer data will be as described below. A buffer with this type of layout is suitable for any transferStateAndData or peer methods for any buffers that are JMemory based. The buffer has to be large enough to hold all of the packet content as returned by method

Parameters:
buffer - buffer containing both state and data in the form
 +--------------+-------------+
 | packet state | packet data |
 +--------------+-------------+
 
offset - the offset
Returns:
number of bytes copied getTotalSize(). If the buffer is too small and a runtime exception may be thrown.

The buffer layout will look like the following:

 +-----+----+
 |State|Data|
 +-----+----+
 


transferStateAndDataTo

public int transferStateAndDataTo(JMemoryPacket packet)
Copies both state and data to the supplied packet from this packet by performing a deep copy of the contents of the buffer into packet's internal memory buffer if that buffer is large enough, otherwise a new buffer is allocated. Both packet's state and data are then peered with the internal buffer containing the copy of the supplied buffer

Parameters:
packet - destination packet
Returns:
number of bytes copied