com.neeve.pkt
Class PktIOBuffer

java.lang.Object
  extended by com.neeve.pkt.PktIOBuffer
All Implemented Interfaces:
com.neeve.util.UtlPool.Item<PktIOBuffer>, com.neeve.util.UtlReferenceTracker.HasReferenceTracker

public final class PktIOBuffer
extends Object
implements com.neeve.util.UtlPool.Item<PktIOBuffer>, com.neeve.util.UtlReferenceTracker.HasReferenceTracker

Implements an IO buffer.

A packet IO buffer is essentially a poolable ByteBuffer. It serves as the unit for IO (network or otherwise). A packet buffers (PktBuffer) serves as window into an IO buffer. The window spans the contents of the packet header or body that the packet buffer backs.

Threading:
As with the contained ByteBuffer object, a packet buffer is not thread safe i.e. only one thread can be executing the packet buffer methods at any point in time.

Field Summary
static boolean DISABLE_NATIVE_BUFFERS
           
 
Method Summary
 PktIOBuffer acquire()
          Acquire a reference to an IO buffer This method increments an IO buffer's ownership count.
static PktIOBuffer create(int length, boolean isNative)
          Create a new IO buffer
static PktIOBuffer createNative(int length)
          Force create a native buffer
 void dispose()
          Dispose of an IO buffer.
 ByteBuffer getBuffer()
          Get the underlying byte buffer.
 int getCapacity()
          Get the buffer capacity
 int getLimit()
          Get the buffer limit
 int getOwnerCount()
          Get an IO buffer's ownership count.
 com.neeve.util.UtlPool<PktIOBuffer> getPool()
          Implementation of UtlPool.Item#getPool.
 PktIOBuffer init()
          Implementation of UtlPool.Item#init.
 boolean isNative()
          Get whether underlying buffer is native
static boolean isNativePktIOBufferEnabled()
           
static int numSlabs()
          Get the number of slabs
 com.neeve.util.UtlReferenceTracker referenceTracker()
           
 PktIOBuffer setByteOrder(ByteOrder order)
          Set the buffer byte ordering
 PktIOBuffer setLimit(int val)
          Set the buffer limit An IO buffer limit serves as the marker for the maximum length of the buffer.
 PktIOBuffer setPool(com.neeve.util.UtlPool<PktIOBuffer> pool)
          Implementation of UtlPool.Item#setPool.
static int slab(int x)
           
static int smallestSlabSize()
          Get the smallest slab size
 String toString()
          Return a string representation of a buffer
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DISABLE_NATIVE_BUFFERS

public static final boolean DISABLE_NATIVE_BUFFERS
Method Detail

isNativePktIOBufferEnabled

public static final boolean isNativePktIOBufferEnabled()
Returns:
true if native PktIOBuffers are enabled.

create

public static PktIOBuffer create(int length,
                                 boolean isNative)
Create a new IO buffer

Parameters:
length - The length of the buffer to create.
isNative - Whether to create a buffer with native or heap storage.
Returns:
Returns the created IO buffer.
Threading:
This method is safe for concurrent access by multiple threads

createNative

public static PktIOBuffer createNative(int length)
Force create a native buffer

Parameters:
length - The length of the buffer to create.
Returns:
Returns the created IO buffer.
Threading:
This method is safe for concurrent access by multiple threads

This method creates a native buffer regardless of whether or not DISABLE_NATIVE_BUFFERS is true. This method should only be used in cases where a native buffer is an absolute necessity, e.g. when working with native code that requires such a buffer.


slab

public static final int slab(int x)

smallestSlabSize

public static final int smallestSlabSize()
Get the smallest slab size


numSlabs

public static final int numSlabs()
Get the number of slabs


setByteOrder

public final PktIOBuffer setByteOrder(ByteOrder order)
Set the buffer byte ordering


setLimit

public final PktIOBuffer setLimit(int val)
Set the buffer limit

An IO buffer limit serves as the marker for the maximum length of the buffer. The user of an IO buffer must ensure that data is not written beyond the limit marker since the IO buffer only wipes data upto the limit marker when recycled through its pool.


getLimit

public final int getLimit()
Get the buffer limit


getCapacity

public final int getCapacity()
Get the buffer capacity


getBuffer

public final ByteBuffer getBuffer()
Get the underlying byte buffer.


isNative

public final boolean isNative()
Get whether underlying buffer is native


acquire

public final PktIOBuffer acquire()
Acquire a reference to an IO buffer

This method increments an IO buffer's ownership count. Buffer ownership is used in conjunction with managing buffer pools. If a buffer is tagged to an object pool, the buffer is released back to the pool by dispose() when the ownership count reduces to 0. If not tagged to a pool, change of ownership count has no side effects.

A buffer's ownership count starts at 1.


getOwnerCount

public final int getOwnerCount()
Get an IO buffer's ownership count.


dispose

public final void dispose()
Dispose of an IO buffer.

This method should be invoked by the user when done with an IO buffer. It decrements the buffer's ownership count and releases it to its object pool (if one is tagged to it) and this method causes the ownership count to reduce to zero. If not tagged to a pool, ownership change methods have no side effects aside from just updating the ownership counter.

Upon return from this method, the caller should assume that the buffer has been disposed and release its reference to the buffer.


init

public final PktIOBuffer init()
Implementation of UtlPool.Item#init.

Specified by:
init in interface com.neeve.util.UtlPool.Item<PktIOBuffer>

setPool

public final PktIOBuffer setPool(com.neeve.util.UtlPool<PktIOBuffer> pool)
Implementation of UtlPool.Item#setPool.

Specified by:
setPool in interface com.neeve.util.UtlPool.Item<PktIOBuffer>

getPool

public final com.neeve.util.UtlPool<PktIOBuffer> getPool()
Implementation of UtlPool.Item#getPool.

Specified by:
getPool in interface com.neeve.util.UtlPool.Item<PktIOBuffer>

referenceTracker

public final com.neeve.util.UtlReferenceTracker referenceTracker()
Specified by:
referenceTracker in interface com.neeve.util.UtlReferenceTracker.HasReferenceTracker

toString

public final String toString()
Return a string representation of a buffer

Overrides:
toString in class Object


Copyright © 2015 Neeve Research, LLC. All Rights Reserved.