This class contains frequently-used static utility methods.
Method from org.apache.jorphan.util.JOrphanUtils Detail: |
public static String baToHexString(byte[] ba) {
StrBuilder sb = new StrBuilder(ba.length*2);
for (int i = 0; i < ba.length; i++) {
int j = ba[i] & 0xff;
if (j < 16) {
sb.append("0"); // $NON-NLS-1$ add zero padding
}
sb.append(Integer.toHexString(j));
}
return sb.toString();
}
Convert binary byte array to hex string. |
public static String booleanToSTRING(boolean value) {
return value ? "TRUE" : "FALSE";
}
Convert a boolean to its string representation Equivalent to
Boolean.valueOf(boolean).toString().toUpperCase() but valid also for JDK
1.3, which does not have valueOf(boolean) |
public static void closeQuietly(InputStream is) {
try {
if (is != null) {
is.close();
}
} catch (IOException ignored) {
}
}
close a stream with no error thrown |
public static void closeQuietly(OutputStream os) {
try {
if (os != null) {
os.close();
}
} catch (IOException ignored) {
}
}
close a stream with no error thrown |
public static void closeQuietly(Writer wr) {
try {
if (wr != null) {
wr.close();
}
} catch (IOException ignored) {
}
}
close a Writer with no error thrown |
public static void closeQuietly(Reader rd) {
try {
if (rd != null) {
rd.close();
}
} catch (IOException ignored) {
}
}
close a Reader with no error thrown |
public static void closeQuietly(Socket sock) {
try {
if (sock!= null) {
sock.close();
}
} catch (IOException ignored) {
}
}
close a Socket with no error thrown |
public static void closeQuietly(ServerSocket sock) {
try {
if (sock!= null) {
sock.close();
}
} catch (IOException ignored) {
}
}
close a Socket with no error thrown |
public static byte[] getByteArraySlice(byte[] array,
int begin,
int end) {
byte[] slice = new byte[(end - begin + 1)];
int count = 0;
for (int i = begin; i < = end; i++) {
slice[count] = array[i];
count++;
}
return slice;
}
Returns a slice of a byte array.
TODO - add bounds checking? |
public static boolean isXML(byte[] target) {
return startsWith(target, XML_PFX,0);
}
Detects if some content starts with the standard XML prefix. |
public static StringBuffer leftAlign(StringBuffer in,
int len) {
int sfx = len - in.length();
if (sfx < = 0) {
return in;
}
if (sfx > SPACES_LEN) {
sfx = SPACES_LEN;
}
in.append(SPACES.substring(0, sfx));
return in;
}
Left aligns some text in a StringBuffer N.B. modifies the input buffer |
public static String replaceAllChars(String source,
char search,
String replace) {
char[] chars = source.toCharArray();
StringBuffer sb = new StringBuffer(source.length()+20);
for(int i = 0; i < chars.length; i++){
char c = chars[i];
if (c == search){
sb.append(replace);
} else {
sb.append(c);
}
}
return sb.toString();
}
Version of String.replaceAll() for JDK1.3
See below for another version which replaces strings rather than chars |
public static String replaceFirst(String source,
String search,
String replace) {
int start = source.indexOf(search);
int len = search.length();
if (start == -1) {
return source;
}
if (start == 0) {
return replace + source.substring(len);
}
return source.substring(0, start) + replace + source.substring(start + len);
}
Simple-minded String.replace() for JDK1.3 Should probably be recoded... |
public static StringBuffer rightAlign(StringBuffer in,
int len) {
int pfx = len - in.length();
if (pfx < = 0) {
return in;
}
if (pfx > SPACES_LEN) {
pfx = SPACES_LEN;
}
in.insert(0, SPACES.substring(0, pfx));
return in;
}
Right aligns some text in a StringBuffer N.B. modifies the input buffer |
public static String[] split(String splittee,
String splitChar) {
return split(splittee,splitChar,true);
}
|
public static String[] split(String splittee,
String splitChar,
boolean truncate) {
if (splittee == null || splitChar == null) {
return new String[0];
}
final String EMPTY_ELEMENT = "";
int spot;
final int splitLength = splitChar.length();
final String adjacentSplit = splitChar + splitChar;
final int adjacentSplitLength = adjacentSplit.length();
if(truncate) {
while ((spot = splittee.indexOf(adjacentSplit)) != -1) {
splittee = splittee.substring(0, spot + splitLength)
+ splittee.substring(spot + adjacentSplitLength, splittee.length());
}
if(splittee.startsWith(splitChar)) {
splittee = splittee.substring(splitLength);
}
if(splittee.endsWith(splitChar)) { // Remove trailing splitter
splittee = splittee.substring(0,splittee.length()-splitLength);
}
}
Vector returns = new Vector();
final int length = splittee.length(); // This is the new length
int start = 0;
spot = 0;
while (start < length && (spot = splittee.indexOf(splitChar, start)) > -1) {
if (spot > 0) {
returns.addElement(splittee.substring(start, spot));
}
else
{
returns.addElement(EMPTY_ELEMENT);
}
start = spot + splitLength;
}
if (start < length) {
returns.add(splittee.substring(start));
} else if (spot == length - splitLength){// Found splitChar at end of line
returns.addElement(EMPTY_ELEMENT);
}
String[] values = new String[returns.size()];
returns.copyInto(values);
return values;
}
This is _almost_ equivalent to the String.split method in JDK 1.4. It is
here to enable us to support earlier JDKs.
Note that unlike JDK1.4 split(), it optionally ignores leading split Characters,
and the splitChar parameter is not a Regular expression
This piece of code used to be part of JMeterUtils, but was moved here
because some JOrphan classes use it too. |
public static String[] split(String splittee,
String delims,
String def) {
StringTokenizer tokens = new StringTokenizer(splittee,delims,def!=null);
boolean lastWasDelim=false;
List strList=new ArrayList();
while (tokens.hasMoreTokens()) {
String tok=tokens.nextToken();
if ( tok.length()==1 // we have a single character; could be a token
&& delims.indexOf(tok)!=-1) // it is a token
{
if (lastWasDelim) {// we saw a delimiter last time
strList.add(def);// so add the default
}
lastWasDelim=true;
} else {
lastWasDelim=false;
strList.add(tok);
}
}
if (lastWasDelim) {
strList.add(def);
}
return (String[])strList.toArray(new String[0]);
}
Takes a String and a tokenizer character string, and returns a new array of
strings of the string split by the tokenizer character(s).
Trailing delimiters are significant (unless the default = null) |
public static boolean startsWith(byte[] target,
byte[] search,
int offset) {
final int targetLength = target.length;
final int searchLength = search.length;
if (offset < 0 || searchLength > targetLength+offset){
return false;
}
for(int i=0;i < searchLength; i++){
if (target[i+offset] != search[i]){
return false;
}
}
return true;
}
Check if a byte array starts with the given byte array. |
public static String substitute(String input,
String pattern,
String sub) {
StringBuffer ret = new StringBuffer(input.length());
int start = 0;
int index = -1;
final int length = pattern.length();
while ((index = input.indexOf(pattern, start)) >= start) {
ret.append(input.substring(start, index));
ret.append(sub);
start = index + length;
}
ret.append(input.substring(start));
return ret.toString();
}
Replace all patterns in a String |
public static String trim(String input,
String delims) {
StringTokenizer tokens = new StringTokenizer(input,delims);
return tokens.hasMoreTokens() ? tokens.nextToken() : "";
}
Trim a string by the tokens provided. |