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