Skip to content

Commit d72120f

Browse files
committed
Java 6 did not support binary literals. Switched to hex.
1 parent 96cc85d commit d72120f

3 files changed

Lines changed: 115 additions & 145 deletions

File tree

src/main/java/com/maxmind/maxminddb/Decoder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public Result decode(long offset) throws MaxMindDbException, IOException {
193193

194194
private Result decodePointer(int ctrlByte, long offset) throws IOException {
195195

196-
int pointerSize = ((ctrlByte >>> 3) & 0b00000011) + 1;
196+
int pointerSize = ((ctrlByte >>> 3) & 0x3) + 1;
197197

198198
if (this.DEBUG) {
199199
Log.debug("Pointer size", String.valueOf(pointerSize));
@@ -208,7 +208,7 @@ private Result decodePointer(int ctrlByte, long offset) throws IOException {
208208
}
209209

210210
buffer.put(0, pointerSize == 4 ? (byte) 0
211-
: (byte) (ctrlByte & 0b00000111));
211+
: (byte) (ctrlByte & 0x7));
212212

213213
long packed = Util.decodeLong(buffer.array());
214214

@@ -329,7 +329,7 @@ private Result decodeMap(long size, long offset) throws MaxMindDbException,
329329

330330
private long[] sizeFromCtrlByte(int ctrlByte, long offset)
331331
throws IOException {
332-
int size = ctrlByte & 0b00011111;
332+
int size = ctrlByte & 0x1f;
333333

334334
if (size < 29) {
335335
return new long[] { size, offset };

src/test/java/com/maxmind/maxminddb/DecoderTest.java

Lines changed: 109 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -21,97 +21,80 @@
2121
import com.fasterxml.jackson.databind.node.ArrayNode;
2222
import com.fasterxml.jackson.databind.node.ObjectNode;
2323

24+
@SuppressWarnings("boxing")
2425
public class DecoderTest {
2526

2627
private static Map<Integer, byte[]> int32() {
27-
int max = ( 2 << 30) - 1;
28+
int max = (2 << 30) - 1;
2829
HashMap<Integer, byte[]> int32 = new HashMap<Integer, byte[]>();
2930

30-
int32.put(0, new byte[] { 0b00000000, 0b00000001 });
31-
int32.put(-1, new byte[] { 0b00000100, 0b00000001,
32-
(byte) 0b11111111, (byte) 0b11111111, (byte) 0b11111111,
33-
(byte) 0b11111111 });
34-
int32.put((2 << 7) - 1, new byte[] { 0b00000001,
35-
0b00000001, (byte) 0b11111111 });
36-
int32.put(1 - (2 << 7), new byte[] { 0b00000100,
37-
0b00000001, (byte) 0b11111111, (byte) 0b11111111,
38-
(byte) 0b11111111, 0b00000001 });
39-
int32.put(500, new byte[] { 0b00000010, 0b00000001,
40-
0b00000001, (byte) 0b11110100 });
41-
42-
int32.put(-500, new byte[] { 0b00000100, 0b00000001,
43-
(byte) 0b11111111, (byte) 0b11111111, (byte) 0b11111110,
44-
0b00001100 });
45-
46-
int32.put((2 << 15) - 1, new byte[] { 0b00000010,
47-
0b00000001, (byte) 0b11111111, (byte) 0b11111111 });
48-
int32.put(1 - (2 << 15), new byte[] { 0b00000100,
49-
0b00000001, (byte) 0b11111111, (byte) 0b11111111, 0b00000000,
50-
0b00000001 });
51-
int32.put((2 << 23) - 1, new byte[] { 0b00000011,
52-
0b00000001, (byte) 0b11111111, (byte) 0b11111111,
53-
(byte) 0b11111111 });
54-
int32.put(1 - (2 << 23), new byte[] { 0b00000100,
55-
0b00000001, (byte) 0b11111111, 0b00000000, 0b00000000,
56-
0b00000001 });
57-
int32.put(max, new byte[] { 0b00000100, 0b00000001,
58-
0b01111111, (byte) 0b11111111, (byte) 0b11111111,
59-
(byte) 0b11111111 });
60-
int32.put(-max, new byte[] { 0b00000100, 0b00000001,
61-
(byte) 0b10000000, 0b00000000, 0b00000000, 0b00000001 });
31+
int32.put(0, new byte[] { 0x0, 0x1 });
32+
int32.put(-1, new byte[] { 0x4, 0x1, (byte) 0xff, (byte) 0xff,
33+
(byte) 0xff, (byte) 0xff });
34+
int32.put((2 << 7) - 1, new byte[] { 0x1, 0x1, (byte) 0xff });
35+
int32.put(1 - (2 << 7), new byte[] { 0x4, 0x1, (byte) 0xff,
36+
(byte) 0xff, (byte) 0xff, 0x1 });
37+
int32.put(500, new byte[] { 0x2, 0x1, 0x1, (byte) 0xf4 });
38+
39+
int32.put(-500, new byte[] { 0x4, 0x1, (byte) 0xff, (byte) 0xff,
40+
(byte) 0xfe, 0xc });
41+
42+
int32.put((2 << 15) - 1, new byte[] { 0x2, 0x1, (byte) 0xff,
43+
(byte) 0xff });
44+
int32.put(1 - (2 << 15), new byte[] { 0x4, 0x1, (byte) 0xff,
45+
(byte) 0xff, 0x0, 0x1 });
46+
int32.put((2 << 23) - 1, new byte[] { 0x3, 0x1, (byte) 0xff,
47+
(byte) 0xff, (byte) 0xff });
48+
int32.put(1 - (2 << 23), new byte[] { 0x4, 0x1, (byte) 0xff, 0x0, 0x0,
49+
0x1 });
50+
int32.put(max, new byte[] { 0x4, 0x1, 0x7f, (byte) 0xff, (byte) 0xff,
51+
(byte) 0xff });
52+
int32.put(-max, new byte[] { 0x4, 0x1, (byte) 0x80, 0x0, 0x0, 0x1 });
6253
return int32;
6354
}
6455

6556
private static Map<Long, byte[]> uint32() {
6657
long max = (((long) 1) << 32) - 1;
6758
HashMap<Long, byte[]> uint32s = new HashMap<Long, byte[]>();
6859

69-
uint32s.put((long) 0, new byte[] { (byte) 0b11000000 });
70-
uint32s.put((long) ((1 << 8) - 1), new byte[] { (byte) 0b11000001,
71-
(byte) 0b11111111 });
72-
uint32s.put((long) 500, new byte[] { (byte) 0b11000010,
73-
0b00000001, (byte) 0b11110100 });
74-
uint32s.put((long) 10872, new byte[] { (byte) 0b11000010,
75-
0b00101010, 0b01111000 });
76-
uint32s.put((long) ((1 << 16) - 1), new byte[] {
77-
(byte) 0b11000010, (byte) 0b11111111, (byte) 0b11111111 });
78-
uint32s.put((long) ((1 << 24) - 1), new byte[] {
79-
(byte) 0b11000011, (byte) 0b11111111, (byte) 0b11111111,
80-
(byte) 0b11111111 });
81-
uint32s.put(max, new byte[] { (byte) 0b11000100, (byte) 0b11111111,
82-
(byte) 0b11111111, (byte) 0b11111111, (byte) 0b11111111 });
60+
uint32s.put((long) 0, new byte[] { (byte) 0xc0 });
61+
uint32s.put((long) ((1 << 8) - 1), new byte[] { (byte) 0xc1,
62+
(byte) 0xff });
63+
uint32s.put((long) 500, new byte[] { (byte) 0xc2, 0x1, (byte) 0xf4 });
64+
uint32s.put((long) 10872, new byte[] { (byte) 0xc2, 0x2a, 0x78 });
65+
uint32s.put((long) ((1 << 16) - 1), new byte[] { (byte) 0xc2,
66+
(byte) 0xff, (byte) 0xff });
67+
uint32s.put((long) ((1 << 24) - 1), new byte[] { (byte) 0xc3,
68+
(byte) 0xff, (byte) 0xff, (byte) 0xff });
69+
uint32s.put(max, new byte[] { (byte) 0xc4, (byte) 0xff, (byte) 0xff,
70+
(byte) 0xff, (byte) 0xff });
8371

8472
return uint32s;
8573
}
8674

87-
@SuppressWarnings("boxing")
8875
private static Map<Integer, byte[]> uint16() {
8976
int max = (1 << 16) - 1;
9077

9178
Map<Integer, byte[]> uint16s = new HashMap<Integer, byte[]>();
9279

93-
uint16s.put(0, new byte[] { (byte) 0b10100000 });
94-
uint16s.put((1 << 8) - 1, new byte[] { (byte) 0b10100001,
95-
(byte) 0b11111111 });
96-
uint16s.put(500, new byte[] { (byte) 0b10100010, 0b00000001,
97-
(byte) 0b11110100 });
98-
uint16s.put(10872, new byte[] { (byte) 0b10100010, 0b00101010,
99-
0b01111000 });
100-
uint16s.put(max, new byte[] { (byte) 0b10100010, (byte) 0b11111111,
101-
(byte) 0b11111111 });
80+
uint16s.put(0, new byte[] { (byte) 0xa0 });
81+
uint16s.put((1 << 8) - 1, new byte[] { (byte) 0xa1, (byte) 0xff });
82+
uint16s.put(500, new byte[] { (byte) 0xa2, 0x1, (byte) 0xf4 });
83+
uint16s.put(10872, new byte[] { (byte) 0xa2, 0x2a, 0x78 });
84+
uint16s.put(max, new byte[] { (byte) 0xa2, (byte) 0xff, (byte) 0xff });
10285
return uint16s;
10386
}
10487

10588
private static Map<BigInteger, byte[]> largeUint(int bits) {
10689
Map<BigInteger, byte[]> uints = new HashMap<BigInteger, byte[]>();
10790

108-
byte ctrlByte = (byte) (bits == 64 ? 0b00000010 : 0b00000011);
91+
byte ctrlByte = (byte) (bits == 64 ? 0x2 : 0x3);
10992

110-
uints.put(BigInteger.valueOf(0), new byte[] { 0b00000000, ctrlByte });
111-
uints.put(BigInteger.valueOf(500), new byte[] { 0b00000010, ctrlByte,
112-
0b00000001, (byte) 0b11110100 });
113-
uints.put(BigInteger.valueOf(10872), new byte[] { 0b00000010, ctrlByte,
114-
0b00101010, 0b01111000 });
93+
uints.put(BigInteger.valueOf(0), new byte[] { 0x0, ctrlByte });
94+
uints.put(BigInteger.valueOf(500), new byte[] { 0x2, ctrlByte, 0x1,
95+
(byte) 0xf4 });
96+
uints.put(BigInteger.valueOf(10872), new byte[] { 0x2, ctrlByte, 0x2a,
97+
0x78 });
11598

11699
for (int power = 1; power <= bits / 8; power++) {
117100

@@ -122,7 +105,7 @@ private static Map<BigInteger, byte[]> largeUint(int bits) {
122105
value[0] = (byte) power;
123106
value[1] = ctrlByte;
124107
for (int i = 2; i < value.length; i++) {
125-
value[i] = (byte) 0b11111111;
108+
value[i] = (byte) 0xff;
126109
}
127110
uints.put(key, value);
128111
}
@@ -133,53 +116,45 @@ private static Map<BigInteger, byte[]> largeUint(int bits) {
133116
private static Map<Long, byte[]> pointers() {
134117
Map<Long, byte[]> pointers = new HashMap<Long, byte[]>();
135118

136-
pointers.put((long) 0, new byte[] { 0b00100000, 0b00000000 });
137-
pointers.put((long) 5, new byte[] { 0b00100000, 0b00000101 });
138-
pointers.put((long) 10, new byte[] { 0b00100000, 0b00001010 });
139-
pointers.put((long) ((1 << 10) - 1), new byte[] { 0b00100011,
140-
(byte) 0b11111111, });
141-
pointers.put((long) 3017, new byte[] { 0b00101000, 0b00000011,
142-
(byte) 0b11001001 });
143-
pointers.put((long) ((1 << 19) - 5), new byte[] { 0b00101111,
144-
(byte) 0b11110111, (byte) 0b11111011 });
145-
pointers.put((long) ((1 << 19) + (1 << 11) - 1), new byte[] {
146-
0b00101111, (byte) 0b11111111, (byte) 0b11111111 });
147-
pointers.put((long) ((1 << 27) - 2), new byte[] { 0b00110111,
148-
(byte) 0b11110111, (byte) 0b11110111, (byte) 0b11111110 });
149-
pointers.put(
150-
(((long) 1) << 27) + (1 << 19) + (1 << 11) - 1,
151-
new byte[] { 0b00110111, (byte) 0b11111111, (byte) 0b11111111,
152-
(byte) 0b11111111 });
153-
pointers.put((((long) 1) << 32) - 1, new byte[] {
154-
0b00111000, (byte) 0b11111111, (byte) 0b11111111,
155-
(byte) 0b11111111, (byte) 0b11111111 });
119+
pointers.put((long) 0, new byte[] { 0x20, 0x0 });
120+
pointers.put((long) 5, new byte[] { 0x20, 0x5 });
121+
pointers.put((long) 10, new byte[] { 0x20, 0xa });
122+
pointers.put((long) ((1 << 10) - 1), new byte[] { 0x23, (byte) 0xff, });
123+
pointers.put((long) 3017, new byte[] { 0x28, 0x3, (byte) 0xc9 });
124+
pointers.put((long) ((1 << 19) - 5), new byte[] { 0x2f, (byte) 0xf7,
125+
(byte) 0xfb });
126+
pointers.put((long) ((1 << 19) + (1 << 11) - 1), new byte[] { 0x2f,
127+
(byte) 0xff, (byte) 0xff });
128+
pointers.put((long) ((1 << 27) - 2), new byte[] { 0x37, (byte) 0xf7,
129+
(byte) 0xf7, (byte) 0xfe });
130+
pointers.put((((long) 1) << 27) + (1 << 19) + (1 << 11) - 1,
131+
new byte[] { 0x37, (byte) 0xff, (byte) 0xff, (byte) 0xff });
132+
pointers.put((((long) 1) << 32) - 1, new byte[] { 0x38, (byte) 0xff,
133+
(byte) 0xff, (byte) 0xff, (byte) 0xff });
156134
return pointers;
157135
}
158136

159137
Map<String, byte[]> strings() {
160138
Map<String, byte[]> strings = new HashMap<String, byte[]>();
161139

162-
this.addTestString(strings, (byte) 0b01000000, "");
163-
this.addTestString(strings, (byte) 0b01000001, "1");
164-
this.addTestString(strings, (byte) 0b01000011, "人");
165-
this.addTestString(strings, (byte) 0b01000011, "123");
166-
this.addTestString(strings, (byte) 0b01011011,
167-
"123456789012345678901234567");
168-
this.addTestString(strings, (byte) 0b01011100,
169-
"1234567890123456789012345678");
170-
this.addTestString(strings, (byte) 0b01011100,
171-
"1234567890123456789012345678");
172-
this.addTestString(strings, new byte[] { 0b01011101, 0b00000000 },
140+
this.addTestString(strings, (byte) 0x40, "");
141+
this.addTestString(strings, (byte) 0x41, "1");
142+
this.addTestString(strings, (byte) 0x43, "人");
143+
this.addTestString(strings, (byte) 0x43, "123");
144+
this.addTestString(strings, (byte) 0x5b, "123456789012345678901234567");
145+
this.addTestString(strings, (byte) 0x5c, "1234567890123456789012345678");
146+
this.addTestString(strings, (byte) 0x5c, "1234567890123456789012345678");
147+
this.addTestString(strings, new byte[] { 0x5d, 0x0 },
173148
"12345678901234567890123456789");
174-
this.addTestString(strings, new byte[] { 0b01011101, 0b00000001 },
149+
this.addTestString(strings, new byte[] { 0x5d, 0x1 },
175150
"123456789012345678901234567890");
176151

177-
this.addTestString(strings, new byte[] { 0b01011110, 0b00000000,
178-
(byte) 0b11010111 }, this.xString(500));
179-
this.addTestString(strings, new byte[] { 0b01011110, 0b00000110,
180-
(byte) 0b10110011 }, this.xString(2000));
181-
this.addTestString(strings, new byte[] { 0b01011111, 0b00000000,
182-
0b00010000, 0b01010011, }, this.xString(70000));
152+
this.addTestString(strings, new byte[] { 0x5e, 0x0, (byte) 0xd7 },
153+
this.xString(500));
154+
this.addTestString(strings, new byte[] { 0x5e, 0x6, (byte) 0xb3 },
155+
this.xString(2000));
156+
this.addTestString(strings, new byte[] { 0x5f, 0x0, 0x10, 0x53, },
157+
this.xString(70000));
183158

184159
return strings;
185160

@@ -192,7 +167,7 @@ Map<byte[], byte[]> bytes() {
192167

193168
for (String s : strings.keySet()) {
194169
byte[] ba = strings.get(s);
195-
ba[0] ^= 0b11000000;
170+
ba[0] ^= 0xc0;
196171

197172
bytes.put(s.getBytes(Charset.forName("UTF-8")), ba);
198173
}
@@ -225,12 +200,12 @@ private void addTestString(Map<String, byte[]> tests, byte ctrl[],
225200

226201
Map<Double, byte[]> doubles() {
227202
Map<Double, byte[]> doubles = new HashMap<Double, byte[]>();
228-
this.addTestDouble(doubles, (byte) 0b01110001, "-1073741824.12457");
229-
this.addTestDouble(doubles, (byte) 0b01110000, "1073741824.12457");
230-
this.addTestDouble(doubles, (byte) 0b01101110, "-3.14159265359");
231-
this.addTestDouble(doubles, (byte) 0b01100011, "123");
232-
this.addTestDouble(doubles, (byte) 0b01100010, ".5");
233-
this.addTestDouble(doubles, (byte) 0b01100011, "-.5");
203+
this.addTestDouble(doubles, (byte) 0x71, "-1073741824.12457");
204+
this.addTestDouble(doubles, (byte) 0x70, "1073741824.12457");
205+
this.addTestDouble(doubles, (byte) 0x6e, "-3.14159265359");
206+
this.addTestDouble(doubles, (byte) 0x63, "123");
207+
this.addTestDouble(doubles, (byte) 0x62, ".5");
208+
this.addTestDouble(doubles, (byte) 0x63, "-.5");
234209
return doubles;
235210
}
236211

@@ -247,8 +222,8 @@ private void addTestDouble(Map<Double, byte[]> tests, byte ctrl, String str) {
247222
Map<Boolean, byte[]> booleans() {
248223
Map<Boolean, byte[]> booleans = new HashMap<Boolean, byte[]>();
249224

250-
booleans.put(Boolean.FALSE, new byte[] { 0b00000000, 0b00000111 });
251-
booleans.put(Boolean.TRUE, new byte[] { 0b00000001, 0b00000111 });
225+
booleans.put(Boolean.FALSE, new byte[] { 0x0, 0x7 });
226+
booleans.put(Boolean.TRUE, new byte[] { 0x1, 0x7 });
252227
return booleans;
253228
}
254229

@@ -258,41 +233,38 @@ Map<ObjectNode, byte[]> maps() {
258233
ObjectMapper om = new ObjectMapper();
259234

260235
ObjectNode empty = om.createObjectNode();
261-
maps.put(empty, new byte[] { (byte) 0b11100000 });
236+
maps.put(empty, new byte[] { (byte) 0xe0 });
262237

263238
ObjectNode one = om.createObjectNode();
264239
one.put("en", "Foo");
265-
maps.put(one, new byte[] { (byte) 0b11100001, /* en */0b01000010,
266-
0b01100101, 0b01101110,
267-
/* Foo */0b01000011, 0b01000110, 0b01101111, 0b01101111 });
240+
maps.put(one, new byte[] { (byte) 0xe1, /* en */0x42, 0x65, 0x6e,
241+
/* Foo */0x43, 0x46, 0x6f, 0x6f });
268242

269243
ObjectNode two = om.createObjectNode();
270244
two.put("en", "Foo");
271245
two.put("zh", "人");
272-
maps.put(two, new byte[] { (byte) 0b11100010,
246+
maps.put(two, new byte[] { (byte) 0xe2,
273247
/* en */
274-
0b01000010, 0b01100101, 0b01101110,
248+
0x42, 0x65, 0x6e,
275249
/* Foo */
276-
0b01000011, 0b01000110, 0b01101111, 0b01101111,
250+
0x43, 0x46, 0x6f, 0x6f,
277251
/* zh */
278-
0b01000010, 0b01111010, 0b01101000,
252+
0x42, 0x7a, 0x68,
279253
/* 人 */
280-
0b01000011, (byte) 0b11100100, (byte) 0b10111010, (byte) 0b10111010 });
254+
0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba });
281255

282256
ObjectNode nested = om.createObjectNode();
283257
nested.put("name", two);
284258

285-
maps.put(nested, new byte[] { (byte) 0b11100001, /* name */
286-
0b01000100, 0b01101110, 0b01100001, 0b01101101, 0b01100101,
287-
(byte) 0b11100010,/* en */
288-
0b01000010, 0b01100101, 0b01101110,
289-
/* Foo */
290-
0b01000011, 0b01000110, 0b01101111, 0b01101111,
291-
/* zh */
292-
0b01000010, 0b01111010, 0b01101000,
293-
/* 人 */
294-
0b01000011, (byte) 0b11100100, (byte) 0b10111010,
295-
(byte) 0b10111010 });
259+
maps.put(nested, new byte[] { (byte) 0xe1, /* name */
260+
0x44, 0x6e, 0x61, 0x6d, 0x65, (byte) 0xe2,/* en */
261+
0x42, 0x65, 0x6e,
262+
/* Foo */
263+
0x43, 0x46, 0x6f, 0x6f,
264+
/* zh */
265+
0x42, 0x7a, 0x68,
266+
/* 人 */
267+
0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba });
296268

297269
/*
298270
* This currently isn't working as assertEquals thinks all arrays are
@@ -321,21 +293,21 @@ Map<ArrayNode, byte[]> arrays() {
321293

322294
ArrayNode f1 = om.createArrayNode();
323295
f1.add("Foo");
324-
arrays.put(f1, new byte[] { 0b00000001, 0b00000100,
296+
arrays.put(f1, new byte[] { 0x1, 0x4,
325297
/* Foo */
326-
0b01000011, 0b01000110, 0b01101111, 0b01101111 });
298+
0x43, 0x46, 0x6f, 0x6f });
327299

328300
ArrayNode f2 = om.createArrayNode();
329301
f2.add("Foo");
330302
f2.add("人");
331-
arrays.put(f2, new byte[] { 0b00000010, 0b00000100,
303+
arrays.put(f2, new byte[] { 0x2, 0x4,
332304
/* Foo */
333-
0b01000011, 0b01000110, 0b01101111, 0b01101111,
305+
0x43, 0x46, 0x6f, 0x6f,
334306
/* 人 */
335-
0b01000011, (byte) 0b11100100, (byte) 0b10111010, (byte) 0b10111010 });
307+
0x43, (byte) 0xe4, (byte) 0xba, (byte) 0xba });
336308

337309
ArrayNode empty = om.createArrayNode();
338-
arrays.put(empty, new byte[] { 0b00000000, 0b00000100 });
310+
arrays.put(empty, new byte[] { 0x0, 0x4 });
339311

340312
return arrays;
341313
}

0 commit comments

Comments
 (0)