Added parseByteArray(). Ah the slow path to demand driven orthogonality.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1047 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: StringUtil.java,v 1.53 2003/02/03 18:26:38 mdb Exp $
|
// $Id: StringUtil.java,v 1.54 2003/02/04 03:01:28 mdb Exp $
|
||||||
//
|
//
|
||||||
// samskivert library - useful routines for java programs
|
// samskivert library - useful routines for java programs
|
||||||
// Copyright (C) 2001 Michael Bayne
|
// Copyright (C) 2001 Michael Bayne
|
||||||
@@ -627,6 +627,34 @@ public class StringUtil
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses an array of signed byte-sized integers from their string
|
||||||
|
* representation. The array should be represented as a bare list of
|
||||||
|
* numbers separated by commas, for example:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 25, 17, 21, 99
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* Any inability to parse the short array will result in the function
|
||||||
|
* returning null.
|
||||||
|
*/
|
||||||
|
public static byte[] parseByteArray (String source)
|
||||||
|
{
|
||||||
|
StringTokenizer tok = new StringTokenizer(source, ",");
|
||||||
|
byte[] vals = new byte[tok.countTokens()];
|
||||||
|
for (int i = 0; tok.hasMoreTokens(); i++) {
|
||||||
|
try {
|
||||||
|
// trim the whitespace from the token
|
||||||
|
String token = tok.nextToken().trim();
|
||||||
|
vals[i] = Byte.parseByte(token);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vals;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an array of short integers from their string representation.
|
* Parses an array of short integers from their string representation.
|
||||||
* The array should be represented as a bare list of numbers separated
|
* The array should be represented as a bare list of numbers separated
|
||||||
|
|||||||
Reference in New Issue
Block a user