From 84abf220b635234a15cccf58f8e65972050329b8 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 4 Feb 2003 03:01:28 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/StringUtil.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index e912d7f0..8464e48a 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -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 // 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: + * + *
+     * 25, 17, 21, 99
+     * 
+ * + * 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. * The array should be represented as a bare list of numbers separated