All Implemented Interfaces:
IRandomStandalone, Serializable, Cloneable, Runnable

public class AsyncFortunaStandaloneextends FortunaStandalone implements Runnable
Fortuna instance that tries to avoid blocking if at all possible by using separate filled buffer segments rather than one buffer (and blocking when that buffer's data has been eaten). Note that this class is not fully Thread safe! The following methods must be synchronized externally, they are not synced here or in super(): addRandomByte(), addRandomBytes(), nextByte(), nextBytes(), seed(byte[])
See Also:
  • Field Details

    • DEFAULT_BUFFERS

      private static final int DEFAULT_BUFFERS
      Number of buffers filled in the background.
      See Also:
    • DEFAULT_BUFSIZE

      private static final int DEFAULT_BUFSIZE
      Size of each background buffer, in bytes.
    • _bufferCount

      private final int _bufferCount
      How many buffers are cycled through, never less than 2.
    • _bufferSize

      private final int _bufferSize
      The size in bytes of each buffer, never less than 16384.
    • asyncBuffers

      private final Object asyncBuffers
      The lock.
    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private final Log _log
      The logger for this class.
    • _isRunning

      private volatile boolean _isRunning
      True until shutdown() is called, which stops the refill thread.
    • _refillThread

      private Thread _refillThread
      The thread that refills empty buffers in the background.
    • _fullBuffers

      Buffers holding freshly generated output, waiting to be consumed.
    • _emptyBuffers

      Consumed buffers waiting to be refilled.
    • _currentBuffer

      private AsyncFortunaStandalone.AsyncBuffer _currentBuffer
      The buffer the PRNG is currently handing out bytes from.
  • Constructor Details

    • AsyncFortunaStandalone

      public AsyncFortunaStandalone(I2PAppContext context)
      Create a new AsyncFortunaStandalone.
      Parameters:
      context - the I2P app context
  • Method Details

    • clone

      public Object clone()
      Return a copy of this object.
      Specified by:
      clone in interface IRandomStandalone
      Overrides:
      clone in class FortunaStandalone
      Returns:
      a clone copy of this instance.
    • startup

      public void startup()
      Start the PRNG.
    • shutdown

      public void shutdown()
      Stop the PRNG.
    • seed

      public void seed(byte[] val)
      The seed is only propogated once the prng is started with startup().
      Overrides:
      seed in class FortunaStandalone
      Parameters:
      val - the seed data (unused)
    • rotateBuffer

      protected void rotateBuffer()
      Make the next available filled buffer current, scheduling any unfilled buffers for refill, and blocking until at least one buffer is ready.
    • run

      public void run()
      The refiller thread. Runs until shutdown() is called, refilling empty buffers and swapping out full ones.
      Specified by:
      run in interface Runnable
    • fillBlock

      public void fillBlock()
      Fill the PRNG output block.
      Overrides:
      fillBlock in class FortunaStandalone
    • doFill

      private void doFill(byte[] buf)
      Fills one buffer with random bytes.
      Parameters:
      buf - the buffer to fill