public class Utils extends 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 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 int |
compareNotNull(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 Object |
deserialize(byte[] data)
De-serialize the byte array to an object.
|
static int |
getByteArrayHash(byte[] value)
Calculate the hash code of the given byte array.
|
static Object |
getField(Object instance,
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 Class<?> |
getNonPrimitiveClass(Class<?> clazz)
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[] |
getResource(String name)
Get a resource from the resource map.
|
static Object |
getStaticField(String classAndField)
Returns a static field.
|
static int |
hashCode(Object o)
Calculate the hash code of the given object.
|
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(String fullyQualifiedClassName)
Returns true if the class is present in the current class loader.
|
static Class<?> |
loadUserClass(String className)
Load a class, but check if it is allowed to load this class first.
|
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 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 byte[] |
serialize(Object obj)
Serialize the object to a byte array.
|
public static final byte[] EMPTY_BYTES
public static final int[] EMPTY_INT_ARRAY
public static long readLong(byte[] buff, int pos)
buff
- the byte arraypos
- the positionpublic 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 int compareNotNull(byte[] data1, byte[] data2)
data1
- the first byte array (must not be null)data2
- the second byte array (must not be null)public static byte[] copy(byte[] source, byte[] target)
source
- the source arraytarget
- the target arraypublic static byte[] cloneByteArray(byte[] b)
b
- the byte array (may not be null)public static byte[] serialize(Object obj)
obj
- the object to serializepublic static Object deserialize(byte[] data)
data
- the byte arrayDbException
- if serialization failspublic static int hashCode(Object o)
o
- the objectpublic static int getMemoryUsed()
public static int getMemoryFree()
public static long getMemoryMax()
public static byte[] newBytes(int len)
len
- the number of bytes requestedOutOfMemoryError
public static int[] newIntArray(int len)
len
- the number of bytes requestedpublic static long[] newLongArray(int len)
len
- the number of bytes requestedpublic static Class<?> loadUserClass(String className)
className
- the name of the classpublic static byte[] getResource(String name) throws IOException
name
- the name of the resourceIOException
public static Object callStaticMethod(String classAndMethod, Object... params) throws Exception
classAndMethod
- a string with the entire class and method name, eg.
"java.lang.System.gc"params
- the method parametersException
public static Object callMethod(Object instance, String methodName, Object... params) throws Exception
instance
- the instance on which the call is donemethodName
- a string with the method nameparams
- the method parametersException
public static Object newInstance(String className, Object... params) throws Exception
className
- a string with the entire class, eg. "java.lang.Integer"params
- the constructor parametersException
public static Object getStaticField(String classAndField) throws Exception
classAndField
- a string with the entire class and field nameException
public static Object getField(Object instance, String fieldName) throws Exception
instance
- the instance on which the call is donefieldName
- the field nameException
public static boolean isClassPresent(String fullyQualifiedClassName)
fullyQualifiedClassName
- a string with the entire class name, eg.
"java.lang.System"public static Class<?> getNonPrimitiveClass(Class<?> clazz)
clazz
- the class (for example: int)public static String getProperty(String key, String defaultValue)
key
- the keydefaultValue
- the default valuepublic static int getProperty(String key, int defaultValue)
key
- the keydefaultValue
- the default valuepublic static boolean getProperty(String key, boolean defaultValue)
key
- the keydefaultValue
- the default valueCopyright © 2012 JBoss by Red Hat. All Rights Reserved.