Top Description Fields Constructors Methods
org.h2.util

public Class MathUtils

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .DataOutputStream, .IOException, java.lang.reflect.Method, java.nio.charset.StandardCharsets, java.security.SecureRandom, java.util.concurrent.ThreadLocalRandom

This is a utility class with mathematical helper functions.

Field Summary

Modifier and TypeField and Description
pack-priv static SecureRandom
secureRandom

The secure random object.

pack-priv static volatile boolean
seeded

True if the secure random object is seeded.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static short

Returns:

the converted short value
convertIntToShort
(int
the value to convert
i
)

Convert an int value to a short value.

public static int

Returns:

the converted int value
convertLongToInt
(long
the value to convert
l
)

Convert a long value to an int value.

public static byte[]

Returns:

the seed
generateAlternativeSeed
()

Generate a seed value, using as much unpredictable data as possible.

private static synchronized SecureRandom
public static int

Returns:

the next power of two value
nextPowerOf2
(int
the original value
x
)

Get the value that is equal to or higher than this value, and that is a power of two.

public static void
randomBytes(byte[]
the target array
bytes
)

Get a number of pseudo random bytes.

public static int

Returns:

the random long value
randomInt
(int
the value returned will be lower than this value
lowerThan
)

Get a pseudo random int value between 0 (including and the given value (excluding).

public static int

Returns:

the rounded value
roundUpInt
(int
the value to be rounded
x
,
int
the block size
blockSizePowerOf2
)

Round the value up to the next block size.

public static long

Returns:

the rounded value
roundUpLong
(long
the value to be rounded
x
,
long
the block size
blockSizePowerOf2
)

Round the value up to the next block size.

public static byte[]

Returns:

the random bytes
secureRandomBytes
(int
the number of bytes
len
)

Get a number of cryptographically secure pseudo random bytes.

public static int

Returns:

the random long value
secureRandomInt
(int
the value returned will be lower than this value
lowerThan
)

Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).

public static long

Returns:

the random long value
secureRandomLong
()

Get a cryptographically secure pseudo random long value.

pack-priv static void
warn(String
the message to print
s
,
Throwable
the stack trace
t
)

Print a message to system output if there was a problem initializing the random number generator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

secureRandomback to summary
pack-priv static SecureRandom secureRandom

The secure random object.

seededback to summary
pack-priv static volatile boolean seeded

True if the secure random object is seeded.

Constructor Detail

MathUtilsback to summary
private MathUtils()

Method Detail

convertIntToShortback to summary
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:int

the value to convert

Returns:short

the converted short value

convertLongToIntback to summary
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:long

the value to convert

Returns:int

the converted int value

generateAlternativeSeedback to summary
public static byte[] generateAlternativeSeed()

Generate a seed value, using as much unpredictable data as possible.

Returns:byte[]

the seed

getSecureRandomback to summary
private static synchronized SecureRandom getSecureRandom()
nextPowerOf2back to summary
public static int nextPowerOf2(int x) throws IllegalArgumentException

Get the value that is equal to or higher than this value, and that is a power of two.

Parameters
x:int

the original value

Returns:int

the next power of two value

Exceptions
IllegalArgumentException:
if x < 0 or x > 0x40000000
randomBytesback to summary
public static void randomBytes(byte[] bytes)

Get a number of pseudo random bytes.

Parameters
bytes:byte[]

the target array

randomIntback to summary
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:int

the value returned will be lower than this value

Returns:int

the random long value

roundUpIntback to summary
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:int

the value to be rounded

blockSizePowerOf2:int

the block size

Returns:int

the rounded value

roundUpLongback to summary
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:long

the value to be rounded

blockSizePowerOf2:long

the block size

Returns:long

the rounded value

secureRandomBytesback to summary
public static byte[] secureRandomBytes(int len)

Get a number of cryptographically secure pseudo random bytes.

Parameters
len:int

the number of bytes

Returns:byte[]

the random bytes

secureRandomIntback to summary
public static int secureRandomInt(int lowerThan)

Get a cryptographically secure pseudo random int value between 0 (including and the given value (excluding).

Parameters
lowerThan:int

the value returned will be lower than this value

Returns:int

the random long value

secureRandomLongback to summary
public static long secureRandomLong()

Get a cryptographically secure pseudo random long value.

Returns:long

the random long value

warnback to summary
pack-priv static void warn(String s, Throwable t)

Print a message to system output if there was a problem initializing the random number generator.

Parameters
s:String

the message to print

t:Throwable

the stack trace