/* $Id$ */ package org.noreply.fancydress.misc; import java.io.IOException; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.TimeZone; /** * This class implements some functionality needed all over the place. */ public class Util { /** * return the octets of this String (US-ASCII encoding) * * @param m String to convert to the octet array * @return octet array */ public static byte[] toOctets (String m) { byte[] result; try { result = m.getBytes("US-ASCII"); } catch (IOException e) { throw new Error(e); } return(result); } /** * return a base16 or hex representation of an octet array * * @param buf octet array * @return the octet array in hex */ public static String asHex(byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 3); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); strbuf.append(" "); } return strbuf.toString(); } /** * Read an octet array from a hex encoding. * * Read an octet array from a hex encoding, ignoring all whitespace. * * @param s hex encoded octet array * @param octet array */ public static byte[] fromHex(String s) { boolean gotone = false; String r = ""; ArrayList a = new ArrayList(); for (int i=0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isWhitespace(c)) continue; r = r + c; if (gotone) { a.add(Integer.valueOf(r, 16)); r = ""; } gotone = !gotone; } byte[] result = new byte[a.size()]; for (int i=0; ib ? a : b; } /** * Check if two octet arrays are equal * * @param a * @param b * @return true if a is equal to b */ public static boolean equal(byte[] a, byte[] b) { boolean result = true; if (a.length != b.length) return(false); for (int i=0; ia starting at s of len octets in length */ public static byte[] slice(byte[] a, int s, int len) { byte[] result = new byte[len]; System.arraycopy(a, s, result, 0, len); return(result); } /* FIXME: switch to singleton so we need not generate the formats every time */ /** * Parse an ISO date yyyy-MM-dd. * * @param s a string holding a date * @return Date the date represented in s * @throws ParseException if s is not a valid date of that format */ public static Date parseDate(String s) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setLenient(false); try { return format.parse(s); } catch (ParseException e) { /* FIXME: US date.. */ format = new SimpleDateFormat("yyyy/MM/dd"); format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setLenient(false); try { return format.parse(s); } catch (ParseException e2) { return null; } } } /** * Parse an ISO date yyyy-MM-dd HH:mm:ss. * * @param s a string holding a date * @return Date the date represented in s * @throws ParseException if s is not a valid date of that format */ public static Date parseDateTime(String s) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setLenient(false); try { return format.parse(s); } catch (ParseException e) { /* FIXME: US date.. */ format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setLenient(false); try { return format.parse(s); } catch (ParseException e2) { return null; } } } /** * Parse an ISO date yyyy-MM-dd HH:mm:ss.SSSS. * * @param s a string holding a date * @return Date the date represented in s * @throws ParseException if s is not a valid date of that format */ public static Date parseDateTimeMS(String s) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS"); format.setTimeZone(TimeZone.getTimeZone("GMT")); format.setLenient(false); try { return format.parse(s); } catch (ParseException e) { return null; } } /** * Parse a boolean (yes/no) value. * * @param s a string, either "yes" or "no" * @return true if s is "yes", false if it is "no" * @throws ParseException if s is neither "yes" or "no" */ public static boolean parseBoolean(String s) throws ParseException { if (s.equals("yes")) return true; else if (s.equals("no")) return false; else { throw new ParseException("Cannot parse boolean expression "+s,0); } } /** * Tokenize comma separated lists into single tokens. * * Single tokens are trimmed of whitespace. * * @param s string of comma separated items. * @param separator a token separator * @return an array of single tokens. */ public static String[] tokenize(String s, char separator) { ArrayList list = new ArrayList(); int indexFrom = 0; int indexOf; do { indexOf = s.indexOf(separator, indexFrom); String v = (indexOf >= 0) ? s.substring(indexFrom, indexOf).trim() : s.substring(indexFrom).trim(); list.add( v ); indexFrom = indexOf + 1; } while (indexOf >= 0); return (String[]) list.toArray(new String[list.size()]); } }