Class BasicArrayCache
- java.lang.Object
-
- org.tukaani.xz.ArrayCache
-
- org.tukaani.xz.BasicArrayCache
-
public class BasicArrayCache extends ArrayCache
A basicArrayCache
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 holdSoftReferences
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 viagetInstance()
which is a good choice in most situations where caching is wanted.- Since:
- 1.7
-
-
Constructor Summary
Constructors Constructor Description BasicArrayCache()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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
-
-
-
-
Method Detail
-
getInstance
public static BasicArrayCache 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
-
-