Package 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
Nested Classes Modifier and Type Class Description 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
Fields Modifier and Type Field Description static byte[]
EMPTY_BYTES
An 0-size byte array.static int[]
EMPTY_INT_ARRAY
An 0-size int array.private static java.util.HashMap<java.lang.String,byte[]>
RESOURCES
-
Constructor Summary
Constructors Modifier Constructor Description private
Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.lang.Object
callMethod(java.lang.Object instance, java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... params)
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 void
collectGarbage()
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
currentNanoTime()
Returns the current value of the high-resolution time source.static long
currentNanoTimePlusMillis(int ms)
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
getGarbageCollectionCount()
static long
getGarbageCollectionTime()
static long
getMemoryFree()
Get the free memory in KB.static long
getMemoryMax()
Get the maximum memory in KB.static long
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 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[]
loadResource(java.lang.String name)
private static int
match(java.lang.Class<?>[] params, java.lang.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 java.lang.Object
newInstance(java.lang.String className, java.lang.Object... params)
Creates a new instance.static <T> java.util.ArrayList<T>
newSmallArrayList()
Create a new ArrayList with an initial capacity of 4.static boolean
parseBoolean(java.lang.String value, boolean defaultValue, boolean throwException)
Parses the specified string to boolean value.private static <X> void
partialQuickSort(X[] array, int low, int high, java.util.Comparator<? super X> comp, int start, int end)
Partial quick sort.static int
scaleForAvailableMemory(int value)
Scale the value with the available memory.static <X> void
sortTopN(X[] array, int fromInclusive, int toExclusive, 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.
-
-
-
Method Detail
-
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 arraypattern
- the patternstart
- 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 arraygood
- 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 arraytarget
- 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:
java.lang.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 arraylen
- the number of bytes in the new array- Returns:
- the byte array
- Throws:
java.lang.OutOfMemoryError
- if the allocation was too large- See Also:
Arrays.copyOf(byte[], int)
-
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> java.util.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, 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.- Type Parameters:
X
- the type of elements- Parameters:
array
- the array.fromInclusive
- the start index, inclusivetoExclusive
- the end index, exclusivecomp
- the comparator.
-
partialQuickSort
private static <X> void partialQuickSort(X[] array, int low, int high, java.util.Comparator<? super X> comp, int start, int end)
Partial quick sort.Works with elements from
low
tohigh
indexes, inclusive.Moves smallest elements to
low..start-1
positions and largest elements toend+1..high
positions. Middle elements are placed intostart..end
positions. All these regions aren't fully sorted.- Type Parameters:
X
- the type of elements- Parameters:
array
- the array to sortlow
- the lower index with data, inclusivehigh
- the higher index with data, inclusive,high > low
comp
- the comparatorstart
- the start index of requested region, inclusiveend
- the end index of requested region, inclusive,end >= start
-
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
- on failure
-
loadResource
private static byte[] loadResource(java.lang.String name) throws java.io.IOException
- 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
- on failure
-
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 donemethodName
- a string with the method nameparams
- the method parameters- Returns:
- the return value from this call
- Throws:
java.lang.Exception
- on failure
-
callMethod
private static java.lang.Object callMethod(java.lang.Object instance, java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Object... params) throws java.lang.Exception
- 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
- on failure
-
match
private static int match(java.lang.Class<?>[] params, java.lang.Object[] values)
-
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)
-
parseBoolean
public static boolean parseBoolean(java.lang.String value, boolean defaultValue, boolean throwException)
Parses the specified string to boolean value.- Parameters:
value
- string to parsedefaultValue
- value to return if value is null or on parsing errorthrowException
- throw exception on parsing error or return default value instead- Returns:
- parsed or default value
- Throws:
java.lang.IllegalArgumentException
- on parsing error ifthrowException
is true
-
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 keydefaultValue
- 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 keydefaultValue
- 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 keydefaultValue
- 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:
System.nanoTime()
-
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:
System.nanoTime()
-
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 nanosecondsms
- additional offset in milliseconds- Returns:
- time in nanoseconds, never equal to 0
- See Also:
System.nanoTime()
-
-