public class Utils
extends java.lang.Object
| Modifier and Type | Class and 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.
|
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EMPTY_BYTES
An 0-size byte array.
|
static int[] |
EMPTY_INT_ARRAY
An 0-size int array.
|
| Modifier and Type | Method and Description |
|---|---|
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 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 long |
getGarbageCollectionCount() |
static long |
getGarbageCollectionTime() |
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 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 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 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 <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.
|
static int |
scaleForAvailableMemory(int value)
Scale the value with the available memory.
|
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.
|
public static final byte[] EMPTY_BYTES
public static final int[] EMPTY_INT_ARRAY
public static int indexOf(byte[] bytes,
byte[] pattern,
int start)
bytes - the byte arraypattern - the patternstart - the start index from where to searchpublic static int getByteArrayHash(byte[] value)
value - the byte arraypublic static boolean compareSecure(byte[] test,
byte[] good)
test - the first arraygood - the second arraypublic static byte[] copy(byte[] source,
byte[] target)
source - the source arraytarget - the target arraypublic static byte[] newBytes(int len)
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.
len - the number of bytes requestedjava.lang.OutOfMemoryError - if the allocation was too largepublic static byte[] copyBytes(byte[] bytes,
int len)
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.
bytes - source arraylen - the number of bytes in the new arrayjava.lang.OutOfMemoryError - if the allocation was too largeArrays.copyOf(byte[], int)public static byte[] cloneByteArray(byte[] b)
b - the byte array (may not be null)public static int getMemoryUsed()
public static int getMemoryFree()
public static long getMemoryMax()
public static long getGarbageCollectionTime()
public static long getGarbageCollectionCount()
public static void collectGarbage()
public static int[] newIntArray(int len)
len - the number of bytes requestedpublic static <T> java.util.ArrayList<T> newSmallArrayList()
T - the typepublic static long[] newLongArray(int len)
len - the number of bytes requestedpublic static <X> void sortTopN(X[] array,
int offset,
int limit,
java.util.Comparator<? super X> comp)
array - the array.offset - the offset.limit - the limit.comp - the comparator.public static boolean haveCommonComparableSuperclass(java.lang.Class<?> c1,
java.lang.Class<?> c2)
c1 - the first classc2 - the second classpublic static byte[] getResource(java.lang.String name)
throws java.io.IOException
name - the name of the resourcejava.io.IOExceptionpublic static java.lang.Object callStaticMethod(java.lang.String classAndMethod,
java.lang.Object... params)
throws java.lang.Exception
classAndMethod - a string with the entire class and method name, eg.
"java.lang.System.gc"params - the method parametersjava.lang.Exceptionpublic static java.lang.Object callMethod(java.lang.Object instance,
java.lang.String methodName,
java.lang.Object... params)
throws java.lang.Exception
instance - the instance on which the call is donemethodName - a string with the method nameparams - the method parametersjava.lang.Exceptionpublic static java.lang.Object newInstance(java.lang.String className,
java.lang.Object... params)
throws java.lang.Exception
className - a string with the entire class, eg. "java.lang.Integer"params - the constructor parametersjava.lang.Exceptionpublic static java.lang.Object getStaticField(java.lang.String classAndField)
throws java.lang.Exception
classAndField - a string with the entire class and field namejava.lang.Exceptionpublic static java.lang.Object getField(java.lang.Object instance,
java.lang.String fieldName)
throws java.lang.Exception
instance - the instance on which the call is donefieldName - the field namejava.lang.Exceptionpublic static boolean isClassPresent(java.lang.String fullyQualifiedClassName)
fullyQualifiedClassName - a string with the entire class name, eg.
"java.lang.System"public static java.lang.Class<?> getNonPrimitiveClass(java.lang.Class<?> clazz)
clazz - the class (for example: int)public static boolean parseBoolean(java.lang.String value,
boolean defaultValue,
boolean throwException)
value - string to parsedefaultValue - value to return if value is null or on parsing errorthrowException - throw exception on parsing error or return default value insteadjava.lang.IllegalArgumentException - on parsing error if throwException is truepublic static java.lang.String getProperty(java.lang.String key,
java.lang.String defaultValue)
key - the keydefaultValue - the default valuepublic static int getProperty(java.lang.String key,
int defaultValue)
key - the keydefaultValue - the default valuepublic static boolean getProperty(java.lang.String key,
boolean defaultValue)
key - the keydefaultValue - the default valuepublic static int scaleForAvailableMemory(int value)
value - the value to scale