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