Package com.forgerock.opendj.util
Class StaticUtils
- java.lang.Object
-
- com.forgerock.opendj.util.StaticUtils
-
public final class StaticUtils extends Object
Common utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEBUG_ENABLEDIndicates whether the SDK is being used in debug mode.static ReferenceCountedObject<ScheduledExecutorService>DEFAULT_SCHEDULERThe default scheduler which should be used when the application does not provide one.static byte[]EMPTY_BYTESA zero-length byte array.static StringEOLThe end-of-line character for this platform.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static charbyteToASCII(byte b)Retrieves the printable ASCII representation of the provided byte.static StringbyteToHex(byte b)Retrieves a string representation of the provided byte in hexadecimal.static StringbyteToLowerHex(byte b)Retrieves a string representation of the provided byte in hexadecimal.static byte[]copyOfBytes(byte[] bytes)Returns a copy of the provided byte array.static StringformatAsGeneralizedTime(long date)Returns a string containing provided date formatted using the generalized time syntax.static StringformatAsGeneralizedTime(Date date)Returns a string containing provided date formatted using the generalized time syntax.static byte[]getBytes(char[] chars)Construct a byte array containing the UTF-8 encoding of the provided character array.static byte[]getBytes(CharSequence s)Construct a byte array containing the UTF-8 encoding of the provided char sequence.static org.forgerock.i18n.LocalizableMessagegetExceptionMessage(Throwable t)Retrieves the best human-readable message for the provided exception.static <P extends Provider>
PgetProvider(Class<P> providerClass, String requestedProvider, ClassLoader classLoader)Find and returns a provider of one or more implementations.static StackTraceElement[]getStackTraceIfDebugEnabled()Returns the stack trace for the calling method, but only if SDK debugging is enabled.static booleanisAlpha(char c)Indicates whether the provided character is an ASCII alphabetic character.static booleanisDigit(char c)Indicates whether the provided character is a numeric digit.static booleanisFips()static booleanisHexDigit(char c)Indicates whether the provided character is a hexadecimal digit.static booleanisKeyChar(char c, boolean allowCompatChars)Indicates whether the provided character is a keychar.static booleanisPrintable(byte b)Returns whether the byte is a printable ASCII character.static voidlogIfDebugEnabled(String msg, StackTraceElement[] stackTrace)Logs the provided message and stack trace if SDK debugging is enabled to either stderr or the debug logger.static voidregisterBcProvider()static voidstackTraceToSingleLineString(StringBuilder buffer, Throwable throwable, boolean isFullStack)Appends a single-line string representation of the provided exception to the given buffer.static StringstackTraceToSingleLineString(Throwable throwable, boolean isFullStack)Retrieves a stack trace from the provided exception as a single-line string.static StringtoLowerCase(String s)Retrieves a lower-case representation of the given string.static StringBuildertoLowerCase(String s, StringBuilder builder)Appends a lower-case representation of the given string to the provided buffer.static StringBuildertoLowerCase(ByteSequence b, StringBuilder builder)Appends a lowercase string representation of the contents of the given byte array to the provided buffer.
-
-
-
Field Detail
-
DEBUG_ENABLED
public static final boolean DEBUG_ENABLED
Indicates whether the SDK is being used in debug mode. In debug mode components may enable certain instrumentation in order to help debug applications.
-
EOL
public static final String EOL
The end-of-line character for this platform.
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTES
A zero-length byte array.
-
DEFAULT_SCHEDULER
public static final ReferenceCountedObject<ScheduledExecutorService> DEFAULT_SCHEDULER
The default scheduler which should be used when the application does not provide one.
-
-
Method Detail
-
byteToHex
public static String byteToHex(byte b)
Retrieves a string representation of the provided byte in hexadecimal.- Parameters:
b- The byte for which to retrieve the hexadecimal string representation.- Returns:
- The string representation of the provided byte in hexadecimal.
-
byteToLowerHex
public static String byteToLowerHex(byte b)
Retrieves a string representation of the provided byte in hexadecimal.- Parameters:
b- The byte for which to retrieve the hexadecimal string representation.- Returns:
- The string representation of the provided byte in hexadecimal using lowercase characters.
-
formatAsGeneralizedTime
public static String formatAsGeneralizedTime(Date date)
Returns a string containing provided date formatted using the generalized time syntax.- Parameters:
date- The date to be formated.- Returns:
- The string containing provided date formatted using the generalized time syntax.
- Throws:
NullPointerException- Ifdatewasnull.
-
formatAsGeneralizedTime
public static String formatAsGeneralizedTime(long date)
Returns a string containing provided date formatted using the generalized time syntax.- Parameters:
date- The date to be formated.- Returns:
- The string containing provided date formatted using the generalized time syntax.
- Throws:
IllegalArgumentException- Ifdatewas invalid.
-
getBytes
public static byte[] getBytes(char[] chars)
Construct a byte array containing the UTF-8 encoding of the provided character array.- Parameters:
chars- The character array to convert to a UTF-8 byte array.- Returns:
- A byte array containing the UTF-8 encoding of the provided character array.
-
getBytes
public static byte[] getBytes(CharSequence s)
Construct a byte array containing the UTF-8 encoding of the provided char sequence. This is significantly faster than callingString.getBytes(String)for ASCII strings.- Parameters:
s- The char sequence to convert to a UTF-8 byte array.- Returns:
- Returns a byte array containing the UTF-8 encoding of the provided string.
-
getExceptionMessage
public static org.forgerock.i18n.LocalizableMessage getExceptionMessage(Throwable t)
Retrieves the best human-readable message for the provided exception. For exceptions defined in the OpenDJ project, it will attempt to use the message (combining it with the message ID if available). For some exceptions that use encapsulation (e.g., InvocationTargetException), it will be unwrapped and the cause will be treated. For all others, the- Parameters:
t- TheThrowableobject for which to retrieve the message.- Returns:
- The human-readable message generated for the provided exception.
-
isAlpha
public static boolean isAlpha(char c)
Indicates whether the provided character is an ASCII alphabetic character.- Parameters:
c- The character for which to make the determination.- Returns:
trueif the provided value is an uppercase or lowercase ASCII alphabetic character, orfalseif it is not.
-
isDigit
public static boolean isDigit(char c)
Indicates whether the provided character is a numeric digit.- Parameters:
c- The character for which to make the determination.- Returns:
trueif the provided character represents a numeric digit, orfalseif not.
-
isHexDigit
public static boolean isHexDigit(char c)
Indicates whether the provided character is a hexadecimal digit.- Parameters:
c- The character for which to make the determination.- Returns:
trueif the provided character represents a hexadecimal digit, orfalseif not.
-
isKeyChar
public static boolean isKeyChar(char c, boolean allowCompatChars)Indicates whether the provided character is a keychar.- Parameters:
c- The character for which to make the determination.allowCompatChars-trueif certain illegal characters should be allowed for compatibility reasons.- Returns:
trueif the provided character represents a keychar, orfalseif not.
-
stackTraceToSingleLineString
public static String stackTraceToSingleLineString(Throwable throwable, boolean isFullStack)
Retrieves a stack trace from the provided exception as a single-line string.- Parameters:
throwable- The exception for which to retrieve the stack trace.isFullStack- Iftrue, provides the full stack trace, otherwise provides a limited extract of stack trace- Returns:
- A stack trace from the provided exception as a single-line string.
-
stackTraceToSingleLineString
public static void stackTraceToSingleLineString(StringBuilder buffer, Throwable throwable, boolean isFullStack)
Appends a single-line string representation of the provided exception to the given buffer.- Parameters:
buffer- The buffer to which the information is to be appended.throwable- The exception for which to retrieve the stack trace.isFullStack- Iftrue, provides the full stack trace, otherwise provides a limited extract of stack trace
-
toLowerCase
public static StringBuilder toLowerCase(ByteSequence b, StringBuilder builder)
Appends a lowercase string representation of the contents of the given byte array to the provided buffer. This implementation presumes that the provided string will contain only ASCII characters and is optimized for that case. However, if a non-ASCII character is encountered it will fall back on a more expensive algorithm that will work properly for non-ASCII characters.- Parameters:
b- The byte array for which to obtain the lowercase string representation.builder- The buffer to which the lowercase form of the string should be appended.- Returns:
- The updated
StringBuilder.
-
toLowerCase
public static String toLowerCase(String s)
Retrieves a lower-case representation of the given string. This implementation presumes that the provided string will contain only ASCII characters and is optimized for that case. However, if a non-ASCII character is encountered it will fall back on a more expensive algorithm that will work properly for non-ASCII characters.- Parameters:
s- The string for which to obtain the lower-case representation.- Returns:
- The lower-case representation of the given string.
-
toLowerCase
public static StringBuilder toLowerCase(String s, StringBuilder builder)
Appends a lower-case representation of the given string to the provided buffer. This implementation presumes that the provided string will contain only ASCII characters and is optimized for that case. However, if a non-ASCII character is encountered it will fall back on a more expensive algorithm that will work properly for non-ASCII characters.- Parameters:
s- The string for which to obtain the lower-case representation.builder- TheStringBuilderto which the lower-case form of the string should be appended.- Returns:
- The updated
StringBuilder.
-
copyOfBytes
public static byte[] copyOfBytes(byte[] bytes)
Returns a copy of the provided byte array.- Parameters:
bytes- The byte array to be copied.- Returns:
- A copy of the provided byte array.
-
getStackTraceIfDebugEnabled
public static StackTraceElement[] getStackTraceIfDebugEnabled()
Returns the stack trace for the calling method, but only if SDK debugging is enabled.- Returns:
- The stack trace for the calling method, but only if SDK debugging
is enabled, otherwise
null..
-
logIfDebugEnabled
public static void logIfDebugEnabled(String msg, StackTraceElement[] stackTrace)
Logs the provided message and stack trace if SDK debugging is enabled to either stderr or the debug logger.- Parameters:
msg- The message to be logged.stackTrace- The stack trace, which may benull.
-
byteToASCII
public static char byteToASCII(byte b)
Retrieves the printable ASCII representation of the provided byte.- Parameters:
b- The byte for which to retrieve the printable ASCII representation.- Returns:
- The printable ASCII representation of the provided byte, or a space if the provided byte does not have printable ASCII representation.
-
isPrintable
public static boolean isPrintable(byte b)
Returns whether the byte is a printable ASCII character.- Parameters:
b- The byte for which to determine whether it is printable ASCII- Returns:
- true if the byte is a printable ASCII character
-
getProvider
public static <P extends Provider> P getProvider(Class<P> providerClass, String requestedProvider, ClassLoader classLoader)
Find and returns a provider of one or more implementations.The provider is loaded using the
ServiceLoaderfacility.- Type Parameters:
P- type of provider- Parameters:
providerClass- class of providerrequestedProvider- name of provider to use, ornullif no specific provider is requested.classLoader- class loader to use to load the provider, ornullto use the default class loader (the context class loader of the current thread).- Returns:
- a provider
- Throws:
ProviderNotFoundException- if no provider is available or if the provider requested using options is not found.
-
isFips
public static boolean isFips()
-
registerBcProvider
public static void registerBcProvider()
-
-