Methods Summary |
---|
public static boolean | endsWith(java.lang.StringBuffer buffer, java.lang.String suffix)Checks that a string buffer ends up with a given string. It may sound
trivial with the existing
JDK API but the various implementation among JDKs can make those
methods extremely resource intensive
and perform poorly due to massive memory allocation and copying. See
if (suffix.length() > buffer.length()) {
return false;
}
// this loop is done on purpose to avoid memory allocation performance
// problems on various JDKs
// StringBuffer.lastIndexOf() was introduced in jdk 1.4 and
// implementation is ok though does allocation/copying
// StringBuffer.toString().endsWith() does massive memory
// allocation/copying on JDK 1.5
// See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169
int endIndex = suffix.length() - 1;
int bufferIndex = buffer.length() - 1;
while (endIndex >= 0) {
if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) {
return false;
}
bufferIndex--;
endIndex--;
}
return true;
|
public static java.lang.String | getStackTrace(java.lang.Throwable t)Convenient method to retrieve the full stacktrace from a given exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
pw.close();
return sw.toString();
|
public static java.util.Vector | lineSplit(java.lang.String data)Splits up a string into a list of lines. It is equivalent
to split(data, '\n').
return split(data, '\n");
|
public static long | parseHumanSizes(java.lang.String humanSize)Takes a human readable size representation eg 10K
a long value. Doesn't support 1.1K or other rational values.
final long KILOBYTE = 1024;
final long MEGABYTE = KILOBYTE * 1024;
final long GIGABYTE = MEGABYTE * 1024;
final long TERABYTE = GIGABYTE * 1024;
final long PETABYTE = TERABYTE * 1024;
//last character isn't a digit
if(!Character.isDigit(humanSize.charAt(humanSize.length()-1))) {
char c = humanSize.charAt(humanSize.length()-1);
long value = Long.valueOf(humanSize.substring(0, humanSize.length()-1)).longValue();
switch (c) {
case 'K":
return value * KILOBYTE;
case 'M":
return value * MEGABYTE;
case 'G":
return value * GIGABYTE;
case 'T":
return value * TERABYTE;
case 'P":
return value * PETABYTE;
default:
return value;
}
} else {
return Long.parseLong(humanSize);
}
|
public static java.lang.String | replace(java.lang.String data, java.lang.String from, java.lang.String to)Replace occurrences into a string.
StringBuffer buf = new StringBuffer(data.length());
int pos = -1;
int i = 0;
while ((pos = data.indexOf(from, i)) != -1) {
buf.append(data.substring(i, pos)).append(to);
i = pos + from.length();
}
buf.append(data.substring(i));
return buf.toString();
|
public static java.lang.String | resolveBackSlash(java.lang.String input)xml does not do "c" like interpretation of strings.
i.e. \n\r\t etc.
this method processes \n, \r, \t, \f, \\
also subs \s -> " \n\r\t\f"
a trailing '\' will be ignored
StringBuffer b = new StringBuffer();
boolean backSlashSeen = false;
for (int i = 0; i < input.length(); ++i) {
char c = input.charAt(i);
if (!backSlashSeen) {
if (c == '\\") {
backSlashSeen = true;
} else {
b.append(c);
}
} else {
switch (c) {
case '\\":
b.append((char) '\\");
break;
case 'n":
b.append((char) '\n");
break;
case 'r":
b.append((char) '\r");
break;
case 't":
b.append((char) '\t");
break;
case 'f":
b.append((char) '\f");
break;
case 's":
b.append(" \t\n\r\f");
break;
default:
b.append(c);
}
backSlashSeen = false;
}
}
return b.toString();
|
public static java.util.Vector | split(java.lang.String data, int ch)Splits up a string where elements are separated by a specific
character and return all elements.
Vector elems = new Vector();
int pos = -1;
int i = 0;
while ((pos = data.indexOf(ch, i)) != -1) {
String elem = data.substring(i, pos);
elems.addElement(elem);
i = pos + 1;
}
elems.addElement(data.substring(i));
return elems;
|