public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static Object[] |
EMPTY_OBJECT_ARRAY |
static String[] |
EMPTY_STRING_ARRAY |
Modifier and Type | Method and Description |
---|---|
static void |
addHexByte(StringBuilder buf,
byte b) |
static <T> T[] |
arrayConcat(T[] first,
T[]... rest)
Concatenates an arbitrary number of arrays returning a new array containing all elements
|
static <T> Set<T> |
asSet(T... a) |
static <T> T |
cloneWithMarshaller(Marshaller marshaller,
T x)
Clones parameter x of type T with a given Marshaller reference;
|
static void |
close(AutoCloseable... cls) |
static void |
close(AutoCloseable cl) |
static void |
close(Context ctx) |
static void |
close(Socket s) |
static Runnable |
composeWithExceptions(Runnable a,
Runnable b)
Given two Runnables, return a Runnable that executes both in sequence,
even if the first throws an exception, and if both throw exceptions, add
any exceptions thrown by the second as suppressed exceptions of the first.
|
static Double |
constructDouble(Class<?> type,
Object o) |
static long |
currentMillisFromNanotime()
System.nanoTime() is less expensive than System.currentTimeMillis() and better suited
to measure time intervals. |
static int |
findNextHighestPowerOfTwo(int num)
Returns a number such that the number is a power of two that is equal to, or greater than, the number passed in as
an argument.
|
static void |
flushAndCloseOutput(ObjectOutput o) |
static void |
flushAndCloseStream(OutputStream o) |
static String |
formatString(Object message,
Object... params) |
static ClassLoader[] |
getClassLoaders(ClassLoader appClassLoader) |
static <T> T |
getInstance(Class<T> clazz)
Instantiates a class by first attempting a static factory method named getInstance() on the class
and then falling back to an empty constructor.
|
static <T> T |
getInstance(String classname,
ClassLoader cl)
Instantiates a class based on the class name provided.
|
static <T> T |
getInstanceStrict(Class<T> clazz)
Similar to
getInstance(Class) except that exceptions are propagated to the caller. |
static <T> T |
getInstanceStrict(String classname,
ClassLoader cl)
Similar to
getInstance(String, ClassLoader) except that exceptions are propagated to the caller. |
static <T> Supplier<T> |
getInstanceSupplier(Class<T> klass) |
static <T> Supplier<T> |
getInstanceSupplier(String className,
ClassLoader classLoader) |
static int |
getNormalizedHash(Object object,
Hash hashFct)
Applies the given hash function to the hash code of a given object, and then normalizes it to ensure a positive
value is always returned.
|
static InputStream |
getResourceAsStream(String resourcePath,
ClassLoader userClassLoader) |
static String |
getResourceAsString(String resourcePath,
ClassLoader userClassLoader) |
static int |
getSegmentSize(int numSegments)
Returns the size of each segment, given a number of segments.
|
static int |
hashCode(byte[] bytes,
int size)
A function that calculates hash code of a byte array based on its
contents but using the given size parameter as deliminator for the
content.
|
static String |
hexDump(byte[] buffer) |
static String |
hexDump(ByteBuffer buffer) |
static String |
hexIdHashCode(Object o)
Prints the identity hash code of the object passed as parameter
in an hexadecimal format in order to safe space.
|
static boolean |
isBasicType(Class<?> type) |
static boolean |
isIBMJavaVendor() |
static boolean |
isOSGiContext()
Tries to determine if the code is running in an OSGi context.
|
static String |
join(List<String> strings,
String separator) |
static <T> Class<T> |
loadClass(String classname,
ClassLoader cl)
Loads the specified class using the passed classloader, or, if it is
null the Infinispan classes'
classloader. |
static <T> Class<T> |
loadClassStrict(String classname,
ClassLoader userClassLoader)
Loads the specified class using the passed classloader, or, if it is
null the Infinispan classes' classloader. |
static Object[] |
objectArray(int length) |
static String |
padString(String s,
int minWidth) |
static String |
prettyPrintSubject(Subject subject)
Prints
Subject 's principals as a one-liner
(as opposed to default Subject's toString() method, which prints every principal on separate line). |
static String |
prettyPrintTime(long millis)
Prints a time for display
|
static String |
prettyPrintTime(long time,
TimeUnit unit) |
static String |
printArray(byte[] array) |
static String |
printArray(byte[] array,
boolean withHash) |
static String |
read(InputStream is)
Reads the given InputStream fully, closes the stream and returns the result as a String.
|
static byte[] |
readStream(InputStream is)
Reads the given InputStream fully, closes the stream and returns the result as a byte array.
|
static void |
recursiveFileRemove(File directory)
Deletes directory recursively.
|
static void |
recursiveFileRemove(String directoryName)
Deletes directory recursively.
|
static void |
renameTempFile(File tempFile,
File lockFile,
File dstFile,
BiConsumer<File,File> renameFailed) |
static CacheException |
rewrapAsCacheException(Throwable t) |
static boolean |
safeEquals(Object a,
Object b)
Null-safe equality test.
|
static String[] |
stringArray(int length) |
static String |
threadDump() |
static UUID |
threadLocalRandomUUID()
Uses a
ThreadLocalRandom to generate a UUID. |
static char[] |
toCharArray(String s) |
static String |
toHexString(byte[] input) |
static String |
toHexString(byte[] input,
int limit) |
static String |
toHexString(byte[] input,
int offset,
int limit) |
static <E> String |
toStr(Collection<E> collection) |
static String |
toStr(Object o) |
static String |
unicodeEscapeString(String s) |
static String |
unicodeUnescapeString(String s) |
static String |
xmlify(String s) |
public static final Object[] EMPTY_OBJECT_ARRAY
public static final String[] EMPTY_STRING_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY
public static <T> Class<T> loadClass(String classname, ClassLoader cl)
Loads the specified class using the passed classloader, or, if it is null
the Infinispan classes'
classloader.
If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this class may be loaded by the bootstrap classloader.
If the class is not found, the ClassNotFoundException
or NoClassDefFoundError
is wrapped as a
CacheConfigurationException
and is re-thrown.
classname
- name of the class to loadcl
- the application classloader which should be used to load the class, or null if the class is always packaged with
InfinispanCacheConfigurationException
- if the class cannot be loadedpublic static boolean isOSGiContext()
public static ClassLoader[] getClassLoaders(ClassLoader appClassLoader)
public static <T> Class<T> loadClassStrict(String classname, ClassLoader userClassLoader) throws ClassNotFoundException
Loads the specified class using the passed classloader, or, if it is null
the Infinispan classes' classloader.
If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this class may be loaded by the bootstrap classloader.
classname
- name of the class to loaduserClassLoader
- the application classloader which should be used to load the class, or null if the class is always packaged with
InfinispanClassNotFoundException
- if the class cannot be loadedpublic static InputStream getResourceAsStream(String resourcePath, ClassLoader userClassLoader)
public static String getResourceAsString(String resourcePath, ClassLoader userClassLoader) throws IOException
IOException
public static <T> T getInstance(Class<T> clazz)
CacheConfigurationException
and rethrown.clazz
- class to instantiatepublic static <T> T getInstanceStrict(Class<T> clazz) throws IllegalAccessException, InstantiationException
getInstance(Class)
except that exceptions are propagated to the caller.clazz
- class to instantiateIllegalAccessException
InstantiationException
public static <T> T getInstance(String classname, ClassLoader cl)
CacheConfigurationException
.classname
- class to instantiatepublic static <T> T getInstanceStrict(String classname, ClassLoader cl) throws ClassNotFoundException, InstantiationException, IllegalAccessException
getInstance(String, ClassLoader)
except that exceptions are propagated to the caller.classname
- class to instantiateClassNotFoundException
InstantiationException
IllegalAccessException
public static <T> T cloneWithMarshaller(Marshaller marshaller, T x)
public static Runnable composeWithExceptions(Runnable a, Runnable b)
public static boolean safeEquals(Object a, Object b)
a
- first object to compareb
- second object to comparepublic static long currentMillisFromNanotime()
System.nanoTime()
is less expensive than System.currentTimeMillis()
and better suited
to measure time intervals. It's NOT suited to know the current time, for example to be compared
with the time of other nodes.System.nanoTime()
, but converted in Milliseconds.public static String prettyPrintTime(long millis)
millis
- time in millispublic static byte[] readStream(InputStream is) throws IOException
is
- the stream to readIOException
- in case of stream read errorspublic static String read(InputStream is) throws IOException
is
- the stream to readIOException
- in case of stream read errorspublic static void close(AutoCloseable cl)
public static void close(Socket s)
public static void close(AutoCloseable... cls)
public static void close(Context ctx)
public static void flushAndCloseStream(OutputStream o)
public static void flushAndCloseOutput(ObjectOutput o)
public static <E> String toStr(Collection<E> collection)
public static String printArray(byte[] array)
public static String printArray(byte[] array, boolean withHash)
public static String toHexString(byte[] input)
public static String toHexString(byte[] input, int limit)
public static String toHexString(byte[] input, int offset, int limit)
public static String threadDump()
public static CacheException rewrapAsCacheException(Throwable t)
@SafeVarargs public static <T> Set<T> asSet(T... a)
public static String hexIdHashCode(Object o)
public static String hexDump(byte[] buffer)
public static String hexDump(ByteBuffer buffer)
public static void addHexByte(StringBuilder buf, byte b)
public static int getNormalizedHash(Object object, Hash hashFct)
object
- to hashhashFct
- hash function to applypublic static int getSegmentSize(int numSegments)
numSegments
- number of segments requiredpublic static boolean isIBMJavaVendor()
public static int findNextHighestPowerOfTwo(int num)
public static int hashCode(byte[] bytes, int size)
public static String prettyPrintSubject(Subject subject)
Subject
's principals as a one-liner
(as opposed to default Subject's toString()
method, which prints every principal on separate line).@SafeVarargs public static <T> T[] arrayConcat(T[] first, T[]... rest)
public static UUID threadLocalRandomUUID()
ThreadLocalRandom
to generate a UUID. Faster, but not securepublic static <T> Supplier<T> getInstanceSupplier(String className, ClassLoader classLoader)
public static void recursiveFileRemove(String directoryName)
directoryName
- Directory to be deletedpublic static void recursiveFileRemove(File directory)
directory
- Directory to be deletedpublic static boolean isBasicType(Class<?> type)
public static char[] toCharArray(String s)
public static Object[] objectArray(int length)
public static String[] stringArray(int length)
public static void renameTempFile(File tempFile, File lockFile, File dstFile, BiConsumer<File,File> renameFailed) throws IOException
IOException
Copyright © 2020 JBoss, a division of Red Hat. All rights reserved.