My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Vector;

import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

/**
* Conversion and JVM compatibility functions.
*
* <p>
* <b><u>Your application should not use this class directly.</u></b>
*/
public abstract class Utils {

private static final String blueCoveImplPackage = getPackage(MicroeditionConnector.class.getName());

private Utils() {

}

private static String getPackage(String className) {
int pStart = className.lastIndexOf('.');
if (pStart == -1) {
return "";
} else {
return className.substring(0, pStart);
}
}

public static byte[] UUIDToByteArray(String uuidStringValue) {
byte[] uuidValue = new byte[16];
if (uuidStringValue.indexOf('-') != -1) {
throw new NumberFormatException("The '-' character is not allowed in UUID: " + uuidStringValue);
}
for (int i = 0; i < 16; i++) {
uuidValue[i] = (byte) Integer.parseInt(uuidStringValue.substring(i * 2, i * 2 + 2), 16);
}
return uuidValue;
}

static byte[] UUIDToByteArray(final UUID uuid) {
return UUIDToByteArray(uuid.toString());
}

public static String UUIDByteArrayToString(byte[] uuidValue) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < uuidValue.length; i++) {
buf.append(Integer.toHexString(uuidValue[i] >> 4 & 0xf));
buf.append(Integer.toHexString(uuidValue[i] & 0xf));
}
return buf.toString();
}

static long UUIDTo32Bit(UUID uuid) {
if (uuid == null) {
return -1;
}
String str = uuid.toString().toUpperCase();
int shortIdx = str.indexOf(BluetoothConsts.SHORT_UUID_BASE);
if ((shortIdx != -1) && (shortIdx + BluetoothConsts.SHORT_UUID_BASE.length() == str.length())) {
// This is short 16-bit or 32-bit UUID
return Long.parseLong(str.substring(0, shortIdx), 16);
}
return -1;
}

static boolean is32Bit(UUID uuid) {
return (UUIDTo32Bit(uuid) != -1);
}

public static int securityOpt(boolean authenticate, boolean encrypt) {
int security = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
if (authenticate) {
if (encrypt) {
security = ServiceRecord.AUTHENTICATE_ENCRYPT;
} else {
security = ServiceRecord.AUTHENTICATE_NOENCRYPT;
}
} else if (encrypt) {
throw new IllegalArgumentException("Illegal encrypt configuration");
}
return security;
}

static boolean isStringSet(String str) {
return ((str != null) && (str.length() > 0));
}

static String loadString(InputStream inputstream) {
if (inputstream == null) {
return null;
}
try {
byte[] buf = new byte[256];
int len = inputstream.read(buf);
return new String(buf, 0, len);
} catch (IOException e) {
return null;
} finally {
try {
inputstream.close();
} catch (IOException ignore) {
}
}
}

static String getResourceProperty(Class owner, String resourceName) {
try {
String value = loadString(owner.getResourceAsStream("/" + resourceName));
if (value != null) {
int cr = value.indexOf('\n');
if (cr != -1) {
value = value.substring(0, cr - 1);
}
}
return value;
} catch (Throwable e) {
return null;
}
}

/**
* Modifying the returned Object will not change the internal representation
* of the object.
*
* @param value
* @return a clone of the array
*/
public static byte[] clone(byte[] value) {
if (value == null) {
return null;
}
int length = ((byte[]) value).length;
byte[] bClone = new byte[length];
System.arraycopy(value, 0, bClone, 0, length);
return bClone;
}

public static Vector clone(Enumeration en) {
Vector copy = new Vector();
while (en.hasMoreElements()) {
copy.addElement(en.nextElement());
}
return copy;
}

static String newStringUTF8(byte bytes[]) {
try {
return new String(bytes, "UTF-8");
} catch (IllegalArgumentException e) {
return new String(bytes);
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}

static byte[] getUTF8Bytes(String str) {
try {
return str.getBytes("UTF-8");
} catch (IllegalArgumentException e) {
return str.getBytes();
} catch (UnsupportedEncodingException e) {
return str.getBytes();
}
}

static String newStringASCII(byte bytes[]) {
try {
return new String(bytes, "US-ASCII");
} catch (IllegalArgumentException e) {
return new String(bytes);
} catch (UnsupportedEncodingException e) {
return new String(bytes);
}
}

static byte[] getASCIIBytes(String str) {
try {
return str.getBytes("US-ASCII");
} catch (IllegalArgumentException e) {
return str.getBytes();
} catch (UnsupportedEncodingException e) {
return str.getBytes();
}
}

/**
* J2ME/J9 compatibility instead of Vector.toArray
*
*/
static Object[] vector2toArray(Vector vector, Object[] anArray) {
vector.copyInto(anArray);
return anArray;
}

/**
* J2ME/J9 compatibility instead of Long.toHexString
*
*/
public static String toHexString(long l) {
StringBuffer buf = new StringBuffer();
String lo = Integer.toHexString((int) l);
if (l > 0xffffffffl) {
String hi = Integer.toHexString((int) (l >> 32));
buf.append(hi);
for (int i = lo.length(); i < 8; i++) {
buf.append('0');
}
}
buf.append(lo);
return buf.toString();
}

static void j2meUsagePatternDellay() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}

static class TimerThread extends Thread {

long delay;

Runnable run;

public TimerThread(long delay, Runnable run) {
this.delay = delay;
this.run = run;
}

public void run() {
try {
Thread.sleep(delay);
run.run();
} catch (InterruptedException e) {
}
}

}

/**
* Java 1.1 compatible. Schedules the specified task for execution after the
* specified delay.
*
* @param delay
* delay in milliseconds before task is to be executed.
* @param run
* task to be scheduled.
*/
static TimerThread schedule(final long delay, final Runnable run) {
TimerThread t = new TimerThread(delay, run);
UtilsJavaSE.threadSetDaemon(t);
t.start();
return t;
}

public static void isLegalAPICall(Vector fqcnSet) throws Error {
UtilsJavaSE.StackTraceLocation ste = UtilsJavaSE.getLocation(fqcnSet);
if (ste != null) {
if (ste.className.startsWith("javax.bluetooth.")) {
return;
}
if (ste.className.startsWith(blueCoveImplPackage + ".")) {
return;
}
throw new Error("Illegal use of the JSR-82 API");
}
}
}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 7514 bytes, 299 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting