-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBitString.java
More file actions
232 lines (204 loc) · 6.27 KB
/
Copy pathBitString.java
File metadata and controls
232 lines (204 loc) · 6.27 KB
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
package com.iab.gpp.encoder.bitstring;
import com.iab.gpp.encoder.datatype.FixedIntegerList;
import com.iab.gpp.encoder.datatype.IntegerSet;
import com.iab.gpp.encoder.datatype.encoder.FibonacciIntegerEncoder;
import com.iab.gpp.encoder.error.DecodingException;
import com.iab.gpp.encoder.error.EncodingException;
public final class BitString {
public static final char TRUE = '1';
public static final char FALSE = '0';
public static final String TRUE_STRING = new String(new char[] {TRUE});
public static final String FALSE_STRING = new String(new char[] {FALSE});
private final BitSet bitSet;
private int readIndex;
private int writeIndex;
public BitString(BitSet bitSet, int length) {
this.bitSet = bitSet;
this.writeIndex = length;
}
public BitString() {
this(new BitSet(), 0);
}
public static final BitString of(String str) {
int length = str.length();
BitString out = new BitString(new BitSet(length), 0);
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
if (c == TRUE) {
out.writeBoolean(true);
} else if (c == FALSE) {
out.writeBoolean(false);
} else {
throw new DecodingException("Invalid bit string");
}
}
return out;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(length());
for (int i = 0; i < writeIndex; i++) {
sb.append(bitSet.get(i) ? TRUE : FALSE);
}
return sb.toString();
}
private boolean getValue(int i) {
if (i >= writeIndex) {
throw new DecodingException("Bit string access out of range");
}
return bitSet.get(i);
}
public int length() {
return writeIndex;
}
public boolean hasRemaining() {
return readIndex < writeIndex;
}
// Used to mark/reset the read cursor when an encoding is ambiguous and may need to be re-read
// under a different interpretation (e.g. legacy fixed vs Fibonacci OptimizedRange).
public int getReadIndex() {
return readIndex;
}
public void setReadIndex(int readIndex) {
this.readIndex = readIndex;
}
public void writeEmpty(int length) {
this.writeIndex += length;
}
public void writeInt(int value, int length) {
int mask = 1 << length;
if (value >= mask) {
throw new EncodingException(
"Numeric value '" + value + "' is too large for a bit string length of '" + length + "'");
}
for (int i = 0; i < length; i++) {
mask >>= 1;
writeBoolean((mask & value) != 0);
}
}
public void writeLong(long value, int length) {
long mask = 1L << length;
if (value >= mask) {
throw new EncodingException(
"Numeric value '" + value + "' is too large for a bit string length of '" + length + "'");
}
for (int i = 0; i < length; i++) {
mask >>= 1;
writeBoolean((mask & value) != 0);
}
}
public void writeBoolean(boolean value) {
int idx = writeIndex++;
if (value) {
bitSet.set(idx);
}
}
public void writeFibonacci(int value) {
int largestIndex = 0;
for (int i = 0; i < FibonacciIntegerEncoder.FIBONACCI_LIMIT; i++) {
if (value >= FibonacciIntegerEncoder.FIBONACCI_NUMBERS[i]) {
largestIndex++;
} else {
break;
}
}
if (largestIndex == FibonacciIntegerEncoder.FIBONACCI_LIMIT) {
throw new EncodingException("Unencodable FibonacciInteger " + value);
}
int out = 1;
int mask = 1;
for (int i = largestIndex - 1; i >= 0; i--) {
mask <<= 1;
int f = FibonacciIntegerEncoder.FIBONACCI_NUMBERS[i];
if (value >= f) {
out |= mask;
value -= f;
}
}
writeInt(out, largestIndex + 1);
}
public void write(BitString other, int from, int to) {
for (int i = from; i < to; i++) {
writeBoolean(other.getValue(i));
}
}
public void write(BitString other) {
int otherLength = other.length();
for (int i = 0; i < otherLength; i++) {
writeBoolean(other.bitSet.get(i));
}
}
public int readInt(int length) {
int from = readIndex;
int to = from + length;
if (from >= writeIndex) {
throw new DecodingException("Bit string access out of range");
}
readIndex = to;
return bitSet.readInt(from, to);
}
// used for usnat v1 to v2 conversion, see note in UsNatCoreSegment
public int peekInt(int length) {
return bitSet.readInt(0, length);
}
// used for usnat v1 to v2 conversion, see note in UsNatCoreSegment
public void putInt(int value, int length) {
int mask = 1 << length;
if (value >= mask) {
throw new EncodingException(
"Numeric value '" + value + "' is too large for a bit string length of '" + length + "'");
}
for (int i = 0; i < length; i++) {
mask >>= 1;
bitSet.set(i, (mask & value) != 0);
}
}
public long readLong(int length) {
int from = readIndex;
int to = from + length;
if (from >= writeIndex) {
throw new DecodingException("Bit string access out of range");
}
readIndex = to;
return bitSet.readLong(from, to);
}
public boolean readBoolean() {
return getValue(readIndex++);
}
public int readFibonacci() {
int value = 0;
int readIndex = this.readIndex;
for (int i = 0; i < FibonacciIntegerEncoder.FIBONACCI_LIMIT; i++) {
if (getValue(readIndex + i)) {
// 1X
value += FibonacciIntegerEncoder.FIBONACCI_NUMBERS[i];
i++;
int next = readIndex + i;
if (getValue(next)) {
// 11
this.readIndex = next + 1;
return value;
}
}
}
throw new DecodingException("Invalid FibonacciInteger");
}
public IntegerSet readIntegerSet(int length) {
int newReadIndex = readIndex + length;
if (newReadIndex > writeIndex) {
throw new DecodingException("Bit string access out of range");
}
IntegerSet out = new IntegerSet(bitSet, readIndex, newReadIndex, 1);
readIndex = newReadIndex;
return out;
}
public FixedIntegerList readFixedIntegerList(int elementLength, int length) {
int newReadIndex = readIndex + elementLength * length;
if (newReadIndex > writeIndex) {
throw new DecodingException("Bit string access out of range");
}
FixedIntegerList out = new FixedIntegerList(bitSet, readIndex, elementLength, length);
readIndex = newReadIndex;
return out;
}
}