Utils

This utility class contains miscellaneous functions.

Methods
static Object callMethod(Object instance, String methodName, Object... params)
Calls an instance method via reflection.
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
static Object callStaticMethod(String classAndMethod, Object... params)
Calls a static method via reflection.
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
static byte[] cloneByteArray(byte[] b)
Create a new byte array and copy all the data.
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
static boolean compareSecure(byte[] test, byte[] good)
Compare two byte arrays.
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
static byte[] copy(byte[] source, byte[] target)
Copy the contents of the source array to the target array.
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
static byte[] copyBytes(byte[] bytes, int len)
Creates a copy of array of bytes with the new size.
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
static int getByteArrayHash(byte[] value)
Calculate the hash code of the given byte array.
static int getByteArrayHash(byte[] value)
Calculate the hash code of the given byte array.
Parameters:
value - the byte array
Returns:
the hash code
static Object getField(Object instance, String fieldName)
Returns a static field.
static Object getField(Object instance, String fieldName) throws Exception
Returns a static field.
Parameters:
instance - the instance on which the call is done
fieldName - the field name
Returns:
the field value
static long getGarbageCollectionTime()
static long getGarbageCollectionTime()
static int getMemoryFree()
Get the free memory in KB.
static int getMemoryFree()
Get the free memory in KB. This method possibly calls System.gc().
Returns:
the free memory
static long getMemoryMax()
Get the maximum memory in KB.
static long getMemoryMax()
Get the maximum memory in KB.
Returns:
the maximum memory
static int getMemoryUsed()
Get the used memory in KB.
static int getMemoryUsed()
Get the used memory in KB. This method possibly calls System.gc().
Returns:
the used memory
static Class getNonPrimitiveClass(Class clazz)
Convert primitive class names to java.lang.* class names.
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)
static String getProperty(String key, String defaultValue)
Get the system property.
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
static int getProperty(String key, int defaultValue)
Get the system property.
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
static boolean getProperty(String key, boolean defaultValue)
Get the system property.
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
static byte[] getResource(String name)
Get a resource from the resource map.
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
static Object getStaticField(String classAndField)
Returns a static field.
static Object getStaticField(String classAndField) throws Exception
Returns a static field.
Parameters:
classAndField - a string with the entire class and field name
Returns:
the field value
static boolean haveCommonComparableSuperclass(Class c1, Class c2)
Checks if given classes have a common Comparable superclass.
static boolean haveCommonComparableSuperclass(Class c1, 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
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 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
static boolean isClassPresent(String fullyQualifiedClassName)
Returns true if the class is present in the current class loader.
static boolean isClassPresent(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
static byte[] newBytes(int len)
Create an array of bytes with the given size.
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
static Object newInstance(String className, Object... params)
Creates a new instance.
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
static int[] newIntArray(int len)
Create an int array with the given size.
static int[] newIntArray(int len)
Create an int array with the given size.
Parameters:
len - the number of bytes requested
Returns:
the int array
static long[] newLongArray(int len)
Create a long array with the given size.
static long[] newLongArray(int len)
Create a long array with the given size.
Parameters:
len - the number of bytes requested
Returns:
the int array
static boolean parseBoolean(String value, boolean defaultValue, boolean throwException)
Parses the specified string to boolean value.
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 {@code throwException} is true
static int scaleForAvailableMemory(int value)
Scale the value with the available memory.
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
static void sortTopN(X[] array, int offset, int limit, Comparator comp)
Find the top limit values using given comparator and place them as in a full array sort, in descending order.
static void sortTopN(X[] array, int offset, int limit, Comparator 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.

Fields
static byte[] EMPTY_BYTES
static int[] EMPTY_INT_ARRAY

EMPTY_BYTES

An 0-size byte array.

EMPTY_INT_ARRAY

An 0-size int array.