Class BasicArrayCache
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.
- Since:
- 1.7
-
Constructor Summary
Constructors Constructor Description BasicArrayCache()
-
Method Summary
Modifier and Type Method 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-allocatedBasicArrayCache
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.Methods inherited from class org.tukaani.xz.ArrayCache
getDefaultCache, getDummyCache, setDefaultCache
-
Constructor Details
-
BasicArrayCache
public BasicArrayCache()
-
-
Method Details
-
getInstance
Returns a statically-allocatedBasicArrayCache
instance. This is often a good choice when a cache is needed. -
getByteArray
public byte[] getByteArray(int size, boolean fillWithZeros)Allocates a new byte array, hopefully reusing an existing array from the cache.- Overrides:
getByteArray
in classArrayCache
- Parameters:
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 undefined
-
putArray
public void putArray(byte[] array)Puts the given byte array to the cache. The caller must no longer use the array.Small arrays aren't cached and will be ignored by this method.
- Overrides:
putArray
in classArrayCache
-
getIntArray
public int[] getIntArray(int size, boolean fillWithZeros)This is like getByteArray but for int arrays.- Overrides:
getIntArray
in classArrayCache
- Parameters:
size
- the minimum size of the array to allocate; an implementation may return an array that is larger than the givensize
fillWithZeros
- if true, the caller expects that the firstsize
elements in the array are zero; if false, the array contents can be anything, which speeds things up when reusing a cached array
-
putArray
public void putArray(int[] array)Puts the given int array to the cache. The caller must no longer use the array.Small arrays aren't cached and will be ignored by this method.
- Overrides:
putArray
in classArrayCache
-