1 4 package com.tc.io; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.bytes.TCByteBufferFactory; 8 import com.tc.test.TCTestCase; 9 10 import java.io.IOException ; 11 import java.security.SecureRandom ; 12 import java.util.Arrays ; 13 import java.util.Date ; 14 import java.util.Random ; 15 16 public class TCByteBufferInputStreamTest extends TCTestCase { 17 18 private Random random = new SecureRandom (); 19 20 public void testExceptions() { 21 try { 22 new TCByteBufferInputStream((TCByteBuffer) null); 23 fail(); 24 } catch (NullPointerException npe) { 25 } 27 28 try { 29 new TCByteBufferInputStream((TCByteBuffer[]) null); 30 fail(); 31 } catch (NullPointerException npe) { 32 } 34 35 try { 36 new TCByteBufferInputStream(new TCByteBuffer[] { TCByteBufferFactory.getInstance(false, 5), null }); 37 fail(); 38 } catch (NullPointerException npe) { 39 } 41 42 try { 43 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 44 bbis.read(null); 45 fail(); 46 } catch (NullPointerException npe) { 47 } 49 50 try { 51 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 52 bbis.read(null, 1, 10); 53 fail(); 54 } catch (NullPointerException npe) { 55 } 57 58 try { 59 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 60 bbis.read(new byte[10], 10, 1); 61 fail(); 62 } catch (IndexOutOfBoundsException ioobe) { 63 } 65 66 try { 67 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 68 bbis.read(new byte[10], -1, 1); 69 fail(); 70 } catch (IndexOutOfBoundsException ioobe) { 71 } 73 74 try { 75 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 76 bbis.read(new byte[10], 1, -1); 77 fail(); 78 } catch (IndexOutOfBoundsException ioobe) { 79 } 81 82 TCByteBufferInputStream bbis = new TCByteBufferInputStream(TCByteBufferFactory.getInstance(false, 10)); 83 for (int i = 0; i < 10; i++) { 84 bbis.close(); 85 } 86 87 try { 88 bbis.read(); 89 fail(); 90 } catch (IllegalStateException ise) { 91 } 93 94 try { 95 bbis.read(new byte[1]); 96 fail(); 97 } catch (IllegalStateException ise) { 98 } 100 101 try { 102 bbis.read(new byte[1], 0, 1); 103 fail(); 104 } catch (IllegalStateException ise) { 105 } 107 } 108 109 public void testToArray() { 110 for (int i = 0; i < 250; i++) { 111 log("testToArray(" + i + ")"); 112 TCByteBuffer[] data = getRandomDataNonZeroLength(); 113 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 114 115 int read = random.nextInt(bbis.available()); 116 for (int r = 0; r < read; r++) { 117 bbis.read(); 118 } 119 120 TCByteBufferInputStream compare = new TCByteBufferInputStream(bbis.toArray()); 121 while (compare.available() > 0) { 122 int orig = bbis.read(); 123 int comp = compare.read(); 124 assertEquals(orig, comp); 125 } 126 127 assertEquals(0, compare.available()); 128 assertEquals(0, bbis.available()); 129 } 130 } 131 132 public void testLimit() { 133 for (int i = 0; i < 250; i++) { 134 log("testLimit(" + i + ")"); 135 TCByteBuffer[] data = getRandomDataNonZeroLength(); 136 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 137 138 int read = random.nextInt(bbis.available()); 139 for (int r = 0; r < read; r++) { 140 bbis.read(); 141 } 142 143 int which = random.nextInt(3); 144 switch (which) { 145 case 0: { 146 bbis.limit(0); 147 assertEquals(0, bbis.available()); 148 break; 149 } 150 case 1: { 151 int before = bbis.available(); 152 bbis.limit(bbis.available()); 153 assertEquals(before, bbis.available()); 154 break; 155 } 156 case 2: { 157 bbis.limit(random.nextInt(bbis.available())); 158 break; 159 } 160 default: { 161 throw new RuntimeException ("" + which); 162 } 163 } 164 165 TCByteBufferInputStream compare = new TCByteBufferInputStream(bbis.toArray()); 166 while (compare.available() > 0) { 167 int orig = bbis.read(); 168 int comp = compare.read(); 169 assertEquals(orig, comp); 170 } 171 172 assertEquals(0, compare.available()); 173 assertEquals(0, bbis.available()); 174 } 175 } 176 177 public void testDuplicateAndLimitZeroLen() { 178 TCByteBufferInputStream bbis = new TCByteBufferInputStream(new TCByteBuffer[] {}); 179 180 assertEquals(0, bbis.available()); 181 assertEquals(0, bbis.duplicateAndLimit(0).available()); 182 } 183 184 public void testDuplicateAndLimit() { 185 for (int i = 0; i < 50; i++) { 186 log("testDuplicateAndLimit(" + i + ")"); 187 TCByteBuffer[] data = getRandomDataNonZeroLength(); 188 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 189 190 int length = bbis.available(); 191 assertTrue(length > 0); 192 int start = random.nextInt(length); 193 bbis.skip(start); 194 int limit = random.nextInt(bbis.available()); 195 196 TCByteBufferInputStream dupe = bbis.duplicateAndLimit(limit); 197 for (int n = 0; n < limit; n++) { 198 int dupeByte = dupe.read(); 199 int origByte = bbis.read(); 200 201 assertTrue(dupeByte != -1); 202 assertTrue(origByte != -1); 203 204 assertEquals(origByte, dupeByte); 205 } 206 207 assertEquals(0, dupe.available()); 208 } 209 } 210 211 public void testDuplicate() { 212 for (int i = 0; i < 250; i++) { 213 log("testDuplicate(" + i + ")"); 214 TCByteBuffer[] data = getRandomData(); 215 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 216 bbis.read(); 217 TCByteBufferInputStream dupe = bbis.duplicate(); 218 assertEquals(bbis.available(), dupe.available()); 219 int read = bbis.read(); 220 if (read != -1) { 221 assertEquals(dupe.available() - 1, bbis.available()); 223 int dupeRead = dupe.read(); 224 assertEquals(read, dupeRead); 225 } 226 227 bbis = new TCByteBufferInputStream(getRandomDataNonZeroLength()); 228 int dupeStart = random.nextInt(bbis.getTotalLength()); 229 for (int n = 0; n < dupeStart; n++) { 230 int b = bbis.read(); 231 assertTrue(b >= 0); 232 } 233 dupe = bbis.duplicate(); 234 while (bbis.available() > 0) { 235 int n1 = bbis.read(); 236 int n2 = dupe.read(); 237 assertEquals(n1, n2); 238 } 239 assertEquals(0, dupe.available()); 240 assertEquals(0, bbis.available()); 241 } 242 } 243 244 public void testOffsetReadArray() { 245 for (int i = 0; i < 25; i++) { 246 log("testOffsetReadArray(" + i + ")"); 247 testOffsetReadArray(getRandomData()); 248 } 249 } 250 251 private void testOffsetReadArray(TCByteBuffer[] data) { 252 final int numBufs = data.length == 0 ? 0 : data.length - 1; 253 254 reportLengths(data); 255 final long totalLength = length(data); 256 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 257 assertEquals(totalLength, bbis.available()); 258 assertEquals(totalLength, bbis.getTotalLength()); 259 260 int index = 0; 261 int bytesRead = 0; 262 while (bbis.available() > 0) { 263 byte[] buffer = new byte[random.nextInt(50) + 1]; 264 byte fill = (byte) random.nextInt(); 265 Arrays.fill(buffer, fill); 266 267 final int offset = random.nextInt(buffer.length); 268 final int length = random.nextInt(buffer.length - offset); 269 final int read = bbis.read(buffer, offset, length); 270 if (read == -1) { 271 break; 272 } 273 bytesRead += read; 274 275 for (int i = 0; i < offset; i++) { 276 assertEquals(fill, buffer[i]); 277 } 278 279 for (int i = offset + length + 1; i < buffer.length; i++) { 280 assertEquals(fill, buffer[i]); 281 } 282 283 for (int i = 0; i < read; i++) { 284 for (; !data[index].hasRemaining() && index < numBufs; index++) { 285 } 287 288 assertEquals(data[index].get(), buffer[offset + i]); 289 } 290 } 291 292 if (index < numBufs) { 293 for (; index < numBufs; index++) { 294 assertEquals(0, data[index + 1].limit()); 295 } 296 } 297 298 assertEquals(index, numBufs); 299 if (numBufs > 0) { 300 assertEquals(0, data[numBufs].remaining()); 301 } 302 303 assertEquals(bytesRead, totalLength); 304 assertEquals(0, bbis.available()); 305 assertEquals(-1, bbis.read()); 306 assertEquals(-1, bbis.read(new byte[10])); 307 assertEquals(-1, bbis.read(new byte[10], 0, 3)); 308 } 309 310 public void testReadBasics() { 311 for (int i = 0; i < 250; i++) { 312 log("testReadBasics(" + i + ")"); 313 testReadBasics(getRandomData()); 314 } 315 } 316 317 public void testBasic() { 318 for (int i = 0; i < 250; i++) { 319 log("testBasic(" + i + ")"); 320 TCByteBuffer[] data = getRandomDataNonZeroLength(); 321 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 322 323 assertTrue(bbis.available() > 0); 324 325 final byte b = data[0].get(0); 326 int read = bbis.read(); 327 assertEquals(b, (byte) read); 328 329 byte[] readArray = new byte[1]; 330 bbis = new TCByteBufferInputStream(data); 331 bbis.read(readArray); 332 assertEquals(b, readArray[0]); 333 334 bbis = new TCByteBufferInputStream(data); 335 bbis.read(readArray, 0, 1); 336 assertEquals(b, readArray[0]); 337 338 bbis = new TCByteBufferInputStream(data); 339 int avail = bbis.available(); 340 bbis.read(new byte[0]); 341 assertEquals(avail, bbis.available()); 342 bbis.read(new byte[0], 0, 0); 343 assertEquals(avail, bbis.available()); 344 bbis.read(new byte[10], 0, 0); 345 assertEquals(avail, bbis.available()); 346 } 347 } 348 349 public void testMarkReset() throws IOException { 350 TCByteBuffer[] data = createBuffersWithRandomData(4, 10); 351 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 352 353 bbis.readInt(); 354 bbis.readInt(); 355 bbis.readInt(); 357 try { 358 bbis.tcReset(); 359 fail(); 360 } catch (IllegalStateException ise) { 361 } 363 364 int avail = bbis.available(); 365 bbis.mark(); 366 367 int i1 = bbis.readInt(); 368 int i2 = bbis.readInt(); 369 int i3 = bbis.readInt(); 371 bbis.tcReset(); 372 373 assertEquals(avail, bbis.available()); 374 assertEquals(i1, bbis.readInt()); 375 assertEquals(i2, bbis.readInt()); 376 assertEquals(i3, bbis.readInt()); 377 378 try { 379 bbis.tcReset(); 380 fail(); 381 } catch (IllegalStateException ise) { 382 } 384 } 385 386 public void testMarkReset2() throws IOException { 387 TCByteBuffer[] data = createBuffersWithRandomData(1, 10); 389 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 390 391 bbis.readInt(); 392 int avail = bbis.available(); 393 bbis.mark(); 394 395 int i1 = bbis.readInt(); 396 397 bbis.tcReset(); 398 399 assertEquals(avail, bbis.available()); 400 assertEquals(i1, bbis.readInt()); 401 } 402 403 private void testReadBasics(TCByteBuffer[] data) { 404 final int numBufs = data.length == 0 ? 0 : data.length - 1; 405 406 reportLengths(data); 407 final long len = length(data); 408 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 409 assertEquals(len, bbis.available()); 410 assertEquals(len, bbis.getTotalLength()); 411 412 int index = 0; 413 for (int i = 0; i < len; i++) { 414 for (; !data[index].hasRemaining() && index < numBufs; index++) { 415 } 417 418 assertEquals(len - i, bbis.available()); 419 int fromStream = bbis.read(); 420 assertFalse(fromStream < 0); 421 422 byte original = data[index].get(); 423 424 assertEquals(original, (byte) fromStream); 425 } 426 427 if (index < numBufs) { 428 for (; index < numBufs; index++) { 429 assertEquals(0, data[index + 1].limit()); 430 } 431 } 432 433 assertEquals(index, numBufs); 434 if (numBufs > 0) { 435 assertEquals(0, data[numBufs].remaining()); 436 } 437 438 assertEquals(0, bbis.available()); 439 assertEquals(-1, bbis.read()); 440 assertEquals(-1, bbis.read(new byte[10])); 441 assertEquals(-1, bbis.read(new byte[10], 0, 3)); 442 } 443 444 private void reportLengths(TCByteBuffer[] data) { 445 if (true) return; 447 448 System.err.print(data.length + " buffers with lengths: "); 449 for (int i = 0; i < data.length; i++) { 450 System.err.print(data[i].limit() + " "); 451 } 452 System.err.println(); 453 } 454 455 public void testTrailingZeroLength() { 456 TCByteBuffer[] data = getRandomData(); 457 TCByteBuffer zeroLen = TCByteBufferFactory.getInstance(false, 0); 458 459 for (int i = 0; i < Math.min(10, data.length); i++) { 460 data[data.length - i - 1] = zeroLen; 461 rewindBuffers(data); 462 testReadArrayBasics(data); 463 464 rewindBuffers(data); 465 testOffsetReadArray(data); 466 } 467 } 468 469 public void testRandomZeroLength() { 470 TCByteBuffer[] data = getRandomDataNonZeroLength(); 471 472 TCByteBuffer zeroLen = TCByteBufferFactory.getInstance(false, 0); 473 474 int num = Math.min(25, data.length); 475 476 for (int i = 0; i < num; i++) { 477 data[random.nextInt(data.length)] = zeroLen; 478 rewindBuffers(data); 479 testReadArrayBasics(data); 480 481 rewindBuffers(data); 482 testOffsetReadArray(data); 483 } 484 } 485 486 private TCByteBuffer[] getRandomDataNonZeroLength() { 487 TCByteBuffer[] data; 488 do { 489 data = getRandomData(); 490 } while ((data.length == 0) || (data[0].limit() == 0)); 491 492 return data; 493 } 494 495 public void testReadArrayBasics() { 496 for (int i = 0; i < 50; i++) { 497 log("testReadArrayBasics(" + i + ")"); 498 testReadArrayBasics(getRandomData()); 499 } 500 } 501 502 private void rewindBuffers(TCByteBuffer[] data) { 503 for (int i = 0; i < data.length; i++) { 504 data[i].rewind(); 505 } 506 } 507 508 private void testReadArrayBasics(TCByteBuffer[] data) { 509 final int numBufs = data.length == 0 ? 0 : data.length - 1; 510 511 reportLengths(data); 512 final long len = length(data); 513 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 514 assertEquals(len, bbis.available()); 515 assertEquals(len, bbis.getTotalLength()); 516 517 int counter = 0; 518 int index = 0; 519 while (bbis.available() > 0) { 520 byte[] buffer = new byte[random.nextInt(10) + 1]; 521 int read = bbis.read(buffer); 522 523 if (read <= 0) { 524 assertEquals(0, bbis.available()); 525 break; 526 } 527 528 counter += read; 529 assertEquals(len - counter, bbis.available()); 530 531 for (int i = 0; i < read; i++) { 532 for (; !data[index].hasRemaining() && index < numBufs; index++) { 533 } 535 assertEquals(data[index].get(), buffer[i]); 536 } 537 } 538 539 if (index < numBufs) { 540 for (; index < numBufs; index++) { 541 assertEquals(0, data[index + 1].limit()); 542 } 543 } 544 545 assertEquals(numBufs, index); 546 if (numBufs > 0) { 547 assertEquals(0, data[numBufs].remaining()); 548 } 549 550 assertEquals(0, bbis.available()); 551 assertEquals(-1, bbis.read()); 552 assertEquals(-1, bbis.read(new byte[10])); 553 assertEquals(-1, bbis.read(new byte[10], 0, 3)); 554 } 555 556 public void testSkip() { 557 for (int i = 0; i < 250; i++) { 558 log("testSkip(" + i + ")"); 559 TCByteBuffer[] data = getRandomDataNonZeroLength(); 560 TCByteBufferInputStream is = new TCByteBufferInputStream(data); 561 562 assertEquals(0, is.skip(0)); 563 assertEquals(0, is.skip(-1)); 564 565 int len = is.getTotalLength(); 566 assertEquals(len, is.available()); 567 long skipped = is.skip(len - 1); 568 assertEquals(len - 1, skipped); 569 assertEquals(1, is.available()); 570 is.read(); 571 assertEquals(0, is.available()); 572 int read = is.read(); 573 assertEquals(-1, read); 574 575 try { 576 is.skip(Integer.MAX_VALUE + 1L); 577 fail(); 578 } catch (IllegalArgumentException iae) { 579 } 581 } 582 } 583 584 private void log(String msg) { 585 System.err.println(new Date () + " - " + msg); 586 } 587 588 public void testZeroLength() { 589 TCByteBuffer[] data = new TCByteBuffer[] {}; 590 long len = length(data); 591 assertEquals(0, len); 592 TCByteBufferInputStream bbis = new TCByteBufferInputStream(data); 593 assertEquals(0, bbis.available()); 594 assertEquals(0, bbis.getTotalLength()); 595 assertEquals(-1, bbis.read()); 596 assertEquals(-1, bbis.read(new byte[10])); 597 assertEquals(-1, bbis.read(new byte[10], 1, 3)); 598 assertEquals(0, bbis.read(new byte[10], 1, 0)); 599 600 testReadArrayBasics(data); 601 testReadBasics(data); 602 testOffsetReadArray(data); 603 604 TCByteBuffer[] toArray = bbis.toArray(); 605 assertEquals(0, toArray.length); 606 } 607 608 private long length(TCByteBuffer[] data) { 609 long rv = 0; 610 for (int i = 0; i < data.length; i++) { 611 rv += data[i].limit(); 612 } 613 return rv; 614 } 615 616 private TCByteBuffer[] createBuffersWithRandomData(int number, int size) { 617 TCByteBuffer[] rv = new TCByteBuffer[number]; 618 for (int i = 0; i < rv.length; i++) { 619 rv[i] = TCByteBufferFactory.getInstance(false, size); 620 byte[] bites = new byte[size]; 621 random.nextBytes(bites); 622 rv[i].put(bites); 623 rv[i].flip(); 624 } 625 626 return rv; 627 } 628 629 private TCByteBuffer[] getRandomData() { 630 final int num = random.nextInt(20); 631 final int maxSize = random.nextInt(200); 632 633 TCByteBuffer[] rv = new TCByteBuffer[num]; 634 635 for (int i = 0; i < num; i++) { 636 rv[i] = TCByteBufferFactory.getInstance(false, maxSize > 0 ? random.nextInt(maxSize) : 0); 637 random.nextBytes(rv[i].array()); 638 } 639 640 return rv; 641 } 642 643 } 644 | Popular Tags |