Package org.h2.util
Class MathUtils
- java.lang.Object
-
- org.h2.util.MathUtils
-
public class MathUtils extends java.lang.Object
This is a utility class with mathematical helper functions.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.security.SecureRandom
secureRandom
The secure random object.(package private) static boolean
seeded
True if the secure random object is seeded.
-
Constructor Summary
Constructors Modifier Constructor Description private
MathUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static short
convertIntToShort(int i)
Convert an int value to a short value.static int
convertLongToInt(long l)
Convert a long value to an int value.static byte[]
generateAlternativeSeed()
Generate a seed value, using as much unpredictable data as possible.private static java.security.SecureRandom
getSecureRandom()
static int
nextPowerOf2(int x)
Get the value that is equal to or higher than this value, and that is a power of two.static void
randomBytes(byte[] bytes)
Get a number of pseudo random bytes.static int
randomInt(int lowerThan)
Get a pseudo random int value between 0 (including and the given value (excluding).static int
roundUpInt(int x, int blockSizePowerOf2)
Round the value up to the next block size.static long
roundUpLong(long x, long blockSizePowerOf2)
Round the value up to the next block size.static byte[]
secureRandomBytes(int len)
Get a number of cryptographically secure pseudo random bytes.static int
secureRandomInt(int lowerThan)
Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).static long
secureRandomLong()
Get a cryptographically secure pseudo random long value.(package private) static void
warn(java.lang.String s, java.lang.Throwable t)
Print a message to system output if there was a problem initializing the random number generator.
-
-
-
Method Detail
-
roundUpInt
public static int roundUpInt(int x, int blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.- Parameters:
x
- the value to be roundedblockSizePowerOf2
- the block size- Returns:
- the rounded value
-
roundUpLong
public static long roundUpLong(long x, long blockSizePowerOf2)
Round the value up to the next block size. The block size must be a power of two. As an example, using the block size of 8, the following rounding operations are done: 0 stays 0; values 1..8 results in 8, 9..16 results in 16, and so on.- Parameters:
x
- the value to be roundedblockSizePowerOf2
- the block size- Returns:
- the rounded value
-
getSecureRandom
private static java.security.SecureRandom getSecureRandom()
-
generateAlternativeSeed
public static byte[] generateAlternativeSeed()
Generate a seed value, using as much unpredictable data as possible.- Returns:
- the seed
-
warn
static void warn(java.lang.String s, java.lang.Throwable t)
Print a message to system output if there was a problem initializing the random number generator.- Parameters:
s
- the message to printt
- the stack trace
-
nextPowerOf2
public static int nextPowerOf2(int x) throws java.lang.IllegalArgumentException
Get the value that is equal to or higher than this value, and that is a power of two.- Parameters:
x
- the original value- Returns:
- the next power of two value
- Throws:
java.lang.IllegalArgumentException
- if x < 0 or x > 0x40000000
-
convertLongToInt
public static int convertLongToInt(long l)
Convert a long value to an int value. Values larger than the biggest int value are converted to the biggest int value, and values smaller than the smallest int value are converted to the smallest int value.- Parameters:
l
- the value to convert- Returns:
- the converted int value
-
convertIntToShort
public static short convertIntToShort(int i)
Convert an int value to a short value. Values larger than the biggest short value are converted to the biggest short value, and values smaller than the smallest short value are converted to the smallest short value.- Parameters:
i
- the value to convert- Returns:
- the converted short value
-
secureRandomLong
public static long secureRandomLong()
Get a cryptographically secure pseudo random long value.- Returns:
- the random long value
-
randomBytes
public static void randomBytes(byte[] bytes)
Get a number of pseudo random bytes.- Parameters:
bytes
- the target array
-
secureRandomBytes
public static byte[] secureRandomBytes(int len)
Get a number of cryptographically secure pseudo random bytes.- Parameters:
len
- the number of bytes- Returns:
- the random bytes
-
randomInt
public static int randomInt(int lowerThan)
Get a pseudo random int value between 0 (including and the given value (excluding). The value is not cryptographically secure.- Parameters:
lowerThan
- the value returned will be lower than this value- Returns:
- the random long value
-
secureRandomInt
public static int secureRandomInt(int lowerThan)
Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).- Parameters:
lowerThan
- the value returned will be lower than this value- Returns:
- the random long value
-
-