public class BasicArrayCache extends ArrayCache
ArrayCache
implementation.
This caches exact array sizes, that is, getByteArray
will return
an array whose size is exactly the requested size. A limited number
of different array sizes are cached at the same time; least recently used
sizes will be dropped from the cache if needed (can happen if several
different (de)compression options are used with the same cache).
The current implementation uses
LinkedHashMap
to map different array sizes
to separate array-based data structures which hold
SoftReferences
to the cached arrays.
In the common case this should give good performance and fairly low
memory usage overhead.
A statically allocated global BasicArrayCache
instance is
available via getInstance()
which is a good choice in most
situations where caching is wanted.
Constructor and Description |
---|
BasicArrayCache() |
Modifier and Type | Method and Description |
---|---|
byte[] |
getByteArray(int size,
boolean fillWithZeros)
Allocates a new byte array, hopefully reusing an existing
array from the cache.
|
static BasicArrayCache |
getInstance()
Returns a statically-allocated
BasicArrayCache instance. |
int[] |
getIntArray(int size,
boolean fillWithZeros)
This is like getByteArray but for int arrays.
|
void |
putArray(byte[] array)
Puts the given byte array to the cache.
|
void |
putArray(int[] array)
Puts the given int array to the cache.
|
getDefaultCache, getDummyCache, setDefaultCache
public static BasicArrayCache getInstance()
BasicArrayCache
instance.
This is often a good choice when a cache is needed.public byte[] getByteArray(int size, boolean fillWithZeros)
getByteArray
in class ArrayCache
size
- size of the array to allocatefillWithZeros
- if true, all the elements of the returned
array will be zero; if false, the contents
of the returned array is undefinedpublic void putArray(byte[] array)
Small arrays aren't cached and will be ignored by this method.
putArray
in class ArrayCache
public int[] getIntArray(int size, boolean fillWithZeros)
getIntArray
in class ArrayCache
size
- the minimum size of the array to allocate;
an implementation may return an array that
is larger than the given size
fillWithZeros
- if true, the caller expects that the first
size
elements in the array are zero;
if false, the array contents can be anything,
which speeds things up when reusing a cached
arraypublic void putArray(int[] array)
Small arrays aren't cached and will be ignored by this method.
putArray
in class ArrayCache