Package org.h2.util

Class MathUtils

java.lang.Object
org.h2.util.MathUtils

public class MathUtils extends Object
This is a utility class with mathematical helper functions.
  • Method Summary

    Modifier and Type
    Method
    Description
    static short
    Convert an int value to a short value.
    static int
    Convert a long value to an int value.
    static byte[]
    Generate a seed value, using as much unpredictable data as possible.
    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[]
    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
    Get a cryptographically secure pseudo random long value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • 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 rounded
      blockSizePowerOf2 - 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 rounded
      blockSizePowerOf2 - the block size
      Returns:
      the rounded value
    • generateAlternativeSeed

      public static byte[] generateAlternativeSeed()
      Generate a seed value, using as much unpredictable data as possible.
      Returns:
      the seed
    • nextPowerOf2

      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 - the original value
      Returns:
      the next power of two value
      Throws:
      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