1:
37:
38:
39: package ;
40:
41: import ;
42:
43:
46: public class Util
47: {
48:
49: private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
50:
51:
52: private static final String BASE64_CHARS =
53: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
54:
55: private static final char[] BASE64_CHARSET = BASE64_CHARS.toCharArray();
56:
57:
58: private Util()
59: {
60: super();
61: }
62:
63:
76: public static String toString(byte[] ba)
77: {
78: return toString(ba, 0, ba.length);
79: }
80:
81:
94: public static final String toString(byte[] ba, int offset, int length)
95: {
96: char[] buf = new char[length * 2];
97: for (int i = 0, j = 0, k; i < length;)
98: {
99: k = ba[offset + i++];
100: buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
101: buf[j++] = HEX_DIGITS[ k & 0x0F];
102: }
103: return new String(buf);
104: }
105:
106:
120: public static String toReversedString(byte[] ba)
121: {
122: return toReversedString(ba, 0, ba.length);
123: }
124:
125:
141: public static final String toReversedString(byte[] ba, int offset, int length)
142: {
143: char[] buf = new char[length * 2];
144: for (int i = offset + length - 1, j = 0, k; i >= offset;)
145: {
146: k = ba[offset + i--];
147: buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
148: buf[j++] = HEX_DIGITS[ k & 0x0F];
149: }
150: return new String(buf);
151: }
152:
153:
161: public static byte[] toBytesFromString(String s)
162: {
163: int limit = s.length();
164: byte[] result = new byte[((limit + 1) / 2)];
165: int i = 0, j = 0;
166: if ((limit % 2) == 1)
167: result[j++] = (byte) fromDigit(s.charAt(i++));
168: while (i < limit)
169: {
170: result[j ] = (byte) (fromDigit(s.charAt(i++)) << 4);
171: result[j++] |= (byte) fromDigit(s.charAt(i++));
172: }
173: return result;
174: }
175:
176:
184: public static byte[] toReversedBytesFromString(String s)
185: {
186: int limit = s.length();
187: byte[] result = new byte[((limit + 1) / 2)];
188: int i = 0;
189: if ((limit % 2) == 1)
190: result[i++] = (byte) fromDigit(s.charAt(--limit));
191: while (limit > 0)
192: {
193: result[i ] = (byte) fromDigit(s.charAt(--limit));
194: result[i++] |= (byte) (fromDigit(s.charAt(--limit)) << 4);
195: }
196: return result;
197: }
198:
199:
205: public static int fromDigit(char c)
206: {
207: if (c >= '0' && c <= '9')
208: return c - '0';
209: else if (c >= 'A' && c <= 'F')
210: return c - 'A' + 10;
211: else if (c >= 'a' && c <= 'f')
212: return c - 'a' + 10;
213: else
214: throw new IllegalArgumentException("Invalid hexadecimal digit: " + c);
215: }
216:
217:
224: public static String toString(int n)
225: {
226: char[] buf = new char[8];
227: for (int i = 7; i >= 0; i--)
228: {
229: buf[i] = HEX_DIGITS[n & 0x0F];
230: n >>>= 4;
231: }
232: return new String(buf);
233: }
234:
235:
239: public static String toString(int[] ia)
240: {
241: int length = ia.length;
242: char[] buf = new char[length * 8];
243: for (int i = 0, j = 0, k; i < length; i++)
244: {
245: k = ia[i];
246: buf[j++] = HEX_DIGITS[(k >>> 28) & 0x0F];
247: buf[j++] = HEX_DIGITS[(k >>> 24) & 0x0F];
248: buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];
249: buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];
250: buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];
251: buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
252: buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
253: buf[j++] = HEX_DIGITS[ k & 0x0F];
254: }
255: return new String(buf);
256: }
257:
258:
265: public static String toString(long n)
266: {
267: char[] b = new char[16];
268: for (int i = 15; i >= 0; i--)
269: {
270: b[i] = HEX_DIGITS[(int)(n & 0x0FL)];
271: n >>>= 4;
272: }
273: return new String(b);
274: }
275:
276:
284: public static String toUnicodeString(byte[] ba)
285: {
286: return toUnicodeString(ba, 0, ba.length);
287: }
288:
289:
297: public static final String toUnicodeString(byte[] ba, int offset, int length)
298: {
299: StringBuffer sb = new StringBuffer();
300: int i = 0;
301: int j = 0;
302: int k;
303: sb.append('\n').append("\"");
304: while (i < length)
305: {
306: sb.append("\\u");
307: k = ba[offset + i++];
308: sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
309: sb.append(HEX_DIGITS[ k & 0x0F]);
310: k = ba[offset + i++];
311: sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
312: sb.append(HEX_DIGITS[ k & 0x0F]);
313: if ((++j % 8) == 0)
314: sb.append("\"+").append('\n').append("\"");
315: }
316: sb.append("\"").append('\n');
317: return sb.toString();
318: }
319:
320:
329: public static String toUnicodeString(int[] ia)
330: {
331: StringBuffer sb = new StringBuffer();
332: int i = 0;
333: int j = 0;
334: int k;
335: sb.append('\n').append("\"");
336: while (i < ia.length)
337: {
338: k = ia[i++];
339: sb.append("\\u");
340: sb.append(HEX_DIGITS[(k >>> 28) & 0x0F]);
341: sb.append(HEX_DIGITS[(k >>> 24) & 0x0F]);
342: sb.append(HEX_DIGITS[(k >>> 20) & 0x0F]);
343: sb.append(HEX_DIGITS[(k >>> 16) & 0x0F]);
344: sb.append("\\u");
345: sb.append(HEX_DIGITS[(k >>> 12) & 0x0F]);
346: sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]);
347: sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
348: sb.append(HEX_DIGITS[ k & 0x0F]);
349: if ((++j % 4) == 0)
350: sb.append("\"+").append('\n').append("\"");
351: }
352: sb.append("\"").append('\n');
353: return sb.toString();
354: }
355:
356: public static byte[] toBytesFromUnicode(String s)
357: {
358: int limit = s.length() * 2;
359: byte[] result = new byte[limit];
360: char c;
361: for (int i = 0; i < limit; i++)
362: {
363: c = s.charAt(i >>> 1);
364: result[i] = (byte)(((i & 1) == 0) ? c >>> 8 : c);
365: }
366: return result;
367: }
368:
369:
384: public static String dumpString(byte[] data, int offset, int length, String m)
385: {
386: if (data == null)
387: return m + "null\n";
388: StringBuffer sb = new StringBuffer(length * 3);
389: if (length > 32)
390: sb.append(m).append("Hexadecimal dump of ")
391: .append(length).append(" bytes...\n");
392:
393: int end = offset + length;
394: String s;
395: int l = Integer.toString(length).length();
396: if (l < 4)
397: l = 4;
398: for (; offset < end; offset += 32)
399: {
400: if (length > 32)
401: {
402: s = " " + offset;
403: sb.append(m).append(s.substring(s.length() - l)).append(": ");
404: }
405: int i = 0;
406: for (; i < 32 && offset + i + 7 < end; i += 8)
407: sb.append(toString(data, offset + i, 8)).append(' ');
408: if (i < 32)
409: for (; i < 32 && offset + i < end; i++)
410: sb.append(byteToString(data[offset + i]));
411: sb.append('\n');
412: }
413: return sb.toString();
414: }
415:
416: public static String dumpString(byte[] data)
417: {
418: return (data == null) ? "null\n" : dumpString(data, 0, data.length, "");
419: }
420:
421: public static String dumpString(byte[] data, String m)
422: {
423: return (data == null) ? "null\n" : dumpString(data, 0, data.length, m);
424: }
425:
426: public static String dumpString(byte[] data, int offset, int length)
427: {
428: return dumpString(data, offset, length, "");
429: }
430:
431:
438: public static String byteToString(int n)
439: {
440: char[] buf = { HEX_DIGITS[(n >>> 4) & 0x0F], HEX_DIGITS[n & 0x0F] };
441: return new String(buf);
442: }
443:
444:
455: public static final String toBase64(byte[] buffer)
456: {
457: int len = buffer.length, pos = len % 3;
458: byte b0 = 0, b1 = 0, b2 = 0;
459: switch (pos)
460: {
461: case 1:
462: b2 = buffer[0];
463: break;
464: case 2:
465: b1 = buffer[0];
466: b2 = buffer[1];
467: break;
468: }
469: StringBuffer sb = new StringBuffer();
470: int c;
471: boolean notleading = false;
472: do
473: {
474: c = (b0 & 0xFC) >>> 2;
475: if (notleading || c != 0)
476: {
477: sb.append(BASE64_CHARSET[c]);
478: notleading = true;
479: }
480: c = ((b0 & 0x03) << 4) | ((b1 & 0xF0) >>> 4);
481: if (notleading || c != 0)
482: {
483: sb.append(BASE64_CHARSET[c]);
484: notleading = true;
485: }
486: c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >>> 6);
487: if (notleading || c != 0)
488: {
489: sb.append(BASE64_CHARSET[c]);
490: notleading = true;
491: }
492: c = b2 & 0x3F;
493: if (notleading || c != 0)
494: {
495: sb.append(BASE64_CHARSET[c]);
496: notleading = true;
497: }
498: if (pos >= len)
499: break;
500: else
501: {
502: try
503: {
504: b0 = buffer[pos++];
505: b1 = buffer[pos++];
506: b2 = buffer[pos++];
507: }
508: catch (ArrayIndexOutOfBoundsException x)
509: {
510: break;
511: }
512: }
513: }
514: while (true);
515:
516: if (notleading)
517: return sb.toString();
518: return "0";
519: }
520:
521:
533: public static final byte[] fromBase64(String str)
534: {
535: int len = str.length();
536: if (len == 0)
537: throw new NumberFormatException("Empty string");
538: byte[] a = new byte[len + 1];
539: int i, j;
540: for (i = 0; i < len; i++)
541: try
542: {
543: a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
544: }
545: catch (ArrayIndexOutOfBoundsException x)
546: {
547: throw new NumberFormatException("Illegal character at #" + i);
548: }
549: i = len - 1;
550: j = len;
551: try
552: {
553: while (true)
554: {
555: a[j] = a[i];
556: if (--i < 0)
557: break;
558: a[j] |= (a[i] & 0x03) << 6;
559: j--;
560: a[j] = (byte)((a[i] & 0x3C) >>> 2);
561: if (--i < 0)
562: break;
563: a[j] |= (a[i] & 0x0F) << 4;
564: j--;
565: a[j] = (byte)((a[i] & 0x30) >>> 4);
566: if (--i < 0)
567: break;
568: a[j] |= (a[i] << 2);
569: j--;
570: a[j] = 0;
571: if (--i < 0)
572: break;
573: }
574: }
575: catch (Exception ignored)
576: {
577: }
578: try
579: {
580: while (a[j] == 0)
581: j++;
582: }
583: catch (Exception x)
584: {
585: return new byte[1];
586: }
587: byte[] result = new byte[len - j + 1];
588: System.arraycopy(a, j, result, 0, len - j + 1);
589: return result;
590: }
591:
592:
593:
594:
603: public static final byte[] trim(BigInteger n)
604: {
605: byte[] in = n.toByteArray();
606: if (in.length == 0 || in[0] != 0)
607: return in;
608: int len = in.length;
609: int i = 1;
610: while (in[i] == 0 && i < len)
611: ++i;
612: byte[] result = new byte[len - i];
613: System.arraycopy(in, i, result, 0, len - i);
614: return result;
615: }
616:
617:
623: public static final String dump(BigInteger x)
624: {
625: return dumpString(trim(x));
626: }
627: }