1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 6 package org.rocksdb; 7 8 import java.util.Random; 9 10 /** 11 * Helper class to get the appropriate Random class instance dependent 12 * on the current platform architecture (32bit vs 64bit) 13 */ 14 public class PlatformRandomHelper { 15 /** 16 * Determine if OS is 32-Bit/64-Bit 17 * 18 * @return boolean value indicating if operating system is 64 Bit. 19 */ isOs64Bit()20 public static boolean isOs64Bit(){ 21 final boolean is64Bit; 22 if (System.getProperty("os.name").contains("Windows")) { 23 is64Bit = (System.getenv("ProgramFiles(x86)") != null); 24 } else { 25 is64Bit = (System.getProperty("os.arch").contains("64")); 26 } 27 return is64Bit; 28 } 29 30 /** 31 * Factory to get a platform specific Random instance 32 * 33 * @return {@link java.util.Random} instance. 34 */ getPlatformSpecificRandomFactory()35 public static Random getPlatformSpecificRandomFactory(){ 36 if (isOs64Bit()) { 37 return new Random(); 38 } 39 return new Random32Bit(); 40 } 41 42 /** 43 * Random32Bit is a class which overrides {@code nextLong} to 44 * provide random numbers which fit in size_t. This workaround 45 * is necessary because there is no unsigned_int < Java 8 46 */ 47 private static class Random32Bit extends Random { 48 @Override nextLong()49 public long nextLong(){ 50 return this.nextInt(Integer.MAX_VALUE); 51 } 52 } 53 54 /** 55 * Utility class constructor 56 */ PlatformRandomHelper()57 private PlatformRandomHelper() { } 58 } 59