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