public class Utils extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
Utils.ClassFactory
The utility methods will try to use the provided class factories to
convert binary name of class to Class object.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static byte[] |
EMPTY_BYTES
An 0-size byte array.
|
static int[] |
EMPTY_INT_ARRAY
An 0-size int array.
|
| 限定符和类型 | 方法和说明 |
|---|---|
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 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 ThreadPoolExecutor |
createSingleThreadExecutor(String threadName) |
static ThreadPoolExecutor |
createSingleThreadExecutor(String threadName,
BlockingQueue<Runnable> workQueue) |
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 void |
flushExecutor(ThreadPoolExecutor executor)
Makes sure that all currently submitted tasks are processed before this method returns.
|
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 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 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 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 Object |
newInstance(String className,
Object... params)
Creates a new instance.
|
static <T> ArrayList<T> |
newSmallArrayList()
Create a new ArrayList with an initial capacity of 4.
|
static boolean |
parseBoolean(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 void |
shutdownExecutor(ThreadPoolExecutor executor) |
static <X> void |
sortTopN(X[] array,
int fromInclusive,
int toExclusive,
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 requestedOutOfMemoryError - 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 arrayOutOfMemoryError - 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 long getMemoryUsed()
public static long getMemoryFree()
public static long getMemoryMax()
public static long getGarbageCollectionTime()
public static long getGarbageCollectionCount()
public static void collectGarbage()
public static <T> ArrayList<T> newSmallArrayList()
T - the typepublic static <X> void sortTopN(X[] array,
int fromInclusive,
int toExclusive,
Comparator<? super X> comp)
X - the type of elementsarray - the array.fromInclusive - the start index, inclusivetoExclusive - the end index, exclusivecomp - the comparator.public static byte[] getResource(String name) throws IOException
name - the name of the resourceIOException - on failurepublic 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 - on failurepublic 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 - on failurepublic static Object newInstance(String className, Object... params) throws Exception
className - a string with the entire class, eg. "java.lang.Integer"params - the constructor parametersException - on failurepublic static Class<?> getNonPrimitiveClass(Class<?> clazz)
clazz - the class (for example: int)public static boolean parseBoolean(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 insteadIllegalArgumentException - on parsing error if throwException is truepublic 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 valuepublic static int scaleForAvailableMemory(int value)
value - the value to scalepublic static long currentNanoTime()
System.nanoTime()public static long currentNanoTimePlusMillis(int ms)
ms - additional offset in millisecondsSystem.nanoTime()public static long nanoTimePlusMillis(long nanoTime,
int ms)
nanoTime - time in nanosecondsms - additional offset in millisecondsSystem.nanoTime()public static ThreadPoolExecutor createSingleThreadExecutor(String threadName)
public static ThreadPoolExecutor createSingleThreadExecutor(String threadName, BlockingQueue<Runnable> workQueue)
public static void flushExecutor(ThreadPoolExecutor executor)
executor - to flushpublic static void shutdownExecutor(ThreadPoolExecutor executor)
Copyright © 2022. All rights reserved.