Package com.opencloud.util
Class StringUtil
- java.lang.Object
-
- com.opencloud.util.StringUtil
-
public class StringUtil extends Object
Utility class containing static methods for appending to strings.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringBuffer
append(StringBuffer buf, byte[] o)
Appends hexBytes to a StringBuffer.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.static boolean
append(StringBuffer buf, String name, int value, boolean comma)
Appends name=value to a StringBuffer, optionally preceded by a comma.static boolean
append(StringBuffer buf, String name, Object[] o, boolean comma)
Appends name=[values] to a StringBuffer, optionally preceded by a comma.static boolean
append(StringBuffer buf, String name, Object o, boolean comma)
Appends name=value to a StringBuffer, optionally preceded by a comma.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.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.static StringBuilder
appendASN1OctetString(StringBuilder buf, byte[] bs)
Appends an octet string to a StringBuilder using standard ASN.1 mark-up.static boolean
appendDecimal(StringBuffer buf, String name, byte[] o, boolean comma)
Appends name=[decimalBytes] to a StringBuffer, optionally preceded by a comma.static boolean
appendHex(StringBuffer buf, String name, byte[] o, boolean comma)
Appends name=[hexBytes] to a StringBuffer, optionally preceded by a comma.static boolean
appendHex(StringBuilder buf, String name, byte[] o, boolean comma)
Appends name=[hexBytes] to a StringBuilder, optionally preceded by a comma.static StringBuilder
appendPrintableASCIIString(StringBuilder buf, byte[] bytes)
Appends a string containing the printable ASCII character values of the specified byte array.static void
appendSpaces(StringBuilder buf, int length)
Appends the given number of space characters to the given StringBuilder.static int
fromHexDigit(char ch)
Converts a hexadecimal character to its decimal equivalent.static byte[]
fromHexString(String s)
Converts a string as returned bytoHexString(byte[], boolean)
back to a byte array.static char
getHexDigit(int v)
Converts a byte to a char containing its hexadecimal value.static String
spaces(int length)
Returns a String containing the given number of space characters.static String
toHexString(byte[] o)
Converts a byte array to a String containing the hexadecimal values of the bytes with commas between each value.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.static String
toPrintableASCIIString(byte[] bytes)
Converts a byte array to a String containing the printable ASCII character values of the bytes.
-
-
-
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
- StringBuffername
- Stringo
- object valuecomma
- 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
- StringBuffername
- Stringo
- object valuescomma
- 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
- StringBuffername
- Stringvalue
- integer valuecomma
- 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
- StringBufferdescription
- Stringflag
- whether to append descriptioncomma
- 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
- StringBuffername
- Stringo
- byte arraycomma
- 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
- StringBuffername
- Stringo
- byte arraycomma
- 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
- StringBuildername
- Stringo
- byte arraycomma
- 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
- StringBuffero
- 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 toappend(buf, o, true)
.- Parameters:
buf
- StringBuildero
- 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
- StringBuildero
- byte arraycomma
- 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 totoHexString(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 arraycomma
- 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 bytoHexString(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 tobs
- byte array representing octet string- Returns:
- the given StringBuilder
-
appendSpaces
public static final void appendSpaces(StringBuilder buf, int length)
Appends the given number of space characters to the given StringBuilder.- Throws:
NullPointerException
- if buf is nullIllegalArgumentException
- if length is less than zero
-
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
-
-