Sea 0.4.0

gov.lbl.dsd.sea.nio.util
Class ByteArrayPool

java.lang.Object
  extended bygov.lbl.dsd.sea.nio.util.ByteArrayPool

public final class ByteArrayPool
extends Object

Simple fixed-size thread-safe pool for recycling byte arrays. Each thread has its own local pool, and each pool can hold at most a given number of byte arrays, evicting arrays beyond that point, as configured through a system property.

Completely unsynchronized, yet thread-safe implementation.

Simple, but often effective if an application creates medium to large temporary arrays at very high frequency, as is often the case in safe and clean I/O buffer management for network servers.

This is not a general purpose replacement for Javas built-in memory and garbage collection subsystem! Do not abuse this class for small or infrequent arrays - those are handled better (read: near-optimal) by the generational garbage collectors and memory allocators of current VMs.

Version:
$Id: ByteArrayPool.java,v 1.5 2004/12/01 21:00:24 hoschek3 Exp $

Method Summary
static void put(byte[] bytes)
          Recycles the given byte array back to the pool.
static byte[] take(int minSize)
          Returns a byte array that has at least the given size.
static byte[] takeExactly(int size)
          Returns a byte array that has exactly the given size.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

takeExactly

public static byte[] takeExactly(int size)
Returns a byte array that has exactly the given size. If there is no such array in the pool, then creates and returns a new array with the given size.

Parameters:
size - the desired size of the byte array.
Returns:
Byte array of desired size.

take

public static byte[] take(int minSize)
Returns a byte array that has at least the given size. If there is no such array in the pool, then creates and returns a new array with the given minSize.

Parameters:
minSize - the minimum desired size of the byte array.
Returns:
Byte array of desired size.

put

public static void put(byte[] bytes)
Recycles the given byte array back to the pool.

Parameters:
bytes - the array to recycle

Sea 0.4.0