## org.h2.util
Class Utils

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

    public class Utils

extends java.lang.Object

This utility class contains miscellaneous functions.

Nested Class Summary
`static interface` `Utils.ClassFactory` The utility methods will try to use the provided class factories to convert binary name of class to Class object.

Field Summary
`static byte[]` `EMPTY_BYTES` An 0-size byte array.
`static int[]` `EMPTY_INT_ARRAY` An 0-size int array.

Method Summary
`static java.lang.Object` `callMethod(java.lang.Object instance, java.lang.String methodName, java.lang.Object... params)` Calls an instance method via reflection.
`static java.lang.Object` `callStaticMethod(java.lang.String classAndMethod, java.lang.Object... params)` Calls a static method via reflection.
`static byte[]` `cloneByteArray(byte[] b)` Create a new byte array and copy all the data.
`static int` `compareNotNullSigned(byte[] data1, byte[] data2)` Compare the contents of two byte arrays.
`static int` `compareNotNullUnsigned(byte[] data1, byte[] data2)` Compare the contents of two byte arrays.
`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 int` `getByteArrayHash(byte[] value)` Calculate the hash code of the given byte array.
`static java.lang.Object` `getField(java.lang.Object instance, java.lang.String fieldName)` Returns a static field.
`static int` `getMemoryFree()` Get the free memory in KB.
`static long` `getMemoryMax()` Get the maximum memory in KB.
`static int` `getMemoryUsed()` Get the used memory in KB.
`static java.lang.Class<?>` `getNonPrimitiveClass(java.lang.Class<?> clazz)` Convert primitive class names to java.lang.* class names.
`static boolean` `getProperty(java.lang.String key, boolean defaultValue)` Get the system property.
`static int` `getProperty(java.lang.String key, int defaultValue)` Get the system property.
`static java.lang.String` `getProperty(java.lang.String key, java.lang.String defaultValue)` Get the system property.
`static byte[]` `getResource(java.lang.String name)` Get a resource from the resource map.
`static java.lang.Object` `getStaticField(java.lang.String classAndField)` Returns a static field.
`static int` `hashCode(java.lang.Object o)` Calculate the hash code of the given object.
`static boolean` `haveCommonComparableSuperclass(java.lang.Class<?> c1, java.lang.Class<?> c2)` Checks if given classes have a common Comparable superclass.
`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.
`static boolean` `isClassPresent(java.lang.String fullyQualifiedClassName)` Returns true if the class is present in the current class loader.
`static java.lang.Object` `newInstance(java.lang.String className, java.lang.Object... params)` Creates a new instance.
`static int[]` `newIntArray(int len)` Create an int array with the given size.
`static long[]` `newLongArray(int len)` Create a long array with the given size.
`static long` `readLong(byte[] buff, int pos)` Read a long value from the byte array at the given position.

`static

`<X> void`

`

`sortTopN(X[] array, int offset, int limit, java.util.Comparator<? super X> comp)`
Find the top limit values using given comparator and place them as in a full array sort, in descending order.

`static void` `writeLong(byte[] buff, int pos, long x)` Write a long value to the byte array at the given position.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### 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.

Method Detail

### writeLong

    public static void writeLong(byte[] buff,
                                 int pos,
                                 long x)

  Write a long value to the byte array at the given position. The most significant byte is written first.

  Parameters:
    `buff` - the byte array
    `pos` - the position
    `x` - the value to write

### readLong

    public static long readLong(byte[] buff,
                                int pos)

  Read a long value from the byte array at the given position. The most significant byte is read first.

  Parameters:
    `buff` - the byte array
    `pos` - the position
  Returns:
    the value

### 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

### compareNotNullSigned

    public static int compareNotNullSigned(byte[] data1,
                                           byte[] data2)

  Compare the contents of two byte arrays. If the content or length of the first array is smaller than the second array, -1 is returned. If the content or length of the second array is smaller than the first array, 1 is returned. If the contents and lengths are the same, 0 is returned.

  This method interprets bytes as signed.

  Parameters:
    `data1` - the first byte array (must not be null)
    `data2` - the second byte array (must not be null)
  Returns:
    the result of the comparison (-1, 1 or 0)

### compareNotNullUnsigned

    public static int compareNotNullUnsigned(byte[] data1,
                                             byte[] data2)

  Compare the contents of two byte arrays. If the content or length of the first array is smaller than the second array, -1 is returned. If the content or length of the second array is smaller than the first array, 1 is returned. If the contents and lengths are the same, 0 is returned.

  This method interprets bytes as unsigned.

  Parameters:
    `data1` - the first byte array (must not be null)
    `data2` - the second byte array (must not be null)
  Returns:
    the result of the comparison (-1, 1 or 0)

### 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

### 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

### hashCode

    public static int hashCode(java.lang.Object o)

  Calculate the hash code of the given object. The object may be null.

  Parameters:
    `o` - the object
  Returns:
    the hash code, or 0 if the object is null

### getMemoryUsed

    public static int getMemoryUsed()

  Get the used memory in KB. This method possibly calls System.gc().

  Returns:
    the used memory

### getMemoryFree

    public static int 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

### newIntArray

    public static int[] newIntArray(int len)

  Create an int array with the given size.

  Parameters:
    `len` - the number of bytes requested
  Returns:
    the int array

### newLongArray

    public static long[] newLongArray(int len)

  Create a long array with the given size.

  Parameters:
    `len` - the number of bytes requested
  Returns:
    the int array

### sortTopN

    public static <X> void sortTopN(X[] array,
                                    int offset,
                                    int limit,
                                    java.util.Comparator<? super X> comp)

  Find the top limit values using given comparator and place them as in a full array sort, in descending order.

  Parameters:
    `array` - the array.
    `offset` - the offset.
    `limit` - the limit.
    `comp` - the comparator.

### haveCommonComparableSuperclass

    public static boolean haveCommonComparableSuperclass(java.lang.Class<?> c1,
                                                         java.lang.Class<?> c2)

  Checks if given classes have a common Comparable superclass.

  Parameters:
    `c1` - the first class
    `c2` - the second class
  Returns:
    true if they have

### getResource

    public static byte[] getResource(java.lang.String name)
                              throws java.io.IOException

  Get a resource from the resource map.

  Parameters:
    `name` - the name of the resource
  Returns:
    the resource data
  Throws:
    `java.io.IOException`

### callStaticMethod

    public static java.lang.Object callStaticMethod(java.lang.String classAndMethod,
                                                    java.lang.Object... params)
                                             throws java.lang.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:
    `java.lang.Exception`

### callMethod

    public static java.lang.Object callMethod(java.lang.Object instance,
                                              java.lang.String methodName,
                                              java.lang.Object... params)
                                       throws java.lang.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:
    `java.lang.Exception`

### newInstance

    public static java.lang.Object newInstance(java.lang.String className,
                                               java.lang.Object... params)
                                        throws java.lang.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:
    `java.lang.Exception`

### getStaticField

    public static java.lang.Object getStaticField(java.lang.String classAndField)
                                           throws java.lang.Exception

  Returns a static field.

  Parameters:
    `classAndField` - a string with the entire class and field name
  Returns:
    the field value
  Throws:
    `java.lang.Exception`

### getField

    public static java.lang.Object getField(java.lang.Object instance,
                                            java.lang.String fieldName)
                                     throws java.lang.Exception

  Returns a static field.

  Parameters:
    `instance` - the instance on which the call is done
    `fieldName` - the field name
  Returns:
    the field value
  Throws:
    `java.lang.Exception`

### isClassPresent

    public static boolean isClassPresent(java.lang.String fullyQualifiedClassName)

  Returns true if the class is present in the current class loader.

  Parameters:
    `fullyQualifiedClassName` - a string with the entire class name, eg. "java.lang.System"
  Returns:
    true if the class is present

### getNonPrimitiveClass

    public static java.lang.Class<?> getNonPrimitiveClass(java.lang.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)

### getProperty

    public static java.lang.String getProperty(java.lang.String key,
                                               java.lang.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(java.lang.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(java.lang.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