1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.sentrysoftware.wbem.sblim.cimclient.internal.cim;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import org.sentrysoftware.wbem.javax.cim.UnsignedInteger8;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class CIMOctetString {
67
68 private UnsignedInteger8 iBytes[];
69
70 private String iASCIIString;
71
72 private char iReplacementChar;
73
74 private String iHexString;
75
76 private int iLength;
77
78
79
80
81
82
83
84
85 public CIMOctetString(UnsignedInteger8 pBytes[]) throws IllegalArgumentException {
86
87 if (pBytes == null || pBytes.length < 4) throw new IllegalArgumentException(
88 "Array of bytes must contain at least four bytes");
89
90
91 for (int i = pBytes.length - 1; i >= 0; i--)
92 if (pBytes[i] == null) throw new IllegalArgumentException(
93 "Array of bytes must not contain any null bytes");
94
95
96 this.iLength = pBytes[3].byteValue() + (pBytes[2].byteValue() * 0x100)
97 + (pBytes[1].byteValue() * 0x10000) + (pBytes[0].byteValue() * 0x1000000);
98
99
100 if (this.iLength != pBytes.length) throw new IllegalArgumentException(
101 "Array of bytes contains invalid length: found " + this.iLength + ", expected "
102 + pBytes.length);
103
104
105 this.iBytes = new UnsignedInteger8[this.iLength];
106 for (int i = this.iLength - 1; i >= 0; i--)
107 this.iBytes[i] = pBytes[i];
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121 public CIMOctetString(String pString, boolean pIsHex) throws IllegalArgumentException {
122 if (pString == null) throw new IllegalArgumentException("String cannot be null");
123
124 if (pIsHex) {
125
126 if (pString.length() < 10) throw new IllegalArgumentException(
127 "Hexadecimal string must contain \"0x\" and at least four pairs of hex digits");
128
129
130 if (pString.charAt(0) != '0' || pString.charAt(1) != 'x') throw new IllegalArgumentException(
131 "Hexadecimal string must begin with \"0x\"");
132
133
134 try {
135 this.iLength = Integer.parseInt(pString.substring(2, 10), 16);
136 } catch (NumberFormatException e) {
137 throw new IllegalArgumentException(
138 "Hexadecimal string length could not be parsed: " + e.toString());
139 }
140
141
142 if ((this.iLength * 2) + 2 != pString.length()) throw new IllegalArgumentException(
143 "Hexadecimal string contains invalid length: found " + this.iLength
144 + ", expected " + ((pString.length() - 2 / 2)));
145
146
147
148 for (int i = pString.length() - 1; i >= 10; i--) {
149 char ch = pString.charAt(i);
150 if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) throw new IllegalArgumentException(
151 "Hexadecimal string could not be parsed, invalid character \'" + ch
152 + "\' at index " + i);
153 }
154
155
156 this.iHexString = new String(pString);
157 } else {
158
159 this.iLength = pString.length() + 4;
160
161
162 this.iASCIIString = new String(pString);
163 this.iReplacementChar = 0xFF;
164 }
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 @Override
184 public synchronized boolean equals(Object pObj) {
185
186 if (!(pObj instanceof CIMOctetString)) return false;
187
188 CIMOctetString that = (CIMOctetString) pObj;
189 int numCompares = 0;
190
191
192 if (this.iLength != that.iLength) return false;
193
194
195 if (this.iBytes != null && that.iBytes != null) {
196 for (int i = this.iLength - 1; i >= 0; i--)
197 if (this.iBytes[i].byteValue() != that.iBytes[i].byteValue()) return false;
198 numCompares++;
199 }
200
201
202 if (this.iHexString != null && that.iHexString != null) {
203 if (!this.iHexString.equalsIgnoreCase(that.iHexString)) return false;
204 numCompares++;
205 }
206
207
208 if (this.iASCIIString != null && that.iASCIIString != null
209 && this.iReplacementChar == that.iReplacementChar) {
210 if (!this.iASCIIString.equalsIgnoreCase(that.iASCIIString)) return false;
211 numCompares++;
212 }
213
214
215 if (numCompares > 0) return true;
216
217
218
219
220 if (this.iBytes != null && that.iBytes == null) {
221 that.getBytes();
222 if (this.iBytes != null && that.iBytes != null) {
223 for (int i = this.iLength - 1; i >= 0; i--)
224 if (this.iBytes[i].byteValue() != that.iBytes[i].byteValue()) return false;
225 numCompares++;
226 }
227 }
228
229
230 if (numCompares > 0) return true;
231
232 if (this.iBytes == null && that.iBytes != null) {
233 getBytes();
234 if (this.iBytes != null && that.iBytes != null) {
235 for (int i = this.iLength - 1; i >= 0; i--)
236 if (this.iBytes[i].byteValue() != that.iBytes[i].byteValue()) return false;
237 numCompares++;
238 }
239 }
240
241
242 if (numCompares > 0) return true;
243
244 if (this.iHexString != null && that.iHexString == null) {
245 that.getHexString();
246 if (this.iHexString != null && that.iHexString != null) {
247 if (!this.iHexString.equalsIgnoreCase(that.iHexString)) return false;
248 numCompares++;
249 }
250 }
251
252
253 if (numCompares > 0) return true;
254
255 if (this.iHexString == null && that.iHexString != null) {
256 getHexString();
257 if (this.iHexString != null && that.iHexString != null) {
258 if (!this.iHexString.equalsIgnoreCase(that.iHexString)) return false;
259 numCompares++;
260 }
261 }
262
263
264 if (numCompares > 0) return true;
265
266 return false;
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280 public synchronized String getASCIIString(char pReplacementChar) {
281
282 if (this.iASCIIString != null && this.iReplacementChar == 0xFF) return this.iASCIIString;
283
284
285 if (pReplacementChar <= 0x1F || pReplacementChar >= 0x7F) throw new IllegalArgumentException(
286 "Replacement character not printable");
287
288
289 if (this.iASCIIString != null && this.iReplacementChar == pReplacementChar) return this.iASCIIString;
290
291
292 StringBuilder str = new StringBuilder("");
293 if (this.iBytes != null) {
294 for (int i = 4; i < this.iBytes.length; i++) {
295 char ch = (char) this.iBytes[i].byteValue();
296 if (ch <= 0x1F || ch >= 0x7F) str.append(pReplacementChar);
297 else str.append(ch);
298 }
299 } else {
300 for (int i = 10; i < this.iHexString.length(); i += 2) {
301 char ch = (char) Integer.parseInt(this.iHexString.substring(i, i + 2), 16);
302 if (ch <= 0x1F || ch >= 0x7F) str.append(pReplacementChar);
303 else str.append(ch);
304 }
305 }
306
307
308
309 this.iASCIIString = new String(str);
310 this.iReplacementChar = pReplacementChar;
311
312 return this.iASCIIString;
313 }
314
315
316
317
318
319
320
321
322 public synchronized UnsignedInteger8[] getBytes() {
323 if (this.iBytes != null) return this.iBytes;
324
325 if (this.iHexString != null) {
326 convertHexStringToBytes();
327 } else {
328 convertASCIIStringToBytes();
329 }
330
331 return this.iBytes;
332 }
333
334
335
336
337
338
339
340
341 public synchronized String getHexString() {
342 if (this.iHexString != null) return this.iHexString;
343
344 if (this.iBytes != null) {
345 convertBytesToHexString();
346 } else {
347 convertASCIIStringToHexString();
348 }
349
350 return this.iHexString;
351 }
352
353
354
355
356
357
358 @Override
359 public int hashCode() {
360 return toString().toLowerCase().hashCode();
361 }
362
363
364
365
366
367
368
369 public int length() {
370 return this.iLength;
371 }
372
373
374
375
376
377
378 @Override
379 public String toString() {
380 return getHexString();
381 }
382
383 private void convertBytesToHexString() {
384
385 StringBuilder str = new StringBuilder("0x");
386
387
388 String len = Integer.toHexString(this.iLength);
389 for (int i = 8 - len.length(); i > 0; i--)
390 str.append('0');
391 str.append(len);
392
393
394 for (int i = 4; i < this.iLength; i++) {
395 String octet = Integer.toHexString(this.iBytes[i].intValue());
396 if (octet.length() == 1) str.append('0');
397 str.append(octet);
398 }
399
400
401 this.iHexString = new String(str);
402
403
404
405 }
406
407 private void convertHexStringToBytes() {
408
409 this.iBytes = new UnsignedInteger8[this.iLength];
410
411
412 for (int idxByte = 0, idxStr = 2, len = this.iHexString.length(); idxStr < len; idxByte++, idxStr += 2) {
413 short s;
414 try {
415 s = Short.parseShort(this.iHexString.substring(idxStr, idxStr + 2), 16);
416 } catch (NumberFormatException e) {
417 throw new IllegalArgumentException("Hex string length could not be parsed: "
418 + e.toString());
419 }
420 this.iBytes[idxByte] = new UnsignedInteger8(s);
421 }
422
423
424
425 }
426
427 private void convertASCIIStringToBytes() {
428
429 this.iBytes = new UnsignedInteger8[this.iLength];
430
431
432 this.iBytes[0] = new UnsignedInteger8((short) ((this.iLength >> 24) & 0xFF));
433 this.iBytes[1] = new UnsignedInteger8((short) ((this.iLength >> 16) & 0xFF));
434 this.iBytes[2] = new UnsignedInteger8((short) ((this.iLength >> 8) & 0xFF));
435 this.iBytes[3] = new UnsignedInteger8((short) (this.iLength & 0xFF));
436
437
438 for (int idxStr = 0, idxByte = 4; idxStr < this.iASCIIString.length(); idxStr++, idxByte++)
439 this.iBytes[idxByte] = new UnsignedInteger8((short) (this.iASCIIString.charAt(idxStr)));
440
441
442
443 }
444
445 private void convertASCIIStringToHexString() {
446
447 StringBuilder str = new StringBuilder("0x");
448
449
450 String len = Integer.toHexString(this.iLength);
451 for (int i = 8 - len.length(); i > 0; i--)
452 str.append('0');
453 str.append(len);
454
455
456 for (int idxAsc = 0, idxHex = 10; idxAsc < this.iASCIIString.length(); idxAsc++, idxHex++) {
457 String octet = Integer.toHexString((this.iASCIIString.charAt(idxAsc)));
458 if (octet.length() == 1) str.append('0');
459 str.append(octet);
460 }
461
462
463 this.iHexString = new String(str);
464
465
466
467 }
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484 }