|
1 /* |
|
2 * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 |
|
25 public class Util { |
|
26 |
|
27 private Util() { } |
|
28 |
|
29 // Returns -1 if equal, o.w. returns index of first difference |
|
30 // |
|
31 public static int cmp(byte[] ba, byte[] bb) { |
|
32 int n = Math.min(ba.length, bb.length); |
|
33 for (int i = 0; i < n; i++) { |
|
34 if ((i >= ba.length) || (i >= bb.length)) |
|
35 return i; |
|
36 if (ba[i] != bb[i]) |
|
37 return i; |
|
38 } |
|
39 if (ba.length != bb.length) |
|
40 return 0; |
|
41 return -1; |
|
42 } |
|
43 |
|
44 // Returns -1 if equal, o.w. returns index of first difference |
|
45 // |
|
46 public static int cmp(char[] ca, char[] cb) { |
|
47 int n = Math.min(ca.length, cb.length); |
|
48 for (int i = 0; i < n; i++) { |
|
49 if ((i >= ca.length) || (i >= cb.length)) |
|
50 return i; |
|
51 if (ca[i] != cb[i]) |
|
52 return i; |
|
53 } |
|
54 if (ca.length != cb.length) |
|
55 return 0; |
|
56 return -1; |
|
57 } |
|
58 |
|
59 public static String toString(byte[] ba, int off, int len) { |
|
60 StringBuffer sb = new StringBuffer(); |
|
61 for (int i = off; i < off + len; i++) { |
|
62 int c = ba[i]; |
|
63 if (c == '\\') { |
|
64 sb.append("\\\\"); |
|
65 continue; |
|
66 } |
|
67 if ((c >= ' ') && (c < 0x7f)) { |
|
68 sb.append((char)c); |
|
69 continue; |
|
70 } |
|
71 sb.append("\\x"); |
|
72 sb.append(Integer.toHexString(c & 0xff)); |
|
73 } |
|
74 return sb.toString(); |
|
75 } |
|
76 |
|
77 public static String toString(byte[] ba) { |
|
78 return toString(ba, 0, ba.length); |
|
79 } |
|
80 |
|
81 public static String toString(char[] ca, int off, int len) { |
|
82 StringBuffer sb = new StringBuffer(); |
|
83 for (int i = off; i < off + len; i++) { |
|
84 char c = ca[i]; |
|
85 if (c == '\\') { |
|
86 sb.append("\\\\"); |
|
87 continue; |
|
88 } |
|
89 if ((c >= ' ') && (c < 0x7f)) { |
|
90 sb.append(c); |
|
91 continue; |
|
92 } |
|
93 sb.append("\\u"); |
|
94 String s = Integer.toHexString(c); |
|
95 while (s.length() < 4) |
|
96 s = "0" + s; |
|
97 sb.append(s); |
|
98 } |
|
99 return sb.toString(); |
|
100 } |
|
101 |
|
102 public static String toString(char[] ca) { |
|
103 return toString(ca, 0, ca.length); |
|
104 } |
|
105 |
|
106 public static String toString(String s) { |
|
107 return toString(s.toCharArray()); |
|
108 } |
|
109 |
|
110 public static String toString(char c) { |
|
111 return toString(new char[]{ c }); |
|
112 } |
|
113 |
|
114 } |