edu.nps.moves.disutil
Class ByteBufferPool

java.lang.Object
  extended by edu.nps.moves.disutil.ByteBufferPool

public class ByteBufferPool
extends java.lang.Object

A pool that holds ByteBuffer objects.

We'd like to reuse buffers for sending, but this is tricky because the Java NIO byte buffers can't expand. So if we try to marshal a PDU that is 144 bytes long and one that is 800 bytes long, we'd always need a byte buffer that is 800 bytes long. This has some obvious problems if we generate 2000 byte buffer objects, which will have a big memory footprint that won't be GC'd. If this turns out to be a problem--I suspect not, right now--we can implement some sort of clear() operation to wipe out the pool once some criteria is met.

Instances of this class are defitnely not thread-safe. If you have two threads, you should have a ByteBufferPool for each thread to ensure that the same byte buffer from the pool isn't being used by two threads.

Author:
DMcG

Constructor Summary
ByteBufferPool()
           
 
Method Summary
 void clear()
          Removes all the byte buffers from the pool, allowing them to be GC'd.
 java.nio.ByteBuffer getByteBufferOfLength(int length)
          Returns a byte buffer from the pool that has the given length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteBufferPool

public ByteBufferPool()
Method Detail

getByteBufferOfLength

public java.nio.ByteBuffer getByteBufferOfLength(int length)
Returns a byte buffer from the pool that has the given length. If there is not ByteBuffer with that length, it is created and added to the pool.

Parameters:
length -
Returns:
The byte buffer with the given length

clear

public void clear()
Removes all the byte buffers from the pool, allowing them to be GC'd.