1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.nio.charset.Charset;
26 import java.nio.charset.StandardCharsets;
27
28 import org.apache.commons.codec.DecoderException;
29 import org.apache.commons.codec.EncoderException;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.params.ParameterizedTest;
34 import org.junit.jupiter.params.provider.ValueSource;
35
36
37
38
39 class BinaryCodecTest {
40
41 private static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
42
43
44 private static final int BIT_0 = 0x01;
45
46
47 private static final int BIT_1 = 0x02;
48
49
50 private static final int BIT_2 = 0x04;
51
52
53 private static final int BIT_3 = 0x08;
54
55
56 private static final int BIT_4 = 0x10;
57
58
59 private static final int BIT_5 = 0x20;
60
61
62 private static final int BIT_6 = 0x40;
63
64
65 private static final int BIT_7 = 0x80;
66
67
68 BinaryCodec instance;
69
70
71
72
73
74
75
76
77
78 void assertDecodeObject(final byte[] bits, final String encodeMe) throws DecoderException {
79 byte[] decoded;
80 decoded = (byte[]) instance.decode(encodeMe);
81 assertEquals(new String(bits), new String(decoded));
82 if (encodeMe == null) {
83 decoded = instance.decode((byte[]) null);
84 } else {
85 decoded = (byte[]) instance.decode((Object) encodeMe.getBytes(CHARSET_UTF8));
86 }
87 assertEquals(new String(bits), new String(decoded));
88 if (encodeMe == null) {
89 decoded = (byte[]) instance.decode((char[]) null);
90 } else {
91 decoded = (byte[]) instance.decode(encodeMe.toCharArray());
92 }
93 assertEquals(new String(bits), new String(decoded));
94 }
95
96 @BeforeEach
97 void setUp() throws Exception {
98 this.instance = new BinaryCodec();
99 }
100
101 @AfterEach
102 void tearDown() throws Exception {
103 this.instance = null;
104 }
105
106
107
108
109 @Test
110 void testDecodeByteArray() {
111
112 byte[] bits = new byte[1];
113 byte[] decoded = instance.decode("00000000".getBytes(CHARSET_UTF8));
114 assertEquals(new String(bits), new String(decoded));
115 bits = new byte[1];
116 bits[0] = BIT_0;
117 decoded = instance.decode("00000001".getBytes(CHARSET_UTF8));
118 assertEquals(new String(bits), new String(decoded));
119 bits = new byte[1];
120 bits[0] = BIT_0 | BIT_1;
121 decoded = instance.decode("00000011".getBytes(CHARSET_UTF8));
122 assertEquals(new String(bits), new String(decoded));
123 bits = new byte[1];
124 bits[0] = BIT_0 | BIT_1 | BIT_2;
125 decoded = instance.decode("00000111".getBytes(CHARSET_UTF8));
126 assertEquals(new String(bits), new String(decoded));
127 bits = new byte[1];
128 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
129 decoded = instance.decode("00001111".getBytes(CHARSET_UTF8));
130 assertEquals(new String(bits), new String(decoded));
131 bits = new byte[1];
132 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
133 decoded = instance.decode("00011111".getBytes(CHARSET_UTF8));
134 assertEquals(new String(bits), new String(decoded));
135 bits = new byte[1];
136 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
137 decoded = instance.decode("00111111".getBytes(CHARSET_UTF8));
138 assertEquals(new String(bits), new String(decoded));
139 bits = new byte[1];
140 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
141 decoded = instance.decode("01111111".getBytes(CHARSET_UTF8));
142 assertEquals(new String(bits), new String(decoded));
143 bits = new byte[1];
144 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
145 decoded = instance.decode("11111111".getBytes(CHARSET_UTF8));
146 assertEquals(new String(bits), new String(decoded));
147
148 bits = new byte[2];
149 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
150 decoded = instance.decode("0000000011111111".getBytes(CHARSET_UTF8));
151 assertEquals(new String(bits), new String(decoded));
152 bits = new byte[2];
153 bits[1] = BIT_0;
154 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
155 decoded = instance.decode("0000000111111111".getBytes(CHARSET_UTF8));
156 assertEquals(new String(bits), new String(decoded));
157 bits = new byte[2];
158 bits[1] = BIT_0 | BIT_1;
159 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
160 decoded = instance.decode("0000001111111111".getBytes(CHARSET_UTF8));
161 assertEquals(new String(bits), new String(decoded));
162 bits = new byte[2];
163 bits[1] = BIT_0 | BIT_1 | BIT_2;
164 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
165 decoded = instance.decode("0000011111111111".getBytes(CHARSET_UTF8));
166 assertEquals(new String(bits), new String(decoded));
167 bits = new byte[2];
168 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
169 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
170 decoded = instance.decode("0000111111111111".getBytes(CHARSET_UTF8));
171 assertEquals(new String(bits), new String(decoded));
172 bits = new byte[2];
173 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
174 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
175 decoded = instance.decode("0001111111111111".getBytes(CHARSET_UTF8));
176 assertEquals(new String(bits), new String(decoded));
177 bits = new byte[2];
178 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
179 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
180 decoded = instance.decode("0011111111111111".getBytes(CHARSET_UTF8));
181 assertEquals(new String(bits), new String(decoded));
182 bits = new byte[2];
183 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
184 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
185 decoded = instance.decode("0111111111111111".getBytes(CHARSET_UTF8));
186 assertEquals(new String(bits), new String(decoded));
187 bits = new byte[2];
188 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
189 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
190 decoded = instance.decode("1111111111111111".getBytes(CHARSET_UTF8));
191 assertEquals(new String(bits), new String(decoded));
192 }
193
194 @ParameterizedTest
195 @ValueSource(strings = { "x", "x00000000", "0000000x", "00000002", "10x0zZ!1", "0000000 ", "0000000\n", "0000000\u0000",
196 "0000000\u00ff", "0000000\uff11", "0000000x00000000" })
197 void testDecodeInvalidInput(final String ascii) {
198 final byte[] bytes = ascii.getBytes(CHARSET_UTF8);
199 final char[] chars = ascii.toCharArray();
200 assertThrows(IllegalArgumentException.class, () -> BinaryCodec.fromAscii(bytes));
201 assertThrows(IllegalArgumentException.class, () -> BinaryCodec.fromAscii(chars));
202 assertThrows(IllegalArgumentException.class, () -> instance.decode(bytes));
203 assertThrows(IllegalArgumentException.class, () -> instance.decode((Object) bytes));
204 assertThrows(IllegalArgumentException.class, () -> instance.decode((Object) chars));
205 assertThrows(IllegalArgumentException.class, () -> instance.decode((Object) ascii));
206 assertThrows(IllegalArgumentException.class, () -> instance.toByteArray(ascii));
207 }
208
209 @ParameterizedTest
210 @ValueSource(strings = { "1", "11", "111", "1111", "11111", "111111", "1111111" })
211 void testDecodeNonAlignedInput(final String prefix) throws DecoderException {
212
213 assertArrayEquals(new byte[0], BinaryCodec.fromAscii(prefix.getBytes(CHARSET_UTF8)));
214 assertArrayEquals(new byte[0], BinaryCodec.fromAscii(prefix.toCharArray()));
215 final String ascii = prefix + "00000001";
216 final byte[] bytes = ascii.getBytes(CHARSET_UTF8);
217 final char[] chars = ascii.toCharArray();
218 final byte[] expected = { BIT_0 };
219 assertArrayEquals(expected, BinaryCodec.fromAscii(bytes));
220 assertArrayEquals(expected, BinaryCodec.fromAscii(chars));
221 assertArrayEquals(expected, instance.decode(bytes));
222 assertArrayEquals(expected, (byte[]) instance.decode((Object) bytes));
223 assertArrayEquals(expected, (byte[]) instance.decode((Object) chars));
224 assertArrayEquals(expected, (byte[]) instance.decode((Object) ascii));
225 assertArrayEquals(expected, instance.toByteArray(ascii));
226 }
227
228
229
230
231 @Test
232 void testDecodeObject() throws Exception {
233 byte[] bits;
234
235 bits = new byte[1];
236 assertDecodeObject(bits, "00000000");
237 bits = new byte[1];
238 bits[0] = BIT_0;
239 assertDecodeObject(bits, "00000001");
240 bits = new byte[1];
241 bits[0] = BIT_0 | BIT_1;
242 assertDecodeObject(bits, "00000011");
243 bits = new byte[1];
244 bits[0] = BIT_0 | BIT_1 | BIT_2;
245 assertDecodeObject(bits, "00000111");
246 bits = new byte[1];
247 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
248 assertDecodeObject(bits, "00001111");
249 bits = new byte[1];
250 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
251 assertDecodeObject(bits, "00011111");
252 bits = new byte[1];
253 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
254 assertDecodeObject(bits, "00111111");
255 bits = new byte[1];
256 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
257 assertDecodeObject(bits, "01111111");
258 bits = new byte[1];
259 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
260 assertDecodeObject(bits, "11111111");
261
262 bits = new byte[2];
263 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
264 assertDecodeObject(bits, "0000000011111111");
265 bits = new byte[2];
266 bits[1] = BIT_0;
267 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
268 assertDecodeObject(bits, "0000000111111111");
269 bits = new byte[2];
270 bits[1] = BIT_0 | BIT_1;
271 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
272 assertDecodeObject(bits, "0000001111111111");
273 bits = new byte[2];
274 bits[1] = BIT_0 | BIT_1 | BIT_2;
275 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
276 assertDecodeObject(bits, "0000011111111111");
277 bits = new byte[2];
278 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
279 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
280 assertDecodeObject(bits, "0000111111111111");
281 bits = new byte[2];
282 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
283 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
284 assertDecodeObject(bits, "0001111111111111");
285 bits = new byte[2];
286 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
287 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
288 assertDecodeObject(bits, "0011111111111111");
289 bits = new byte[2];
290 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
291 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
292 assertDecodeObject(bits, "0111111111111111");
293 bits = new byte[2];
294 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
295 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
296 assertDecodeObject(bits, "1111111111111111");
297 assertDecodeObject(new byte[0], null);
298 }
299
300
301
302
303 @Test
304 void testDecodeObjectException() {
305 try {
306 this.instance.decode(new Object());
307 } catch (final DecoderException e) {
308
309 return;
310 }
311 fail("Expected DecoderException");
312 }
313
314
315
316
317 @Test
318 void testEncodeByteArray() {
319
320 byte[] bits = new byte[1];
321 String encoded = new String(instance.encode(bits));
322 assertEquals("00000000", encoded);
323 bits = new byte[1];
324 bits[0] = BIT_0;
325 encoded = new String(instance.encode(bits));
326 assertEquals("00000001", encoded);
327 bits = new byte[1];
328 bits[0] = BIT_0 | BIT_1;
329 encoded = new String(instance.encode(bits));
330 assertEquals("00000011", encoded);
331 bits = new byte[1];
332 bits[0] = BIT_0 | BIT_1 | BIT_2;
333 encoded = new String(instance.encode(bits));
334 assertEquals("00000111", encoded);
335 bits = new byte[1];
336 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
337 encoded = new String(instance.encode(bits));
338 assertEquals("00001111", encoded);
339 bits = new byte[1];
340 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
341 encoded = new String(instance.encode(bits));
342 assertEquals("00011111", encoded);
343 bits = new byte[1];
344 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
345 encoded = new String(instance.encode(bits));
346 assertEquals("00111111", encoded);
347 bits = new byte[1];
348 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
349 encoded = new String(instance.encode(bits));
350 assertEquals("01111111", encoded);
351 bits = new byte[1];
352 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
353 encoded = new String(instance.encode(bits));
354 assertEquals("11111111", encoded);
355
356 bits = new byte[2];
357 encoded = new String(instance.encode(bits));
358 assertEquals("0000000000000000", encoded);
359 bits = new byte[2];
360 bits[0] = BIT_0;
361 encoded = new String(instance.encode(bits));
362 assertEquals("0000000000000001", encoded);
363 bits = new byte[2];
364 bits[0] = BIT_0 | BIT_1;
365 encoded = new String(instance.encode(bits));
366 assertEquals("0000000000000011", encoded);
367 bits = new byte[2];
368 bits[0] = BIT_0 | BIT_1 | BIT_2;
369 encoded = new String(instance.encode(bits));
370 assertEquals("0000000000000111", encoded);
371 bits = new byte[2];
372 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
373 encoded = new String(instance.encode(bits));
374 assertEquals("0000000000001111", encoded);
375 bits = new byte[2];
376 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
377 encoded = new String(instance.encode(bits));
378 assertEquals("0000000000011111", encoded);
379 bits = new byte[2];
380 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
381 encoded = new String(instance.encode(bits));
382 assertEquals("0000000000111111", encoded);
383 bits = new byte[2];
384 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
385 encoded = new String(instance.encode(bits));
386 assertEquals("0000000001111111", encoded);
387 bits = new byte[2];
388 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
389 encoded = new String(instance.encode(bits));
390 assertEquals("0000000011111111", encoded);
391
392 bits = new byte[2];
393 bits[1] = BIT_0;
394 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
395 encoded = new String(instance.encode(bits));
396 assertEquals("0000000111111111", encoded);
397 bits = new byte[2];
398 bits[1] = BIT_0 | BIT_1;
399 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
400 encoded = new String(instance.encode(bits));
401 assertEquals("0000001111111111", encoded);
402 bits = new byte[2];
403 bits[1] = BIT_0 | BIT_1 | BIT_2;
404 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
405 encoded = new String(instance.encode(bits));
406 assertEquals("0000011111111111", encoded);
407 bits = new byte[2];
408 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
409 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
410 encoded = new String(instance.encode(bits));
411 assertEquals("0000111111111111", encoded);
412 bits = new byte[2];
413 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
414 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
415 encoded = new String(instance.encode(bits));
416 assertEquals("0001111111111111", encoded);
417 bits = new byte[2];
418 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
419 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
420 encoded = new String(instance.encode(bits));
421 assertEquals("0011111111111111", encoded);
422 bits = new byte[2];
423 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
424 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
425 encoded = new String(instance.encode(bits));
426 assertEquals("0111111111111111", encoded);
427 bits = new byte[2];
428 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
429 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
430 encoded = new String(instance.encode(bits));
431 assertEquals("1111111111111111", encoded);
432 assertEquals(0, instance.encode((byte[]) null).length);
433 }
434
435
436
437
438 @Test
439 void testEncodeObject() throws Exception {
440
441 byte[] bits = new byte[1];
442 String encoded = new String((char[]) instance.encode((Object) bits));
443 assertEquals("00000000", encoded);
444 bits = new byte[1];
445 bits[0] = BIT_0;
446 encoded = new String((char[]) instance.encode((Object) bits));
447 assertEquals("00000001", encoded);
448 bits = new byte[1];
449 bits[0] = BIT_0 | BIT_1;
450 encoded = new String((char[]) instance.encode((Object) bits));
451 assertEquals("00000011", encoded);
452 bits = new byte[1];
453 bits[0] = BIT_0 | BIT_1 | BIT_2;
454 encoded = new String((char[]) instance.encode((Object) bits));
455 assertEquals("00000111", encoded);
456 bits = new byte[1];
457 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
458 encoded = new String((char[]) instance.encode((Object) bits));
459 assertEquals("00001111", encoded);
460 bits = new byte[1];
461 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
462 encoded = new String((char[]) instance.encode((Object) bits));
463 assertEquals("00011111", encoded);
464 bits = new byte[1];
465 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
466 encoded = new String((char[]) instance.encode((Object) bits));
467 assertEquals("00111111", encoded);
468 bits = new byte[1];
469 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
470 encoded = new String((char[]) instance.encode((Object) bits));
471 assertEquals("01111111", encoded);
472 bits = new byte[1];
473 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
474 encoded = new String((char[]) instance.encode((Object) bits));
475 assertEquals("11111111", encoded);
476
477 bits = new byte[2];
478 encoded = new String((char[]) instance.encode((Object) bits));
479 assertEquals("0000000000000000", encoded);
480 bits = new byte[2];
481 bits[0] = BIT_0;
482 encoded = new String((char[]) instance.encode((Object) bits));
483 assertEquals("0000000000000001", encoded);
484 bits = new byte[2];
485 bits[0] = BIT_0 | BIT_1;
486 encoded = new String((char[]) instance.encode((Object) bits));
487 assertEquals("0000000000000011", encoded);
488 bits = new byte[2];
489 bits[0] = BIT_0 | BIT_1 | BIT_2;
490 encoded = new String((char[]) instance.encode((Object) bits));
491 assertEquals("0000000000000111", encoded);
492 bits = new byte[2];
493 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
494 encoded = new String((char[]) instance.encode((Object) bits));
495 assertEquals("0000000000001111", encoded);
496 bits = new byte[2];
497 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
498 encoded = new String((char[]) instance.encode((Object) bits));
499 assertEquals("0000000000011111", encoded);
500 bits = new byte[2];
501 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
502 encoded = new String((char[]) instance.encode((Object) bits));
503 assertEquals("0000000000111111", encoded);
504 bits = new byte[2];
505 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
506 encoded = new String((char[]) instance.encode((Object) bits));
507 assertEquals("0000000001111111", encoded);
508 bits = new byte[2];
509 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
510 encoded = new String((char[]) instance.encode((Object) bits));
511 assertEquals("0000000011111111", encoded);
512
513 bits = new byte[2];
514 bits[1] = BIT_0;
515 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
516 encoded = new String((char[]) instance.encode((Object) bits));
517 assertEquals("0000000111111111", encoded);
518 bits = new byte[2];
519 bits[1] = BIT_0 | BIT_1;
520 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
521 encoded = new String((char[]) instance.encode((Object) bits));
522 assertEquals("0000001111111111", encoded);
523 bits = new byte[2];
524 bits[1] = BIT_0 | BIT_1 | BIT_2;
525 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
526 encoded = new String((char[]) instance.encode((Object) bits));
527 assertEquals("0000011111111111", encoded);
528 bits = new byte[2];
529 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
530 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
531 encoded = new String((char[]) instance.encode((Object) bits));
532 assertEquals("0000111111111111", encoded);
533 bits = new byte[2];
534 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
535 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
536 encoded = new String((char[]) instance.encode((Object) bits));
537 assertEquals("0001111111111111", encoded);
538 bits = new byte[2];
539 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
540 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
541 encoded = new String((char[]) instance.encode((Object) bits));
542 assertEquals("0011111111111111", encoded);
543 bits = new byte[2];
544 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
545 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
546 encoded = new String((char[]) instance.encode((Object) bits));
547 assertEquals("0111111111111111", encoded);
548 bits = new byte[2];
549 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
550 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
551 encoded = new String((char[]) instance.encode((Object) bits));
552 assertEquals("1111111111111111", encoded);
553 }
554
555
556
557
558 @Test
559 void testEncodeObjectException() {
560 assertThrows(EncoderException.class, () -> instance.encode(""));
561 }
562
563
564
565
566 @Test
567 void testEncodeObjectNull() throws Exception {
568 final Object obj = new byte[0];
569 assertEquals(0, ((char[]) instance.encode(obj)).length);
570 }
571
572
573
574
575 @Test
576 void testFromAsciiByteArray() {
577 assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
578 assertEquals(0, BinaryCodec.fromAscii(new byte[0]).length);
579 assertArrayEquals(new byte[0], BinaryCodec.fromAscii("1".getBytes(CHARSET_UTF8)));
580 assertArrayEquals(new byte[] { 0 }, BinaryCodec.fromAscii("100000000".getBytes(CHARSET_UTF8)));
581 assertArrayEquals(new byte[] { (byte) 0x80 }, BinaryCodec.fromAscii("010000000".getBytes(CHARSET_UTF8)));
582
583 byte[] bits = new byte[1];
584 byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes(CHARSET_UTF8));
585 assertEquals(new String(bits), new String(decoded));
586 bits = new byte[1];
587 bits[0] = BIT_0;
588 decoded = BinaryCodec.fromAscii("00000001".getBytes(CHARSET_UTF8));
589 assertEquals(new String(bits), new String(decoded));
590 bits = new byte[1];
591 bits[0] = BIT_0 | BIT_1;
592 decoded = BinaryCodec.fromAscii("00000011".getBytes(CHARSET_UTF8));
593 assertEquals(new String(bits), new String(decoded));
594 bits = new byte[1];
595 bits[0] = BIT_0 | BIT_1 | BIT_2;
596 decoded = BinaryCodec.fromAscii("00000111".getBytes(CHARSET_UTF8));
597 assertEquals(new String(bits), new String(decoded));
598 bits = new byte[1];
599 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
600 decoded = BinaryCodec.fromAscii("00001111".getBytes(CHARSET_UTF8));
601 assertEquals(new String(bits), new String(decoded));
602 bits = new byte[1];
603 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
604 decoded = BinaryCodec.fromAscii("00011111".getBytes(CHARSET_UTF8));
605 assertEquals(new String(bits), new String(decoded));
606 bits = new byte[1];
607 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
608 decoded = BinaryCodec.fromAscii("00111111".getBytes(CHARSET_UTF8));
609 assertEquals(new String(bits), new String(decoded));
610 bits = new byte[1];
611 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
612 decoded = BinaryCodec.fromAscii("01111111".getBytes(CHARSET_UTF8));
613 assertEquals(new String(bits), new String(decoded));
614 bits = new byte[1];
615 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
616 decoded = BinaryCodec.fromAscii("11111111".getBytes(CHARSET_UTF8));
617 assertEquals(new String(bits), new String(decoded));
618
619 bits = new byte[2];
620 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
621 decoded = BinaryCodec.fromAscii("0000000011111111".getBytes(CHARSET_UTF8));
622 assertEquals(new String(bits), new String(decoded));
623 bits = new byte[2];
624 bits[1] = BIT_0;
625 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
626 decoded = BinaryCodec.fromAscii("0000000111111111".getBytes(CHARSET_UTF8));
627 assertEquals(new String(bits), new String(decoded));
628 bits = new byte[2];
629 bits[1] = BIT_0 | BIT_1;
630 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
631 decoded = BinaryCodec.fromAscii("0000001111111111".getBytes(CHARSET_UTF8));
632 assertEquals(new String(bits), new String(decoded));
633 bits = new byte[2];
634 bits[1] = BIT_0 | BIT_1 | BIT_2;
635 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
636 decoded = BinaryCodec.fromAscii("0000011111111111".getBytes(CHARSET_UTF8));
637 assertEquals(new String(bits), new String(decoded));
638 bits = new byte[2];
639 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
640 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
641 decoded = BinaryCodec.fromAscii("0000111111111111".getBytes(CHARSET_UTF8));
642 assertEquals(new String(bits), new String(decoded));
643 bits = new byte[2];
644 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
645 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
646 decoded = BinaryCodec.fromAscii("0001111111111111".getBytes(CHARSET_UTF8));
647 assertEquals(new String(bits), new String(decoded));
648 bits = new byte[2];
649 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
650 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
651 decoded = BinaryCodec.fromAscii("0011111111111111".getBytes(CHARSET_UTF8));
652 assertEquals(new String(bits), new String(decoded));
653 bits = new byte[2];
654 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
655 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
656 decoded = BinaryCodec.fromAscii("0111111111111111".getBytes(CHARSET_UTF8));
657 assertEquals(new String(bits), new String(decoded));
658 bits = new byte[2];
659 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
660 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
661 decoded = BinaryCodec.fromAscii("1111111111111111".getBytes(CHARSET_UTF8));
662 assertEquals(new String(bits), new String(decoded));
663 assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length);
664 }
665
666
667
668
669 @Test
670 void testFromAsciiCharArray() {
671 assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
672 assertEquals(0, BinaryCodec.fromAscii(new char[0]).length);
673 assertArrayEquals(new byte[0], BinaryCodec.fromAscii("1".toCharArray()));
674 assertArrayEquals(new byte[] { 0 }, BinaryCodec.fromAscii("100000000".toCharArray()));
675 assertArrayEquals(new byte[] { (byte) 0x80 }, BinaryCodec.fromAscii("010000000".toCharArray()));
676
677 byte[] bits = new byte[1];
678 byte[] decoded = BinaryCodec.fromAscii("00000000".toCharArray());
679 assertEquals(new String(bits), new String(decoded));
680 bits = new byte[1];
681 bits[0] = BIT_0;
682 decoded = BinaryCodec.fromAscii("00000001".toCharArray());
683 assertEquals(new String(bits), new String(decoded));
684 bits = new byte[1];
685 bits[0] = BIT_0 | BIT_1;
686 decoded = BinaryCodec.fromAscii("00000011".toCharArray());
687 assertEquals(new String(bits), new String(decoded));
688 bits = new byte[1];
689 bits[0] = BIT_0 | BIT_1 | BIT_2;
690 decoded = BinaryCodec.fromAscii("00000111".toCharArray());
691 assertEquals(new String(bits), new String(decoded));
692 bits = new byte[1];
693 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
694 decoded = BinaryCodec.fromAscii("00001111".toCharArray());
695 assertEquals(new String(bits), new String(decoded));
696 bits = new byte[1];
697 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
698 decoded = BinaryCodec.fromAscii("00011111".toCharArray());
699 assertEquals(new String(bits), new String(decoded));
700 bits = new byte[1];
701 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
702 decoded = BinaryCodec.fromAscii("00111111".toCharArray());
703 assertEquals(new String(bits), new String(decoded));
704 bits = new byte[1];
705 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
706 decoded = BinaryCodec.fromAscii("01111111".toCharArray());
707 assertEquals(new String(bits), new String(decoded));
708 bits = new byte[1];
709 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
710 decoded = BinaryCodec.fromAscii("11111111".toCharArray());
711 assertEquals(new String(bits), new String(decoded));
712
713 bits = new byte[2];
714 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
715 decoded = BinaryCodec.fromAscii("0000000011111111".toCharArray());
716 assertEquals(new String(bits), new String(decoded));
717 bits = new byte[2];
718 bits[1] = BIT_0;
719 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
720 decoded = BinaryCodec.fromAscii("0000000111111111".toCharArray());
721 assertEquals(new String(bits), new String(decoded));
722 bits = new byte[2];
723 bits[1] = BIT_0 | BIT_1;
724 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
725 decoded = BinaryCodec.fromAscii("0000001111111111".toCharArray());
726 assertEquals(new String(bits), new String(decoded));
727 bits = new byte[2];
728 bits[1] = BIT_0 | BIT_1 | BIT_2;
729 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
730 decoded = BinaryCodec.fromAscii("0000011111111111".toCharArray());
731 assertEquals(new String(bits), new String(decoded));
732 bits = new byte[2];
733 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
734 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
735 decoded = BinaryCodec.fromAscii("0000111111111111".toCharArray());
736 assertEquals(new String(bits), new String(decoded));
737 bits = new byte[2];
738 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
739 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
740 decoded = BinaryCodec.fromAscii("0001111111111111".toCharArray());
741 assertEquals(new String(bits), new String(decoded));
742 bits = new byte[2];
743 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
744 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
745 decoded = BinaryCodec.fromAscii("0011111111111111".toCharArray());
746 assertEquals(new String(bits), new String(decoded));
747 bits = new byte[2];
748 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
749 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
750 decoded = BinaryCodec.fromAscii("0111111111111111".toCharArray());
751 assertEquals(new String(bits), new String(decoded));
752 bits = new byte[2];
753 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
754 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
755 decoded = BinaryCodec.fromAscii("1111111111111111".toCharArray());
756 assertEquals(new String(bits), new String(decoded));
757 assertEquals(0, BinaryCodec.fromAscii((char[]) null).length);
758 }
759
760 @Test
761 void testToAsciiBytes() {
762
763 byte[] bits = new byte[1];
764 String encoded = new String(BinaryCodec.toAsciiBytes(bits));
765 assertEquals("00000000", encoded);
766 bits = new byte[1];
767 bits[0] = BIT_0;
768 encoded = new String(BinaryCodec.toAsciiBytes(bits));
769 assertEquals("00000001", encoded);
770 bits = new byte[1];
771 bits[0] = BIT_0 | BIT_1;
772 encoded = new String(BinaryCodec.toAsciiBytes(bits));
773 assertEquals("00000011", encoded);
774 bits = new byte[1];
775 bits[0] = BIT_0 | BIT_1 | BIT_2;
776 encoded = new String(BinaryCodec.toAsciiBytes(bits));
777 assertEquals("00000111", encoded);
778 bits = new byte[1];
779 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
780 encoded = new String(BinaryCodec.toAsciiBytes(bits));
781 assertEquals("00001111", encoded);
782 bits = new byte[1];
783 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
784 encoded = new String(BinaryCodec.toAsciiBytes(bits));
785 assertEquals("00011111", encoded);
786 bits = new byte[1];
787 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
788 encoded = new String(BinaryCodec.toAsciiBytes(bits));
789 assertEquals("00111111", encoded);
790 bits = new byte[1];
791 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
792 encoded = new String(BinaryCodec.toAsciiBytes(bits));
793 assertEquals("01111111", encoded);
794 bits = new byte[1];
795 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
796 encoded = new String(BinaryCodec.toAsciiBytes(bits));
797 assertEquals("11111111", encoded);
798
799 bits = new byte[2];
800 encoded = new String(BinaryCodec.toAsciiBytes(bits));
801 assertEquals("0000000000000000", encoded);
802 bits = new byte[2];
803 bits[0] = BIT_0;
804 encoded = new String(BinaryCodec.toAsciiBytes(bits));
805 assertEquals("0000000000000001", encoded);
806 bits = new byte[2];
807 bits[0] = BIT_0 | BIT_1;
808 encoded = new String(BinaryCodec.toAsciiBytes(bits));
809 assertEquals("0000000000000011", encoded);
810 bits = new byte[2];
811 bits[0] = BIT_0 | BIT_1 | BIT_2;
812 encoded = new String(BinaryCodec.toAsciiBytes(bits));
813 assertEquals("0000000000000111", encoded);
814 bits = new byte[2];
815 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
816 encoded = new String(BinaryCodec.toAsciiBytes(bits));
817 assertEquals("0000000000001111", encoded);
818 bits = new byte[2];
819 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
820 encoded = new String(BinaryCodec.toAsciiBytes(bits));
821 assertEquals("0000000000011111", encoded);
822 bits = new byte[2];
823 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
824 encoded = new String(BinaryCodec.toAsciiBytes(bits));
825 assertEquals("0000000000111111", encoded);
826 bits = new byte[2];
827 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
828 encoded = new String(BinaryCodec.toAsciiBytes(bits));
829 assertEquals("0000000001111111", encoded);
830 bits = new byte[2];
831 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
832 encoded = new String(BinaryCodec.toAsciiBytes(bits));
833 assertEquals("0000000011111111", encoded);
834
835 bits = new byte[2];
836 bits[1] = BIT_0;
837 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
838 encoded = new String(BinaryCodec.toAsciiBytes(bits));
839 assertEquals("0000000111111111", encoded);
840 bits = new byte[2];
841 bits[1] = BIT_0 | BIT_1;
842 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
843 encoded = new String(BinaryCodec.toAsciiBytes(bits));
844 assertEquals("0000001111111111", encoded);
845 bits = new byte[2];
846 bits[1] = BIT_0 | BIT_1 | BIT_2;
847 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
848 encoded = new String(BinaryCodec.toAsciiBytes(bits));
849 assertEquals("0000011111111111", encoded);
850 bits = new byte[2];
851 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
852 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
853 encoded = new String(BinaryCodec.toAsciiBytes(bits));
854 assertEquals("0000111111111111", encoded);
855 bits = new byte[2];
856 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
857 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
858 encoded = new String(BinaryCodec.toAsciiBytes(bits));
859 assertEquals("0001111111111111", encoded);
860 bits = new byte[2];
861 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
862 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
863 encoded = new String(BinaryCodec.toAsciiBytes(bits));
864 assertEquals("0011111111111111", encoded);
865 bits = new byte[2];
866 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
867 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
868 encoded = new String(BinaryCodec.toAsciiBytes(bits));
869 assertEquals("0111111111111111", encoded);
870 bits = new byte[2];
871 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
872 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
873 encoded = new String(BinaryCodec.toAsciiBytes(bits));
874 assertEquals("1111111111111111", encoded);
875 assertEquals(0, BinaryCodec.toAsciiBytes((byte[]) null).length);
876 }
877
878 @Test
879 void testToAsciiChars() {
880
881 byte[] bits = new byte[1];
882 String encoded = new String(BinaryCodec.toAsciiChars(bits));
883 assertEquals("00000000", encoded);
884 bits = new byte[1];
885 bits[0] = BIT_0;
886 encoded = new String(BinaryCodec.toAsciiChars(bits));
887 assertEquals("00000001", encoded);
888 bits = new byte[1];
889 bits[0] = BIT_0 | BIT_1;
890 encoded = new String(BinaryCodec.toAsciiChars(bits));
891 assertEquals("00000011", encoded);
892 bits = new byte[1];
893 bits[0] = BIT_0 | BIT_1 | BIT_2;
894 encoded = new String(BinaryCodec.toAsciiChars(bits));
895 assertEquals("00000111", encoded);
896 bits = new byte[1];
897 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
898 encoded = new String(BinaryCodec.toAsciiChars(bits));
899 assertEquals("00001111", encoded);
900 bits = new byte[1];
901 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
902 encoded = new String(BinaryCodec.toAsciiChars(bits));
903 assertEquals("00011111", encoded);
904 bits = new byte[1];
905 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
906 encoded = new String(BinaryCodec.toAsciiChars(bits));
907 assertEquals("00111111", encoded);
908 bits = new byte[1];
909 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
910 encoded = new String(BinaryCodec.toAsciiChars(bits));
911 assertEquals("01111111", encoded);
912 bits = new byte[1];
913 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
914 encoded = new String(BinaryCodec.toAsciiChars(bits));
915 assertEquals("11111111", encoded);
916
917 bits = new byte[2];
918 encoded = new String(BinaryCodec.toAsciiChars(bits));
919 assertEquals("0000000000000000", encoded);
920 bits = new byte[2];
921 bits[0] = BIT_0;
922 encoded = new String(BinaryCodec.toAsciiChars(bits));
923 assertEquals("0000000000000001", encoded);
924 bits = new byte[2];
925 bits[0] = BIT_0 | BIT_1;
926 encoded = new String(BinaryCodec.toAsciiChars(bits));
927 assertEquals("0000000000000011", encoded);
928 bits = new byte[2];
929 bits[0] = BIT_0 | BIT_1 | BIT_2;
930 encoded = new String(BinaryCodec.toAsciiChars(bits));
931 assertEquals("0000000000000111", encoded);
932 bits = new byte[2];
933 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
934 encoded = new String(BinaryCodec.toAsciiChars(bits));
935 assertEquals("0000000000001111", encoded);
936 bits = new byte[2];
937 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
938 encoded = new String(BinaryCodec.toAsciiChars(bits));
939 assertEquals("0000000000011111", encoded);
940 bits = new byte[2];
941 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
942 encoded = new String(BinaryCodec.toAsciiChars(bits));
943 assertEquals("0000000000111111", encoded);
944 bits = new byte[2];
945 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
946 encoded = new String(BinaryCodec.toAsciiChars(bits));
947 assertEquals("0000000001111111", encoded);
948 bits = new byte[2];
949 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
950 encoded = new String(BinaryCodec.toAsciiChars(bits));
951 assertEquals("0000000011111111", encoded);
952
953 bits = new byte[2];
954 bits[1] = BIT_0;
955 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
956 encoded = new String(BinaryCodec.toAsciiChars(bits));
957 assertEquals("0000000111111111", encoded);
958 bits = new byte[2];
959 bits[1] = BIT_0 | BIT_1;
960 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
961 encoded = new String(BinaryCodec.toAsciiChars(bits));
962 assertEquals("0000001111111111", encoded);
963 bits = new byte[2];
964 bits[1] = BIT_0 | BIT_1 | BIT_2;
965 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
966 encoded = new String(BinaryCodec.toAsciiChars(bits));
967 assertEquals("0000011111111111", encoded);
968 bits = new byte[2];
969 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
970 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
971 encoded = new String(BinaryCodec.toAsciiChars(bits));
972 assertEquals("0000111111111111", encoded);
973 bits = new byte[2];
974 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
975 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
976 encoded = new String(BinaryCodec.toAsciiChars(bits));
977 assertEquals("0001111111111111", encoded);
978 bits = new byte[2];
979 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
980 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
981 encoded = new String(BinaryCodec.toAsciiChars(bits));
982 assertEquals("0011111111111111", encoded);
983 bits = new byte[2];
984 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
985 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
986 encoded = new String(BinaryCodec.toAsciiChars(bits));
987 assertEquals("0111111111111111", encoded);
988 bits = new byte[2];
989 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
990 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
991 encoded = new String(BinaryCodec.toAsciiChars(bits));
992 assertEquals("1111111111111111", encoded);
993 assertEquals(0, BinaryCodec.toAsciiChars((byte[]) null).length);
994 }
995
996
997
998
999 @Test
1000 void testToAsciiString() {
1001
1002 byte[] bits = new byte[1];
1003 String encoded = BinaryCodec.toAsciiString(bits);
1004 assertEquals("00000000", encoded);
1005 bits = new byte[1];
1006 bits[0] = BIT_0;
1007 encoded = BinaryCodec.toAsciiString(bits);
1008 assertEquals("00000001", encoded);
1009 bits = new byte[1];
1010 bits[0] = BIT_0 | BIT_1;
1011 encoded = BinaryCodec.toAsciiString(bits);
1012 assertEquals("00000011", encoded);
1013 bits = new byte[1];
1014 bits[0] = BIT_0 | BIT_1 | BIT_2;
1015 encoded = BinaryCodec.toAsciiString(bits);
1016 assertEquals("00000111", encoded);
1017 bits = new byte[1];
1018 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1019 encoded = BinaryCodec.toAsciiString(bits);
1020 assertEquals("00001111", encoded);
1021 bits = new byte[1];
1022 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1023 encoded = BinaryCodec.toAsciiString(bits);
1024 assertEquals("00011111", encoded);
1025 bits = new byte[1];
1026 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1027 encoded = BinaryCodec.toAsciiString(bits);
1028 assertEquals("00111111", encoded);
1029 bits = new byte[1];
1030 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1031 encoded = BinaryCodec.toAsciiString(bits);
1032 assertEquals("01111111", encoded);
1033 bits = new byte[1];
1034 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1035 encoded = BinaryCodec.toAsciiString(bits);
1036 assertEquals("11111111", encoded);
1037
1038 bits = new byte[2];
1039 encoded = BinaryCodec.toAsciiString(bits);
1040 assertEquals("0000000000000000", encoded);
1041 bits = new byte[2];
1042 bits[0] = BIT_0;
1043 encoded = BinaryCodec.toAsciiString(bits);
1044 assertEquals("0000000000000001", encoded);
1045 bits = new byte[2];
1046 bits[0] = BIT_0 | BIT_1;
1047 encoded = BinaryCodec.toAsciiString(bits);
1048 assertEquals("0000000000000011", encoded);
1049 bits = new byte[2];
1050 bits[0] = BIT_0 | BIT_1 | BIT_2;
1051 encoded = BinaryCodec.toAsciiString(bits);
1052 assertEquals("0000000000000111", encoded);
1053 bits = new byte[2];
1054 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1055 encoded = BinaryCodec.toAsciiString(bits);
1056 assertEquals("0000000000001111", encoded);
1057 bits = new byte[2];
1058 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1059 encoded = BinaryCodec.toAsciiString(bits);
1060 assertEquals("0000000000011111", encoded);
1061 bits = new byte[2];
1062 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1063 encoded = BinaryCodec.toAsciiString(bits);
1064 assertEquals("0000000000111111", encoded);
1065 bits = new byte[2];
1066 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1067 encoded = BinaryCodec.toAsciiString(bits);
1068 assertEquals("0000000001111111", encoded);
1069 bits = new byte[2];
1070 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1071 encoded = BinaryCodec.toAsciiString(bits);
1072 assertEquals("0000000011111111", encoded);
1073
1074 bits = new byte[2];
1075 bits[1] = BIT_0;
1076 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1077 encoded = BinaryCodec.toAsciiString(bits);
1078 assertEquals("0000000111111111", encoded);
1079 bits = new byte[2];
1080 bits[1] = BIT_0 | BIT_1;
1081 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1082 encoded = BinaryCodec.toAsciiString(bits);
1083 assertEquals("0000001111111111", encoded);
1084 bits = new byte[2];
1085 bits[1] = BIT_0 | BIT_1 | BIT_2;
1086 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1087 encoded = BinaryCodec.toAsciiString(bits);
1088 assertEquals("0000011111111111", encoded);
1089 bits = new byte[2];
1090 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1091 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1092 encoded = BinaryCodec.toAsciiString(bits);
1093 assertEquals("0000111111111111", encoded);
1094 bits = new byte[2];
1095 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1096 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1097 encoded = BinaryCodec.toAsciiString(bits);
1098 assertEquals("0001111111111111", encoded);
1099 bits = new byte[2];
1100 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1101 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1102 encoded = BinaryCodec.toAsciiString(bits);
1103 assertEquals("0011111111111111", encoded);
1104 bits = new byte[2];
1105 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1106 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1107 encoded = BinaryCodec.toAsciiString(bits);
1108 assertEquals("0111111111111111", encoded);
1109 bits = new byte[2];
1110 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1111 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1112 encoded = BinaryCodec.toAsciiString(bits);
1113 assertEquals("1111111111111111", encoded);
1114 assertEquals("", BinaryCodec.toAsciiString(null));
1115 }
1116
1117
1118
1119
1120 @Test
1121 void testToByteArrayFromString() {
1122
1123 byte[] bits = new byte[1];
1124 byte[] decoded = instance.toByteArray("00000000");
1125 assertEquals(new String(bits), new String(decoded));
1126 bits = new byte[1];
1127 bits[0] = BIT_0;
1128 decoded = instance.toByteArray("00000001");
1129 assertEquals(new String(bits), new String(decoded));
1130 bits = new byte[1];
1131 bits[0] = BIT_0 | BIT_1;
1132 decoded = instance.toByteArray("00000011");
1133 assertEquals(new String(bits), new String(decoded));
1134 bits = new byte[1];
1135 bits[0] = BIT_0 | BIT_1 | BIT_2;
1136 decoded = instance.toByteArray("00000111");
1137 assertEquals(new String(bits), new String(decoded));
1138 bits = new byte[1];
1139 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1140 decoded = instance.toByteArray("00001111");
1141 assertEquals(new String(bits), new String(decoded));
1142 bits = new byte[1];
1143 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1144 decoded = instance.toByteArray("00011111");
1145 assertEquals(new String(bits), new String(decoded));
1146 bits = new byte[1];
1147 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1148 decoded = instance.toByteArray("00111111");
1149 assertEquals(new String(bits), new String(decoded));
1150 bits = new byte[1];
1151 bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1152 decoded = instance.toByteArray("01111111");
1153 assertEquals(new String(bits), new String(decoded));
1154 bits = new byte[1];
1155 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1156 decoded = instance.toByteArray("11111111");
1157 assertEquals(new String(bits), new String(decoded));
1158
1159 bits = new byte[2];
1160 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1161 decoded = instance.toByteArray("0000000011111111");
1162 assertEquals(new String(bits), new String(decoded));
1163 bits = new byte[2];
1164 bits[1] = BIT_0;
1165 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1166 decoded = instance.toByteArray("0000000111111111");
1167 assertEquals(new String(bits), new String(decoded));
1168 bits = new byte[2];
1169 bits[1] = BIT_0 | BIT_1;
1170 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1171 decoded = instance.toByteArray("0000001111111111");
1172 assertEquals(new String(bits), new String(decoded));
1173 bits = new byte[2];
1174 bits[1] = BIT_0 | BIT_1 | BIT_2;
1175 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1176 decoded = instance.toByteArray("0000011111111111");
1177 assertEquals(new String(bits), new String(decoded));
1178 bits = new byte[2];
1179 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3;
1180 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1181 decoded = instance.toByteArray("0000111111111111");
1182 assertEquals(new String(bits), new String(decoded));
1183 bits = new byte[2];
1184 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4;
1185 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1186 decoded = instance.toByteArray("0001111111111111");
1187 assertEquals(new String(bits), new String(decoded));
1188 bits = new byte[2];
1189 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5;
1190 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1191 decoded = instance.toByteArray("0011111111111111");
1192 assertEquals(new String(bits), new String(decoded));
1193 bits = new byte[2];
1194 bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6;
1195 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1196 decoded = instance.toByteArray("0111111111111111");
1197 assertEquals(new String(bits), new String(decoded));
1198 bits = new byte[2];
1199 bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1200 bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7);
1201 decoded = instance.toByteArray("1111111111111111");
1202 assertEquals(new String(bits), new String(decoded));
1203 assertEquals(0, instance.toByteArray((String) null).length);
1204 }
1205
1206 }