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 package com.intel.bluetooth;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.UnsupportedEncodingException;
30 import java.util.Enumeration;
31 import java.util.Vector;
32
33 import javax.bluetooth.ServiceRecord;
34 import javax.bluetooth.UUID;
35
36
37
38
39
40
41
42 public abstract class Utils {
43
44 private static final String blueCoveImplPackage = getPackage(MicroeditionConnector.class.getName());
45
46 private Utils() {
47
48 }
49
50 private static String getPackage(String className) {
51 int pStart = className.lastIndexOf('.');
52 if (pStart == -1) {
53 return "";
54 } else {
55 return className.substring(0, pStart);
56 }
57 }
58
59 public static byte[] UUIDToByteArray(String uuidStringValue) {
60 byte[] uuidValue = new byte[16];
61 if (uuidStringValue.indexOf('-') != -1) {
62 throw new NumberFormatException("The '-' character is not allowed in UUID: " + uuidStringValue);
63 }
64 for (int i = 0; i < 16; i++) {
65 uuidValue[i] = (byte) Integer.parseInt(uuidStringValue.substring(i * 2, i * 2 + 2), 16);
66 }
67 return uuidValue;
68 }
69
70 static byte[] UUIDToByteArray(final UUID uuid) {
71 return UUIDToByteArray(uuid.toString());
72 }
73
74 public static String UUIDByteArrayToString(byte[] uuidValue) {
75 StringBuffer buf = new StringBuffer();
76 for (int i = 0; i < uuidValue.length; i++) {
77 buf.append(Integer.toHexString(uuidValue[i] >> 4 & 0xf));
78 buf.append(Integer.toHexString(uuidValue[i] & 0xf));
79 }
80 return buf.toString();
81 }
82
83 static long UUIDTo32Bit(UUID uuid) {
84 if (uuid == null) {
85 return -1;
86 }
87 String str = uuid.toString().toUpperCase();
88 int shortIdx = str.indexOf(BluetoothConsts.SHORT_UUID_BASE);
89 if ((shortIdx != -1) && (shortIdx + BluetoothConsts.SHORT_UUID_BASE.length() == str.length())) {
90
91 return Long.parseLong(str.substring(0, shortIdx), 16);
92 }
93 return -1;
94 }
95
96 static boolean is32Bit(UUID uuid) {
97 return (UUIDTo32Bit(uuid) != -1);
98 }
99
100 public static int securityOpt(boolean authenticate, boolean encrypt) {
101 int security = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
102 if (authenticate) {
103 if (encrypt) {
104 security = ServiceRecord.AUTHENTICATE_ENCRYPT;
105 } else {
106 security = ServiceRecord.AUTHENTICATE_NOENCRYPT;
107 }
108 } else if (encrypt) {
109 throw new IllegalArgumentException("Illegal encrypt configuration");
110 }
111 return security;
112 }
113
114 static boolean isStringSet(String str) {
115 return ((str != null) && (str.length() > 0));
116 }
117
118 static String loadString(InputStream inputstream) {
119 if (inputstream == null) {
120 return null;
121 }
122 try {
123 byte[] buf = new byte[256];
124 int len = inputstream.read(buf);
125 return new String(buf, 0, len);
126 } catch (IOException e) {
127 return null;
128 } finally {
129 try {
130 inputstream.close();
131 } catch (IOException ignore) {
132 }
133 }
134 }
135
136 static String getResourceProperty(Class owner, String resourceName) {
137 try {
138 String value = loadString(owner.getResourceAsStream("/" + resourceName));
139 if (value != null) {
140 int cr = value.indexOf('\n');
141 if (cr != -1) {
142 value = value.substring(0, cr - 1);
143 }
144 }
145 return value;
146 } catch (Throwable e) {
147 return null;
148 }
149 }
150
151
152
153
154
155
156
157
158 public static byte[] clone(byte[] value) {
159 if (value == null) {
160 return null;
161 }
162 int length = ((byte[]) value).length;
163 byte[] bClone = new byte[length];
164 System.arraycopy(value, 0, bClone, 0, length);
165 return bClone;
166 }
167
168 public static Vector clone(Enumeration en) {
169 Vector copy = new Vector();
170 while (en.hasMoreElements()) {
171 copy.addElement(en.nextElement());
172 }
173 return copy;
174 }
175
176 static String newStringUTF8(byte bytes[]) {
177 try {
178 return new String(bytes, "UTF-8");
179 } catch (IllegalArgumentException e) {
180 return new String(bytes);
181 } catch (UnsupportedEncodingException e) {
182 return new String(bytes);
183 }
184 }
185
186 static byte[] getUTF8Bytes(String str) {
187 try {
188 return str.getBytes("UTF-8");
189 } catch (IllegalArgumentException e) {
190 return str.getBytes();
191 } catch (UnsupportedEncodingException e) {
192 return str.getBytes();
193 }
194 }
195
196 static String newStringASCII(byte bytes[]) {
197 try {
198 return new String(bytes, "US-ASCII");
199 } catch (IllegalArgumentException e) {
200 return new String(bytes);
201 } catch (UnsupportedEncodingException e) {
202 return new String(bytes);
203 }
204 }
205
206 static byte[] getASCIIBytes(String str) {
207 try {
208 return str.getBytes("US-ASCII");
209 } catch (IllegalArgumentException e) {
210 return str.getBytes();
211 } catch (UnsupportedEncodingException e) {
212 return str.getBytes();
213 }
214 }
215
216
217
218
219
220 static Object[] vector2toArray(Vector vector, Object[] anArray) {
221 vector.copyInto(anArray);
222 return anArray;
223 }
224
225
226
227
228
229 public static String toHexString(long l) {
230 StringBuffer buf = new StringBuffer();
231 String lo = Integer.toHexString((int) l);
232 if (l > 0xffffffffl) {
233 String hi = Integer.toHexString((int) (l >> 32));
234 buf.append(hi);
235 for (int i = lo.length(); i < 8; i++) {
236 buf.append('0');
237 }
238 }
239 buf.append(lo);
240 return buf.toString();
241 }
242
243 static void j2meUsagePatternDellay() {
244 try {
245 Thread.sleep(100);
246 } catch (InterruptedException e) {
247 }
248 }
249
250 static class TimerThread extends Thread {
251
252 long delay;
253
254 Runnable run;
255
256 public TimerThread(long delay, Runnable run) {
257 this.delay = delay;
258 this.run = run;
259 }
260
261 public void run() {
262 try {
263 Thread.sleep(delay);
264 run.run();
265 } catch (InterruptedException e) {
266 }
267 }
268
269 }
270
271
272
273
274
275
276
277
278
279
280 static TimerThread schedule(final long delay, final Runnable run) {
281 TimerThread t = new TimerThread(delay, run);
282 UtilsJavaSE.threadSetDaemon(t);
283 t.start();
284 return t;
285 }
286
287 public static void isLegalAPICall(Vector fqcnSet) throws Error {
288 UtilsJavaSE.StackTraceLocation ste = UtilsJavaSE.getLocation(fqcnSet);
289 if (ste != null) {
290 if (ste.className.startsWith("javax.bluetooth.")) {
291 return;
292 }
293 if (ste.className.startsWith(blueCoveImplPackage + ".")) {
294 return;
295 }
296 throw new Error("Illegal use of the JSR-82 API");
297 }
298 }
299 }