[cvs] / jsr166 / src / main / java / util / SplittableRandom.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/SplittableRandom.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.24, Mon Oct 7 10:54:27 2013 UTC revision 1.25, Tue Dec 2 12:17:38 2014 UTC
# Line 25  Line 25 
25    
26  package java.util;  package java.util;
27    
 import java.net.NetworkInterface;  
28  import java.util.concurrent.atomic.AtomicLong;  import java.util.concurrent.atomic.AtomicLong;
29  import java.util.function.IntConsumer;  import java.util.function.IntConsumer;
30  import java.util.function.LongConsumer;  import java.util.function.LongConsumer;
# Line 140  Line 139 
139       * other cases, this split must be performed in a thread-safe       * other cases, this split must be performed in a thread-safe
140       * manner, so we use an AtomicLong to represent the seed rather       * manner, so we use an AtomicLong to represent the seed rather
141       * than use an explicit SplittableRandom. To bootstrap the       * than use an explicit SplittableRandom. To bootstrap the
142       * defaultGen, we start off using a seed based on current time and       * defaultGen, we start off using a seed based on current time
143       * network interface address unless the java.util.secureRandomSeed       * unless the java.util.secureRandomSeed property is set. This
144       * property is set. This serves as a slimmed-down (and insecure)       * serves as a slimmed-down (and insecure) variant of SecureRandom
145       * variant of SecureRandom that also avoids stalls that may occur       * that also avoids stalls that may occur when using /dev/random.
      * when using /dev/random.  
146       *       *
147       * It is a relatively simple matter to apply the basic design here       * It is a relatively simple matter to apply the basic design here
148       * to use 128 bit seeds. However, emulating 128bit arithmetic and       * to use 128 bit seeds. However, emulating 128bit arithmetic and
# Line 237  Line 235 
235                  s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);                  s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
236              return s;              return s;
237          }          }
238          long h = 0L;          return (mix64(System.currentTimeMillis()) ^
         try {  
             Enumeration<NetworkInterface> ifcs =  
                     NetworkInterface.getNetworkInterfaces();  
             boolean retry = false; // retry once if getHardwareAddress is null  
             while (ifcs.hasMoreElements()) {  
                 NetworkInterface ifc = ifcs.nextElement();  
                 if (!ifc.isVirtual()) { // skip fake addresses  
                     byte[] bs = ifc.getHardwareAddress();  
                     if (bs != null) {  
                         int n = bs.length;  
                         int m = Math.min(n >>> 1, 4);  
                         for (int i = 0; i < m; ++i)  
                             h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];  
                         if (m < 4)  
                             h = (h << 8) ^ bs[n-1-m];  
                         h = mix64(h);  
                         break;  
                     }  
                     else if (!retry)  
                         retry = true;  
                     else  
                         break;  
                 }  
             }  
         } catch (Exception ignore) {  
         }  
         return (h ^ mix64(System.currentTimeMillis()) ^  
239                  mix64(System.nanoTime()));                  mix64(System.nanoTime()));
240      }      }
241    

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.25

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8