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