Class BasicArrayCache


  • public class BasicArrayCache
    extends ArrayCache
    A basic 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

      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-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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BasicArrayCache

        public BasicArrayCache()
    • Method Detail

      • getInstance

        public static BasicArrayCache getInstance()
        Returns a statically-allocated BasicArrayCache 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 class ArrayCache
        Parameters:
        size - size of the array to allocate
        fillWithZeros - 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 class ArrayCache
      • getIntArray

        public int[] getIntArray​(int size,
                                 boolean fillWithZeros)
        This is like getByteArray but for int arrays.
        Overrides:
        getIntArray in class ArrayCache
        Parameters:
        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 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 class ArrayCache