StringUtilpublic class StringUtil extends Object Title: String Utility Description: Collection of string handling utilities |
Fields Summary |
---|
private static final String | ENCODING |
Constructors Summary |
---|
private StringUtil()Constructor for the StringUtil object
|
Methods Summary |
---|
public static java.lang.String | format(java.lang.String message, java.lang.Object[] params)Apply printf() like formatting to a string.
Primarily used for logging.
int currentParamNumber = 0;
StringBuffer formattedMessage = new StringBuffer();
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == '%") {
if (currentParamNumber >= params.length) {
formattedMessage.append("?missing data?");
} else if (
(params[currentParamNumber] instanceof Number)
&& (i + 1 < message.length())) {
i
+= matchOptionalFormatting(
(Number) params[currentParamNumber++],
message.substring(i + 1),
formattedMessage);
} else {
formattedMessage.append(
params[currentParamNumber++].toString());
}
} else {
if ((message.charAt(i) == '\\")
&& (i + 1 < message.length())
&& (message.charAt(i + 1) == '%")) {
formattedMessage.append('%");
i++;
} else {
formattedMessage.append(message.charAt(i));
}
}
}
return formattedMessage.toString();
| public static java.lang.String | getFromCompressedUnicode(byte[] string, int offset, int len)Read 8 bit data (in ISO-8859-1 codepage) into a (unicode) Java
String and return.
(In Excel terms, read compressed 8 bit unicode as a string)
try {
return new String(string, offset, len, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /* unreachable */
}
| public static java.lang.String | getFromUnicodeBE(byte[] string, int offset, int len)Given a byte array of 16-bit unicode characters in big endian
format (most important byte first), return a Java String representation
of it.
{ 0x00, 0x16 } -0x16
if ((offset < 0) || (offset >= string.length)) {
throw new ArrayIndexOutOfBoundsException("Illegal offset");
}
if ((len < 0) || (((string.length - offset) / 2) < len)) {
throw new IllegalArgumentException("Illegal length");
}
try {
return new String(string, offset, len * 2, "UTF-16BE");
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
| public static java.lang.String | getFromUnicodeBE(byte[] string)Given a byte array of 16-bit unicode characters in big endian
format (most important byte first), return a Java String representation
of it.
{ 0x00, 0x16 } -0x16
if(string.length == 0) { return ""; }
return getFromUnicodeBE(string, 0, string.length / 2);
| public static java.lang.String | getFromUnicodeLE(byte[] string, int offset, int len)Given a byte array of 16-bit unicode characters in Little Endian
format (most important byte last), return a Java String representation
of it.
{ 0x16, 0x00 } -0x16
if ((offset < 0) || (offset >= string.length)) {
throw new ArrayIndexOutOfBoundsException("Illegal offset");
}
if ((len < 0) || (((string.length - offset) / 2) < len)) {
throw new IllegalArgumentException("Illegal length");
}
try {
return new String(string, offset, len * 2, "UTF-16LE");
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
| public static java.lang.String | getFromUnicodeLE(byte[] string)Given a byte array of 16-bit unicode characters in little endian
format (most important byte last), return a Java String representation
of it.
{ 0x16, 0x00 } -0x16
if(string.length == 0) { return ""; }
return getFromUnicodeLE(string, 0, string.length / 2);
| public static java.lang.String | getPreferredEncoding()
return ENCODING;
| public static boolean | hasMultibyte(java.lang.String value)check the parameter has multibyte character
if( value == null )return false;
for(int i = 0 ; i < value.length() ; i++ ){
char c = value.charAt(i);
if(c > 0xFF )return true;
}
return false;
| public static boolean | isUnicodeString(java.lang.String value)Checks to see if a given String needs to be represented as Unicode
try {
return !value.equals(new String(value.getBytes("ISO-8859-1"), "ISO-8859-1"));
} catch (UnsupportedEncodingException e) {
return true;
}
| private static int | matchOptionalFormatting(java.lang.Number number, java.lang.String formatting, java.lang.StringBuffer outputTo)
NumberFormat numberFormat = NumberFormat.getInstance();
if ((0 < formatting.length())
&& Character.isDigit(formatting.charAt(0))) {
numberFormat.setMinimumIntegerDigits(
Integer.parseInt(formatting.charAt(0) + ""));
if ((2 < formatting.length())
&& (formatting.charAt(1) == '.")
&& Character.isDigit(formatting.charAt(2))) {
numberFormat.setMaximumFractionDigits(
Integer.parseInt(formatting.charAt(2) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0));
return 3;
}
numberFormat.format(number, outputTo, new FieldPosition(0));
return 1;
} else if (
(0 < formatting.length()) && (formatting.charAt(0) == '.")) {
if ((1 < formatting.length())
&& Character.isDigit(formatting.charAt(1))) {
numberFormat.setMaximumFractionDigits(
Integer.parseInt(formatting.charAt(1) + ""));
numberFormat.format(number, outputTo, new FieldPosition(0));
return 2;
}
}
numberFormat.format(number, outputTo, new FieldPosition(0));
return 1;
| public static void | putCompressedUnicode(java.lang.String input, byte[] output, int offset)Takes a unicode (java) string, and returns it as 8 bit data (in ISO-8859-1
codepage).
(In Excel terms, write compressed 8 bit unicode)
try {
byte[] bytes = input.getBytes("ISO-8859-1");
System.arraycopy(bytes, 0, output, offset, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
| public static void | putUnicodeBE(java.lang.String input, byte[] output, int offset)Takes a unicode string, and returns it as big endian (most
important byte first) bytes in the supplied byte array.
(In Excel terms, write uncompressed unicode)
try {
byte[] bytes = input.getBytes("UTF-16BE");
System.arraycopy(bytes, 0, output, offset, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
| public static void | putUnicodeLE(java.lang.String input, byte[] output, int offset)Takes a unicode string, and returns it as little endian (most
important byte last) bytes in the supplied byte array.
(In Excel terms, write uncompressed unicode)
try {
byte[] bytes = input.getBytes("UTF-16LE");
System.arraycopy(bytes, 0, output, offset, bytes.length);
} catch (UnsupportedEncodingException e) {
throw new InternalError(); /*unreachable*/
}
|
|