Class StaticUtils


  • public final class StaticUtils
    extends Object
    Common utility methods.
    • 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.
    • 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 - If date was null.
      • 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 - If date was 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 calling String.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 - The Throwable object 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:
        true if the provided value is an uppercase or lowercase ASCII alphabetic character, or false if 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:
        true if the provided character represents a numeric digit, or false if 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:
        true if the provided character represents a hexadecimal digit, or false if 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 - true if certain illegal characters should be allowed for compatibility reasons.
        Returns:
        true if the provided character represents a keychar, or false if 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 - If true, 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 - If true, 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 - The StringBuilder to 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 be null.
      • 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 ServiceLoader facility.

        Type Parameters:
        P - type of provider
        Parameters:
        providerClass - class of provider
        requestedProvider - name of provider to use, or null if no specific provider is requested.
        classLoader - class loader to use to load the provider, or null to 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()