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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | BasicArrayCache.CyclicStack<T>
A thread-safe stack-like data structure whose |
| private static class | BasicArrayCache.LazyHolder
Helper class for the singleton instance. |
| Modifier and Type | Field and Description |
|---|---|
| private final BasicArrayCache. | byteArrayCache
Stacks for cached byte arrays. |
| private static final int | CACHEABLE_SIZE_MIN
Arrays smaller than this many elements will not be cached. |
| private static final int | ELEMENTS_PER_STACK
Number of arrays of the same type and size to keep in the cache. |
| private final BasicArrayCache. | intArrayCache
Stacks for cached int arrays. |
| private static final int | STACKS_MAX
Number of stacks i.e. how many different array sizes to cache. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static <T> T | |
| public byte[] | getByteArray(int
size of the array to allocate size, boolean if true, all the elements of the returned
array will be zero; if false, the contents
of the returned array is undefined fillWithZeros)Overrides org. |
| public static BasicArrayCache | |
| public int[] | getIntArray(int
the minimum size of the array to allocate;
an implementation may return an array that
is larger than the given size, boolean sizeif true, the caller expects that the first
fillWithZeros)size elements in the array are zero;
if false, the array contents can be anything,
which speeds things up when reusing a cached
arrayOverrides org. |
| private static <T> void | putArray(BasicArrayCache.
Puts the |
| public void | putArray(byte[] array)
Overrides org. |
| public void | putArray(int[] array)
Overrides org. |
| byteArrayCache | back to summary |
|---|---|
| private final BasicArrayCache. Stacks for cached byte arrays. | |
| CACHEABLE_SIZE_MIN | back to summary |
|---|---|
| private static final int CACHEABLE_SIZE_MIN Arrays smaller than this many elements will not be cached. | |
| ELEMENTS_PER_STACK | back to summary |
|---|---|
| private static final int ELEMENTS_PER_STACK Number of arrays of the same type and size to keep in the cache. (ELEMENTS_PER_STACK - 1) is used as a bit mask so ELEMENTS_PER_STACK must be a power of two! | |
| intArrayCache | back to summary |
|---|---|
| private final BasicArrayCache. Stacks for cached int arrays. | |
| STACKS_MAX | back to summary |
|---|---|
| private static final int STACKS_MAX Number of stacks i.e. how many different array sizes to cache. | |
| BasicArrayCache | back to summary |
|---|---|
| public BasicArrayCache() | |
| getArray | back to summary |
|---|---|
| private static <T> T getArray(BasicArrayCache. Gets | |
| getByteArray | back to summary |
|---|---|
| public byte[] getByteArray(int size, boolean fillWithZeros) Overrides org. Allocates a new byte array, hopefully reusing an existing array from the cache.
| |
| getInstance | back to summary |
|---|---|
| public static BasicArrayCache getInstance() Returns a statically-allocated | |
| getIntArray | back to summary |
|---|---|
| public int[] getIntArray(int size, boolean fillWithZeros) Overrides org. This is like getByteArray but for int arrays.
| |
| putArray | back to summary |
|---|---|
| private static <T> void putArray(BasicArrayCache. Puts the | |
| putArray | back to summary |
|---|---|
| public void putArray(byte[] array) Overrides org. 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. | |
| putArray | back to summary |
|---|---|
| public void putArray(int[] array) Overrides org. 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. | |
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
This class won't be serialized but this is needed
to silence a compiler warning.
|
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected boolean | removeEldestEntry(Map.
The least recently inserted entry in the map, or if
this is an access-ordered map, the least recently accessed
entry. This is the entry that will be removed if this
method returns eldesttrue. If the map was empty prior
to the put or putAll invocation resulting
in this invocation, this will be the entry that was just
inserted; in other words, if the map contains a single
entry, the eldest entry is also the newest.Overrides java. |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. This class won't be serialized but this is needed to silence a compiler warning. | |
| CacheMap | back to summary |
|---|---|
| public CacheMap() Creates a new CacheMap. | |
| removeEldestEntry | back to summary |
|---|---|
| protected boolean removeEldestEntry(Map. Overrides java. Returns true if the map is full and the least recently used stack should be removed.
| |
push method
overwrites the oldest element in the stack if the stack is full.
| Modifier and Type | Field and Description |
|---|---|
| private final T[] | elements
Array that holds the elements in the cyclic stack. |
| private int | pos
Read-write position in the |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized T | |
| public synchronized void |
| elements | back to summary |
|---|---|
| private final T[] elements Array that holds the elements in the cyclic stack.
| |
| pos | back to summary |
|---|---|
| private int pos Read-write position in the
Note that | |
| CyclicStack | back to summary |
|---|---|
| private CyclicStack() | |
| pop | back to summary |
|---|---|
| public synchronized T pop() Gets the most recently added element from the stack.
If the stack is empty, | |
| push | back to summary |
|---|---|
| public synchronized void push(T e) Adds a new element to the stack. If the stack is full, the oldest element is overwritten. | |
getInstance() is called.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final BasicArrayCache |
| Access | Constructor and Description |
|---|---|
| private |
| INSTANCE | back to summary |
|---|---|
| pack-priv static final BasicArrayCache INSTANCE | |
| LazyHolder | back to summary |
|---|---|
| private LazyHolder() | |