Class StringUtil


  • public class StringUtil
    extends Object
    Utility class containing static methods for appending to strings.
    • Method Detail

      • append

        public static boolean append​(StringBuffer buf,
                                     String name,
                                     Object o,
                                     boolean comma)
        Appends name=value to a StringBuffer, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        name - String
        o - object value
        comma - whether to precede with a comma
        Returns:
        o != null || comma
      • append

        public static boolean append​(StringBuffer buf,
                                     String name,
                                     Object[] o,
                                     boolean comma)
        Appends name=[values] to a StringBuffer, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        name - String
        o - object values
        comma - whether to precede with a comma
        Returns:
        o != null || comma
      • append

        public static boolean append​(StringBuffer buf,
                                     String name,
                                     int value,
                                     boolean comma)
        Appends name=value to a StringBuffer, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        name - String
        value - integer value
        comma - whether to precede with a comma
        Returns:
        true
      • append

        public static boolean append​(StringBuffer buf,
                                     String description,
                                     boolean flag,
                                     boolean comma)
        Appends description to a StringBuffer if a flag is set, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        description - String
        flag - whether to append description
        comma - whether to precede with a comma
        Returns:
        whether anything was appended
      • appendDecimal

        public static boolean appendDecimal​(StringBuffer buf,
                                            String name,
                                            byte[] o,
                                            boolean comma)
        Appends name=[decimalBytes] to a StringBuffer, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        name - String
        o - byte array
        comma - whether to precede with a comma
        Returns:
        o != null || comma
      • appendHex

        public static boolean appendHex​(StringBuffer buf,
                                        String name,
                                        byte[] o,
                                        boolean comma)
        Appends name=[hexBytes] to a StringBuffer, optionally preceded by a comma.
        Parameters:
        buf - StringBuffer
        name - String
        o - byte array
        comma - whether to precede with a comma
        Returns:
        o != null || comma
      • appendHex

        public static boolean appendHex​(StringBuilder buf,
                                        String name,
                                        byte[] o,
                                        boolean comma)
        Appends name=[hexBytes] to a StringBuilder, optionally preceded by a comma.
        Parameters:
        buf - StringBuilder
        name - String
        o - byte array
        comma - whether to precede with a comma
        Returns:
        o != null || comma
      • append

        public static StringBuffer append​(StringBuffer buf,
                                          byte[] o)
        Appends hexBytes to a StringBuffer.
        Parameters:
        buf - StringBuffer
        o - byte array
        Returns:
        buf
      • append

        public static StringBuilder append​(StringBuilder buf,
                                           byte[] o)
        Converts the specified byte array to 8-bit hexadecimal characters and appends them to the specified StringBuilder with commas between each value. Equivalent to append(buf, o, true).
        Parameters:
        buf - StringBuilder
        o - byte array
        Returns:
        buf
      • append

        public static StringBuilder append​(StringBuilder buf,
                                           byte[] o,
                                           boolean comma)
        Converts the specified byte array to 8-bit hexadecimal characters and appends them to the specified StringBuilder, optionally with commas between each value.
        Parameters:
        buf - StringBuilder
        o - byte array
        comma - flag indicating whether or not commas should be inserted between each appended byte value.
        Returns:
        buf
      • toHexString

        public static String toHexString​(byte[] o)
        Converts a byte array to a String containing the hexadecimal values of the bytes with commas between each value. Equivalent to toHexString(byte[], boolean) toHexString(o, true)}.
        Parameters:
        o - byte array
        Returns:
        String
      • toHexString

        public static String toHexString​(byte[] o,
                                         boolean comma)
        Converts a byte array to a String containing the hexadecimal values of the bytes, optionally with commas between each value.
        Parameters:
        o - byte array
        comma - flag indicating whether or not commas should be inserted between each appended byte value.
        Returns:
        String
      • appendPrintableASCIIString

        public static StringBuilder appendPrintableASCIIString​(StringBuilder buf,
                                                               byte[] bytes)
        Appends a string containing the printable ASCII character values of the specified byte array. Printable characters are deemed to be those with byte values between 32 and 127 inclusive. Any other byte value is rendered as a period '.' character.
        Parameters:
        buf - a string builder to append to.
        bytes - byte array.
        Returns:
        the input buf.
      • toPrintableASCIIString

        public static String toPrintableASCIIString​(byte[] bytes)
        Converts a byte array to a String containing the printable ASCII character values of the bytes. Printable characters are deemed to be those with byte values between 32 and 127 inclusive. Any other byte value is rendered as a period '.' character.
        Parameters:
        bytes - byte array.
        Returns:
        a string.
      • fromHexString

        public static byte[] fromHexString​(String s)
        Converts a string as returned by toHexString(byte[], boolean) back to a byte array. Commas, if present, are silently ignored and skipped over.
        Parameters:
        s - a string
        Returns:
        byte array.
      • getHexDigit

        public static char getHexDigit​(int v)
        Converts a byte to a char containing its hexadecimal value.
        Parameters:
        v - byte
        Returns:
        char
      • fromHexDigit

        public static int fromHexDigit​(char ch)
        Converts a hexadecimal character to its decimal equivalent.
        Parameters:
        ch - hex character
        Returns:
        decimal value, or -1 if the character is not a valid hexadecimal digit.
      • appendASN1OctetString

        public static StringBuilder appendASN1OctetString​(StringBuilder buf,
                                                          byte[] bs)
        Appends an octet string to a StringBuilder using standard ASN.1 mark-up. For example, new byte[] {7,15,31} is appended as '070F1F'H.
        Parameters:
        buf - StringBuilder to append to
        bs - byte array representing octet string
        Returns:
        the given StringBuilder
      • spaces

        public static final String spaces​(int length)
        Returns a String containing the given number of space characters.
        Throws:
        IllegalArgumentException - if length is less than zero