summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/misc/Util.java
blob: d9b46daebef5ba8c97e95f25274b6a9e7f5655ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* $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; 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) {
			/* 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 <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);
		}
	}

	/**
	 * 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()]);
	}
}