blob: dbb95c93328e1ad7fc16c9e541533830b6f61044 (
plain) (
blame)
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
|
package org.uic.barcode.asn1.uper;
public class AsnUtils {
private static byte[] mask = new byte[] {
(byte) 0b1000_0000,
0b0100_0000,
0b0010_0000,
0b0001_0000,
0b0000_1000,
0b0000_0100,
0b0000_0010,
0b0000_0001,
};
public static byte[] fromBooleanString(final String s) {
char[] ascii = s.toCharArray();
if (ascii == null || ascii.length == 0) {
return null;
}
// get length/8 times bytes with 3 bit shifts to the right of the length
final byte[] l_raw = new byte[ascii.length >> 3];
/*
* We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
* loop.
*/
for (int ii = 0, jj = 0; ii < l_raw.length; ii++, jj += 8) {
for (int bits = 0; bits < mask.length; ++bits) {
if (ascii[jj + bits] == '1') {
l_raw[ii] |= mask[bits];
}
}
}
return l_raw;
}
public static String toBooleanString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length);
for (int i = 0; i < bytes.length*8;i++) {
sb.append(AsnUtils.get(bytes,i) ? "1" : "0");
}
return sb.toString();
}
public static boolean get(byte[] bytes, int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("Index " + index + " is less than 0");
} else if (index >= bytes.length * 8) {
throw new IndexOutOfBoundsException("Index " + index + " violates the limit " + bytes.length*8);
}
boolean result = (bytes[index / 8] & mask[index % 8]) != 0;
return result;
}
}
|