java.lang.Object
net.i2p.util.SimpleByteCache

public final class SimpleByteCacheextends Object
Like ByteCache but works directly with byte arrays, not ByteArrays. These are designed to be small caches. Unlike ByteCache, the arrays are not zeroed before being returned to the cache, so a caller that needs cleanup must zero the array itself.

A single global cleanup task runs once a minute over all caches and halves any cache that has been underfilled for 90 seconds, so the array sizes of an idle program are not retained indefinitely.

Only the static methods are public here.

  • Method Details

    • getInstance

      public static SimpleByteCache getInstance(int size)
      Cache responsible for arrays of the given size, using the default size.
      Parameters:
      size - how large should the objects cached be?
      Returns:
      the shared cache holding arrays of size bytes
    • getInstance

      public static SimpleByteCache getInstance(int cacheSize, int size)
      Cache responsible for objects of the given size.

      There is one cache per size for the life of the JVM, and cacheSize is (re)applied to it whenever it differs from the size the cache already holds: a later call with a different cacheSize resizes the shared cache, discarding the excess on a shrink. The arrays evicted are simply garbage collected - nothing zeroes them.

      Parameters:
      cacheSize - how many objects (NOT memory bytes) to keep before discarding released objects
      size - how large should the objects cached be?
      Returns:
      the cache for arrays of size bytes, shared by every caller that asked for the same size
    • clearAll

      public static void clearAll()
      Clear everything (memory pressure).
    • acquire

      public static byte[] acquire(int size)
      Next available array, either from the cache or a brand new one.
      Parameters:
      size - how large should the object be?
      Returns:
      a byte array of exactly that length, taken from the cache when one of that size is free and allocated otherwise; never null
    • release

      public static void release(byte[] entry)
      Put this array back onto the available cache for reuse.
      Parameters:
      entry - the array to recycle; ignored if null, or if no cache exists for its length