Package org.h2.util

Class Utils

java.lang.Object
org.h2.util.Utils

public class Utils extends Object
This utility class contains miscellaneous functions.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    The utility methods will try to use the provided class factories to convert binary name of class to Class object.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte[]
    An 0-size byte array.
    static final int[]
    An 0-size int array.
    private static final HashMap<String,byte[]>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static Object
    callMethod(Object instance, Class<?> clazz, String methodName, Object... params)
     
    static Object
    callMethod(Object instance, String methodName, Object... params)
    Calls an instance method via reflection.
    static Object
    callStaticMethod(String classAndMethod, Object... params)
    Calls a static method via reflection.
    static byte[]
    cloneByteArray(byte[] b)
    Create a new byte array and copy all the data.
    static void
    Run Java memory garbage collection.
    static boolean
    compareSecure(byte[] test, byte[] good)
    Compare two byte arrays.
    static byte[]
    copy(byte[] source, byte[] target)
    Copy the contents of the source array to the target array.
    static byte[]
    copyBytes(byte[] bytes, int len)
    Creates a copy of array of bytes with the new size.
    static long
    Returns the current value of the high-resolution time source.
    static long
    Returns the current value of the high-resolution time source plus the specified offset.
    static int
    getByteArrayHash(byte[] value)
    Calculate the hash code of the given byte array.
    static long
     
    static long
     
    static long
    Get the free memory in KB.
    static long
    Get the maximum memory in KB.
    static long
    Get the used memory in KB.
    static Class<?>
    Convert primitive class names to java.lang.* class names.
    static boolean
    getProperty(String key, boolean defaultValue)
    Get the system property.
    static int
    getProperty(String key, int defaultValue)
    Get the system property.
    static String
    getProperty(String key, String defaultValue)
    Get the system property.
    static byte[]
    Get a resource from the resource map.
    static int
    indexOf(byte[] bytes, byte[] pattern, int start)
    Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index.
    private static byte[]
     
    private static int
    match(Class<?>[] params, Object[] values)
     
    static long
    nanoTimePlusMillis(long nanoTime, int ms)
    Returns the current value of the high-resolution time source plus the specified offset.
    static byte[]
    newBytes(int len)
    Create an array of bytes with the given size.
    static Object
    newInstance(String className, Object... params)
    Creates a new instance.
    static <T> ArrayList<T>
    Create a new ArrayList with an initial capacity of 4.
    static boolean
    parseBoolean(String value, boolean defaultValue, boolean throwException)
    Parses the specified string to boolean value.
    private static <X> void
    partialQuickSort(X[] array, int low, int high, Comparator<? super X> comp, int start, int end)
    Partial quick sort.
    static int
    Scale the value with the available memory.
    static <X> void
    sortTopN(X[] array, int fromInclusive, int toExclusive, Comparator<? super X> comp)
    Find the top limit values using given comparator and place them as in a full array sort, in descending order.

    Methods inherited from class java.lang.Object

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

    • EMPTY_BYTES

      public static final byte[] EMPTY_BYTES
      An 0-size byte array.
    • EMPTY_INT_ARRAY

      public static final int[] EMPTY_INT_ARRAY
      An 0-size int array.
    • RESOURCES

      private static final HashMap<String,byte[]> RESOURCES
  • Constructor Details

    • Utils

      private Utils()
  • Method Details

    • indexOf

      public static int indexOf(byte[] bytes, byte[] pattern, int start)
      Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index. This methods returns -1 if the pattern has not been found, and the start position if the pattern is empty.
      Parameters:
      bytes - the byte array
      pattern - the pattern
      start - the start index from where to search
      Returns:
      the index
    • getByteArrayHash

      public static int getByteArrayHash(byte[] value)
      Calculate the hash code of the given byte array.
      Parameters:
      value - the byte array
      Returns:
      the hash code
    • compareSecure

      public static boolean compareSecure(byte[] test, byte[] good)
      Compare two byte arrays. This method will always loop over all bytes and doesn't use conditional operations in the loop to make sure an attacker can not use a timing attack when trying out passwords.
      Parameters:
      test - the first array
      good - the second array
      Returns:
      true if both byte arrays contain the same bytes
    • copy

      public static byte[] copy(byte[] source, byte[] target)
      Copy the contents of the source array to the target array. If the size if the target array is too small, a larger array is created.
      Parameters:
      source - the source array
      target - the target array
      Returns:
      the target array or a new one if the target array was too small
    • newBytes

      public static byte[] newBytes(int len)
      Create an array of bytes with the given size. If this is not possible because not enough memory is available, an OutOfMemoryError with the requested size in the message is thrown.

      This method should be used if the size of the array is user defined, or stored in a file, so wrong size data can be distinguished from regular out-of-memory.

      Parameters:
      len - the number of bytes requested
      Returns:
      the byte array
      Throws:
      OutOfMemoryError - if the allocation was too large
    • copyBytes

      public static byte[] copyBytes(byte[] bytes, int len)
      Creates a copy of array of bytes with the new size. If this is not possible because not enough memory is available, an OutOfMemoryError with the requested size in the message is thrown.

      This method should be used if the size of the array is user defined, or stored in a file, so wrong size data can be distinguished from regular out-of-memory.

      Parameters:
      bytes - source array
      len - the number of bytes in the new array
      Returns:
      the byte array
      Throws:
      OutOfMemoryError - if the allocation was too large
      See Also:
    • cloneByteArray

      public static byte[] cloneByteArray(byte[] b)
      Create a new byte array and copy all the data. If the size of the byte array is zero, the same array is returned.
      Parameters:
      b - the byte array (may not be null)
      Returns:
      a new byte array
    • getMemoryUsed

      public static long getMemoryUsed()
      Get the used memory in KB. This method possibly calls System.gc().
      Returns:
      the used memory
    • getMemoryFree

      public static long getMemoryFree()
      Get the free memory in KB. This method possibly calls System.gc().
      Returns:
      the free memory
    • getMemoryMax

      public static long getMemoryMax()
      Get the maximum memory in KB.
      Returns:
      the maximum memory
    • getGarbageCollectionTime

      public static long getGarbageCollectionTime()
    • getGarbageCollectionCount

      public static long getGarbageCollectionCount()
    • collectGarbage

      public static void collectGarbage()
      Run Java memory garbage collection.
    • newSmallArrayList

      public static <T> ArrayList<T> newSmallArrayList()
      Create a new ArrayList with an initial capacity of 4.
      Type Parameters:
      T - the type
      Returns:
      the object
    • sortTopN

      public static <X> void sortTopN(X[] array, int fromInclusive, int toExclusive, Comparator<? super X> comp)
      Find the top limit values using given comparator and place them as in a full array sort, in descending order.
      Type Parameters:
      X - the type of elements
      Parameters:
      array - the array.
      fromInclusive - the start index, inclusive
      toExclusive - the end index, exclusive
      comp - the comparator.
    • partialQuickSort

      private static <X> void partialQuickSort(X[] array, int low, int high, Comparator<? super X> comp, int start, int end)
      Partial quick sort.

      Works with elements from low to high indexes, inclusive.

      Moves smallest elements to low..start-1 positions and largest elements to end+1..high positions. Middle elements are placed into start..end positions. All these regions aren't fully sorted.

      Type Parameters:
      X - the type of elements
      Parameters:
      array - the array to sort
      low - the lower index with data, inclusive
      high - the higher index with data, inclusive, high > low
      comp - the comparator
      start - the start index of requested region, inclusive
      end - the end index of requested region, inclusive, end >= start
    • getResource

      public static byte[] getResource(String name) throws IOException
      Get a resource from the resource map.
      Parameters:
      name - the name of the resource
      Returns:
      the resource data
      Throws:
      IOException - on failure
    • loadResource

      private static byte[] loadResource(String name) throws IOException
      Throws:
      IOException
    • callStaticMethod

      public static Object callStaticMethod(String classAndMethod, Object... params) throws Exception
      Calls a static method via reflection. This will try to use the method where the most parameter classes match exactly (this algorithm is simpler than the one in the Java specification, but works well for most cases).
      Parameters:
      classAndMethod - a string with the entire class and method name, eg. "java.lang.System.gc"
      params - the method parameters
      Returns:
      the return value from this call
      Throws:
      Exception - on failure
    • callMethod

      public static Object callMethod(Object instance, String methodName, Object... params) throws Exception
      Calls an instance method via reflection. This will try to use the method where the most parameter classes match exactly (this algorithm is simpler than the one in the Java specification, but works well for most cases).
      Parameters:
      instance - the instance on which the call is done
      methodName - a string with the method name
      params - the method parameters
      Returns:
      the return value from this call
      Throws:
      Exception - on failure
    • callMethod

      private static Object callMethod(Object instance, Class<?> clazz, String methodName, Object... params) throws Exception
      Throws:
      Exception
    • newInstance

      public static Object newInstance(String className, Object... params) throws Exception
      Creates a new instance. This will try to use the constructor where the most parameter classes match exactly (this algorithm is simpler than the one in the Java specification, but works well for most cases).
      Parameters:
      className - a string with the entire class, eg. "java.lang.Integer"
      params - the constructor parameters
      Returns:
      the newly created object
      Throws:
      Exception - on failure
    • match

      private static int match(Class<?>[] params, Object[] values)
    • getNonPrimitiveClass

      public static Class<?> getNonPrimitiveClass(Class<?> clazz)
      Convert primitive class names to java.lang.* class names.
      Parameters:
      clazz - the class (for example: int)
      Returns:
      the non-primitive class (for example: java.lang.Integer)
    • parseBoolean

      public static boolean parseBoolean(String value, boolean defaultValue, boolean throwException)
      Parses the specified string to boolean value.
      Parameters:
      value - string to parse
      defaultValue - value to return if value is null or on parsing error
      throwException - throw exception on parsing error or return default value instead
      Returns:
      parsed or default value
      Throws:
      IllegalArgumentException - on parsing error if throwException is true
    • getProperty

      public static String getProperty(String key, String defaultValue)
      Get the system property. If the system property is not set, or if a security exception occurs, the default value is returned.
      Parameters:
      key - the key
      defaultValue - the default value
      Returns:
      the value
    • getProperty

      public static int getProperty(String key, int defaultValue)
      Get the system property. If the system property is not set, or if a security exception occurs, the default value is returned.
      Parameters:
      key - the key
      defaultValue - the default value
      Returns:
      the value
    • getProperty

      public static boolean getProperty(String key, boolean defaultValue)
      Get the system property. If the system property is not set, or if a security exception occurs, the default value is returned.
      Parameters:
      key - the key
      defaultValue - the default value
      Returns:
      the value
    • scaleForAvailableMemory

      public static int scaleForAvailableMemory(int value)
      Scale the value with the available memory. If 1 GB of RAM is available, the value is returned, if 2 GB are available, then twice the value, and so on.
      Parameters:
      value - the value to scale
      Returns:
      the scaled value
    • currentNanoTime

      public static long currentNanoTime()
      Returns the current value of the high-resolution time source.
      Returns:
      time in nanoseconds, never equal to 0
      See Also:
    • currentNanoTimePlusMillis

      public static long currentNanoTimePlusMillis(int ms)
      Returns the current value of the high-resolution time source plus the specified offset.
      Parameters:
      ms - additional offset in milliseconds
      Returns:
      time in nanoseconds, never equal to 0
      See Also:
    • nanoTimePlusMillis

      public static long nanoTimePlusMillis(long nanoTime, int ms)
      Returns the current value of the high-resolution time source plus the specified offset.
      Parameters:
      nanoTime - time in nanoseconds
      ms - additional offset in milliseconds
      Returns:
      time in nanoseconds, never equal to 0
      See Also: