| 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;
|