Class ZeroBytePool

java.lang.Object
de.tomatengames.util.pool.ZeroBytePool
All Implemented Interfaces:
BytePool

public class ZeroBytePool extends Object implements BytePool
A BytePool wrapper that clears (zeros) the provided byte arrays on free.

If this is the only access to the underlying BytePool, then all unused byte arrays are clear and are clear when claimed.

This may wrap other BytePools to improve

  • Data protection: Caller data does not remain in memory of the pool but instead gets immediately deleted when no longer used.
  • Fail-safety: Caller data written to and left in the byte array is never given to other callers. Callers should never use the contents of their claimed byte arrays without overwriting them first, but that may happen through bugs in caller code. Clearing the contents can drastically lower the severity of such a bug because it is just zeros then.
Unfortunately, clearing is a significant performance overhead, worsened by the lack of a standard accelerated way to do such clearing in Java. So, the decision to use this wrapper involves weighing security against efficiency.
Since:
1.8
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new ZeroBytePool that wraps the specified source BytePool.
  • Method Summary

    Modifier and Type
    Method
    Description
    Pooled<byte[]>
    claim(int minLength)
    Claims a byte array that is at least as long as the specified minimum length.
    static void
    zero(byte[] array)
    Clears all elements in the specified byte array by setting them to zero.
    static void
    zero(byte[] array, int off, int len)
    Clears the specified range in the specified byte array by setting its elements in that range to zero.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface BytePool

    ofLength
  • Constructor Details

    • ZeroBytePool

      public ZeroBytePool(BytePool source)
      Constructs a new ZeroBytePool that wraps the specified source BytePool.
      Parameters:
      source - the underlying BytePool to wrap
  • Method Details

    • zero

      public static void zero(byte[] array)
      Clears all elements in the specified byte array by setting them to zero.
      Parameters:
      array - the byte array to be zeroed out
    • zero

      public static void zero(byte[] array, int off, int len)
      Clears the specified range in the specified byte array by setting its elements in that range to zero.
      Parameters:
      array - the byte array to be zeroed out
      off - the starting index within the array where zeroing begins
      len - the number of bytes to set to zero
    • claim

      public Pooled<byte[]> claim(int minLength)
      Description copied from interface: BytePool
      Claims a byte array that is at least as long as the specified minimum length.
      Specified by:
      claim in interface BytePool
      Parameters:
      minLength - The minimum required length of the byte array.
      Returns:
      A Pooled instance wrapping a byte array of at least the specified minimum length.