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