Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8361100

SecureRandom nextLong memory usage

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 26
    • security-libs
    • None
    • minimal
    • Java API
    • SE

      Summary

      SecureRandom nextLong memory usage enhancement.

      Problem

      SecureRandom uses straightforward implementations inherited from Random but in the process does double the memory allocations necessary. The delegation to SecureRandom.engineNextBytes does not provide int or long values, the caller must allocate a byte array and assemble the value itself.

      Solution

      Provide an explicit implementation of the nextLong() method in SecureRandom, which is currently inherited from Random. The new implementation overrides this method to call nextBytes() with an 8-byte array and converts the resulting bytes into a single long value, eliminating the need for multiple allocations.

      Specification

      This method overrides the implementation inherited from {@code Random} to avoid double allocation. It generates a single 8-byte value using {@code nextBytes()} and returns the result as a {@code long}.

      /** * {@inheritDoc} */ @Override public long nextLong() { }

            kthirupattur Koushik Muthukrishnan Thirupattur
            rriggs Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: