summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/misc/Util.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/noreply/fancydress/misc/Util.java')
-rw-r--r--src/org/noreply/fancydress/misc/Util.java227
1 files changed, 227 insertions, 0 deletions
diff --git a/src/org/noreply/fancydress/misc/Util.java b/src/org/noreply/fancydress/misc/Util.java
new file mode 100644
index 0000000..e25ffe7
--- /dev/null
+++ b/src/org/noreply/fancydress/misc/Util.java
@@ -0,0 +1,227 @@
+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; i<result.length; i++) {
+ Integer o = (Integer) a.get(i);
+ int val = o.intValue();
+ result[i] = (byte) val;
+ };
+ return(result);
+ }
+
+ /**
+ * Return the smaller one of a and b.
+ *
+ * @param a
+ * @param b
+ * @return a&lt;b ? a : b
+ */
+ public static int min(int a, int b) {
+ return a<b ? a : b;
+ }
+
+ /**
+ * Return the greater one of a and b.
+ *
+ * @param a
+ * @param b
+ * @return a&gt;b ? a : b
+ */
+ public static int max(int a, int b) {
+ return a>b ? 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; i<a.length; i++)
+ result = result && (a[i] == b[i]);
+ return(result);
+ };
+
+ /**
+ * Concatinate two octet arrays
+ *
+ * @param a a
+ * @param b b
+ * @return a + b
+ */
+ public static byte[] concat(byte[] a, byte[] b) {
+ byte[] result = new byte[ a.length + b.length ];
+ for (int i=0; i<a.length; i++)
+ result[i] = a[i];
+ for (int i=0; i<b.length; i++)
+ result[a.length + i] = b[i];
+ return(result);
+ }
+
+ /**
+ * Return a substring of an octet array
+ *
+ * @param a an octet array
+ * @param s starting position
+ * @param len length to copy
+ * @return the slice of <code>a</code> starting at <code>s</code> of <code>len</code> 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 <code>s</code>
+ * @throws ParseException if <code>s</code> 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 <code>s</code>
+ * @throws ParseException if <code>s</code> 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) {
+ 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 <code>s</code>
+ * @throws ParseException if <code>s</code> 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 <code>s</code> is "yes", false if it is "no"
+ * @throws ParseException if <code>s</code> 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);
+ }
+ }
+}