com.partnersoft.v3x.io
Class BufferPool

java.lang.Object
  extended by com.partnersoft.v3x.io.BufferPool

public class BufferPool
extends java.lang.Object

This goober allows careful individuals a chance to optimize operations which require byte buffers by maintaining a pool of these buffers which are reused.

Caution must be exercised to prevent memory-related errors such as those prevalent to C/C++ environments - acquiring and releasing buffers is essentially the same as malloc() and free(), and reuse of a buffer once you have released it could cause all kinds of trouble - perhaps overwriting data at the same time as another object that has legitimately acquired the buffer.

In general, follow these rules:

The benefits should be fairly obvious - buffers are reused rather than garbage collected, reducing the amount of allocation and gc needed. This means that larger, more efficient buffers are practical. In a single-threaded system, only a few buffers will be allocated.


Constructor Summary
BufferPool()
           
BufferPool(int bufferSize, int maxBuffers)
           
 
Method Summary
 byte[] catchBuffer()
          Grabs a buffer from the pool; allocates if necessary.
 int getBufferSize()
          Size (int bytes) of buffers pooled.
 int getMaxBuffers()
          Maximum number of buffers pooled.
 int getNumberOfBuffers()
          Gets the current pool size.
 void releaseBuffer(byte[] buffer)
          Releases a buffer back to the pool.
 void setBufferSize(int newSize)
          Set buffer size.
 void setMaxBuffers(int newMax)
          Set maximum number of buffers in pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferPool

public BufferPool(int bufferSize,
                  int maxBuffers)

BufferPool

public BufferPool()
Method Detail

getBufferSize

public int getBufferSize()
Size (int bytes) of buffers pooled.


setBufferSize

public void setBufferSize(int newSize)
Set buffer size. Clears the current buffer pool.


getMaxBuffers

public int getMaxBuffers()
Maximum number of buffers pooled.


setMaxBuffers

public void setMaxBuffers(int newMax)
Set maximum number of buffers in pool. Clears pool if new max is larger than current pool size.


getNumberOfBuffers

public int getNumberOfBuffers()
Gets the current pool size.


catchBuffer

public byte[] catchBuffer()
Grabs a buffer from the pool; allocates if necessary. Be sure to release it with releaseBuffer() when you're done with it so it can go back into the pool, eh? And don't go hanging onto a reference and modifying it while someone else is trying to use it, or I'll come find you and run over your garden gnomes with my truck.


releaseBuffer

public void releaseBuffer(byte[] buffer)
Releases a buffer back to the pool. Discards it if we already have a full pool or if it's size doesn't match our current bufferSize.