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 18package org.apache.harmony.tests.java.nio; 19 20import java.nio.BufferOverflowException; 21import java.nio.BufferUnderflowException; 22import java.nio.ByteBuffer; 23import java.nio.ByteOrder; 24import java.nio.CharBuffer; 25import java.nio.DoubleBuffer; 26import java.nio.FloatBuffer; 27import java.nio.IntBuffer; 28import java.nio.InvalidMarkException; 29import java.nio.LongBuffer; 30import java.nio.ReadOnlyBufferException; 31import java.nio.ShortBuffer; 32import java.util.Arrays; 33 34/** 35 * Tests java.nio.ByteBuffer 36 * 37 */ 38public class ByteBufferTest extends AbstractBufferTest { 39 protected static final int SMALL_TEST_LENGTH = 5; 40 protected static final int BUFFER_LENGTH = 250; 41 42 protected ByteBuffer buf; 43 44 protected void setUp() throws Exception { 45 buf = ByteBuffer.allocate(10); 46 loadTestData1(buf); 47 baseBuf = buf; 48 } 49 50 protected void tearDown() throws Exception { 51 super.tearDown(); 52 } 53 54 public void testArray() { 55 if (buf.hasArray()) { 56 byte array[] = buf.array(); 57 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 58 59 loadTestData1(array, buf.arrayOffset(), buf.capacity()); 60 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 61 62 loadTestData2(array, buf.arrayOffset(), buf.capacity()); 63 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 64 65 loadTestData1(buf); 66 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 67 68 loadTestData2(buf); 69 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 70 } else { 71 if (buf.isReadOnly()) { 72 try { 73 buf.array(); 74 fail("Should throw Exception"); //$NON-NLS-1$ 75 } catch (UnsupportedOperationException e) { 76 // expected 77 // Note:can not tell when to throw 78 // UnsupportedOperationException 79 // or ReadOnlyBufferException, so catch all. 80 } 81 } else { 82 try { 83 buf.array(); 84 fail("Should throw Exception"); //$NON-NLS-1$ 85 } catch (UnsupportedOperationException e) { 86 // expected 87 } 88 } 89 } 90 } 91 92 public void testArrayOffset() { 93 if (buf.hasArray()) { 94 byte array[] = buf.array(); 95 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 96 97 loadTestData1(array, buf.arrayOffset(), buf.capacity()); 98 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 99 100 loadTestData2(array, buf.arrayOffset(), buf.capacity()); 101 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 102 103 loadTestData1(buf); 104 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 105 106 loadTestData2(buf); 107 assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity()); 108 } else { 109 if (buf.isReadOnly()) { 110 try { 111 buf.arrayOffset(); 112 fail("Should throw Exception"); //$NON-NLS-1$ 113 } catch (UnsupportedOperationException e) { 114 // expected 115 // Note:can not tell when to throw 116 // UnsupportedOperationException 117 // or ReadOnlyBufferException, so catch all. 118 } 119 } else { 120 try { 121 buf.arrayOffset(); 122 fail("Should throw Exception"); //$NON-NLS-1$ 123 } catch (UnsupportedOperationException e) { 124 // expected 125 } 126 } 127 } 128 } 129 130 public void testAsReadOnlyBuffer() { 131 buf.clear(); 132 buf.mark(); 133 buf.position(buf.limit()); 134 135 // readonly's contents should be the same as buf 136 ByteBuffer readonly = buf.asReadOnlyBuffer(); 137 assertNotSame(buf, readonly); 138 assertTrue(readonly.isReadOnly()); 139 assertEquals(buf.position(), readonly.position()); 140 assertEquals(buf.limit(), readonly.limit()); 141 assertEquals(buf.isDirect(), readonly.isDirect()); 142 assertEquals(buf.order(), readonly.order()); 143 assertContentEquals(buf, readonly); 144 145 // readonly's position, mark, and limit should be independent to buf 146 readonly.reset(); 147 assertEquals(readonly.position(), 0); 148 readonly.clear(); 149 assertEquals(buf.position(), buf.limit()); 150 buf.reset(); 151 assertEquals(buf.position(), 0); 152 } 153 154 public void testCompact() { 155 if (buf.isReadOnly()) { 156 try { 157 buf.compact(); 158 fail("Should throw Exception"); //$NON-NLS-1$ 159 } catch (ReadOnlyBufferException e) { 160 // expected 161 } 162 return; 163 } 164 165 // case: buffer is full 166 buf.clear(); 167 buf.mark(); 168 loadTestData1(buf); 169 ByteBuffer ret = buf.compact(); 170 assertSame(ret, buf); 171 assertEquals(buf.position(), buf.capacity()); 172 assertEquals(buf.limit(), buf.capacity()); 173 assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity()); 174 try { 175 buf.reset(); 176 fail("Should throw Exception"); //$NON-NLS-1$ 177 } catch (InvalidMarkException e) { 178 // expected 179 } 180 181 // case: buffer is empty 182 buf.position(0); 183 buf.limit(0); 184 buf.mark(); 185 ret = buf.compact(); 186 assertSame(ret, buf); 187 assertEquals(buf.position(), 0); 188 assertEquals(buf.limit(), buf.capacity()); 189 assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity()); 190 try { 191 buf.reset(); 192 fail("Should throw Exception"); //$NON-NLS-1$ 193 } catch (InvalidMarkException e) { 194 // expected 195 } 196 197 // case: normal 198 assertTrue(buf.capacity() > SMALL_TEST_LENGTH); 199 buf.position(1); 200 buf.limit(SMALL_TEST_LENGTH); 201 buf.mark(); 202 ret = buf.compact(); 203 assertSame(ret, buf); 204 assertEquals(buf.position(), 4); 205 assertEquals(buf.limit(), buf.capacity()); 206 assertContentLikeTestData1(buf, 0, (byte) 1, 4); 207 try { 208 buf.reset(); 209 fail("Should throw Exception"); //$NON-NLS-1$ 210 } catch (InvalidMarkException e) { 211 // expected 212 } 213 } 214 215 public void testCompareTo() { 216 // compare to self 217 assertEquals(0, buf.compareTo(buf)); 218 219 // normal cases 220 if (!buf.isReadOnly()) { 221 assertTrue(buf.capacity() > SMALL_TEST_LENGTH); 222 buf.clear(); 223 ByteBuffer other = ByteBuffer.allocate(buf.capacity()); 224 loadTestData1(buf); 225 loadTestData1(other); 226 assertEquals(0, buf.compareTo(other)); 227 assertEquals(0, other.compareTo(buf)); 228 buf.position(1); 229 assertTrue(buf.compareTo(other) > 0); 230 assertTrue(other.compareTo(buf) < 0); 231 other.position(2); 232 assertTrue(buf.compareTo(other) < 0); 233 assertTrue(other.compareTo(buf) > 0); 234 buf.position(2); 235 other.limit(SMALL_TEST_LENGTH); 236 assertTrue(buf.compareTo(other) > 0); 237 assertTrue(other.compareTo(buf) < 0); 238 } 239 240 assertTrue(ByteBuffer.wrap(new byte[21]).compareTo(ByteBuffer.allocateDirect(21)) == 0); 241 } 242 243 public void testDuplicate() { 244 buf.clear(); 245 buf.mark(); 246 buf.position(buf.limit()); 247 248 // duplicate's contents should be the same as buf 249 ByteBuffer duplicate = buf.duplicate(); 250 assertNotSame(buf, duplicate); 251 assertEquals(buf.position(), duplicate.position()); 252 assertEquals(buf.limit(), duplicate.limit()); 253 assertEquals(buf.isReadOnly(), duplicate.isReadOnly()); 254 assertEquals(buf.isDirect(), duplicate.isDirect()); 255 assertEquals(buf.order(), duplicate.order()); 256 assertContentEquals(buf, duplicate); 257 258 // duplicate's position, mark, and limit should be independent to buf 259 duplicate.reset(); 260 assertEquals(duplicate.position(), 0); 261 duplicate.clear(); 262 assertEquals(buf.position(), buf.limit()); 263 buf.reset(); 264 assertEquals(buf.position(), 0); 265 266 // duplicate share the same content with buf 267 if (!duplicate.isReadOnly()) { 268 loadTestData1(buf); 269 assertContentEquals(buf, duplicate); 270 loadTestData2(duplicate); 271 assertContentEquals(buf, duplicate); 272 } 273 } 274 275 public void testEquals() { 276 // equal to self 277 assertTrue(buf.equals(buf)); 278 ByteBuffer readonly = buf.asReadOnlyBuffer(); 279 assertTrue(buf.equals(readonly)); 280 ByteBuffer duplicate = buf.duplicate(); 281 assertTrue(buf.equals(duplicate)); 282 283 // always false, if type mismatch 284 assertFalse(buf.equals(Boolean.TRUE)); 285 286 assertTrue(buf.capacity() > SMALL_TEST_LENGTH); 287 288 buf.limit(buf.capacity()).position(0); 289 readonly.limit(readonly.capacity()).position(1); 290 assertFalse(buf.equals(readonly)); 291 292 buf.limit(buf.capacity() - 1).position(0); 293 duplicate.limit(duplicate.capacity()).position(0); 294 assertFalse(buf.equals(duplicate)); 295 } 296 297 /* 298 * Class under test for byte get() 299 */ 300 public void testGet() { 301 buf.clear(); 302 for (int i = 0; i < buf.capacity(); i++) { 303 assertEquals(buf.position(), i); 304 assertEquals(buf.get(), buf.get(i)); 305 } 306 try { 307 buf.get(); 308 fail("Should throw Exception"); //$NON-NLS-1$ 309 } catch (BufferUnderflowException e) { 310 // expected 311 } 312 } 313 314 /* 315 * Class under test for java.nio.ByteBuffer get(byte[]) 316 */ 317 public void testGetbyteArray() { 318 byte array[] = new byte[1]; 319 buf.clear(); 320 for (int i = 0; i < buf.capacity(); i++) { 321 assertEquals(buf.position(), i); 322 ByteBuffer ret = buf.get(array); 323 assertEquals(array[0], buf.get(i)); 324 assertSame(ret, buf); 325 } 326 try { 327 buf.get(array); 328 fail("Should throw Exception"); //$NON-NLS-1$ 329 } catch (BufferUnderflowException e) { 330 // expected 331 } 332 try { 333 buf.get((byte[])null); 334 fail("Should throw Exception"); //$NON-NLS-1$ 335 } catch (NullPointerException e) { 336 // expected 337 } 338 } 339 340 /* 341 * Class under test for java.nio.ByteBuffer get(byte[], int, int) 342 */ 343 public void testGetbyteArrayintint() { 344 buf.clear(); 345 byte array[] = new byte[buf.capacity()]; 346 347 try { 348 buf.get(new byte[buf.capacity() + 1], 0, buf.capacity() + 1); 349 fail("Should throw Exception"); //$NON-NLS-1$ 350 } catch (BufferUnderflowException e) { 351 // expected 352 } 353 assertEquals(buf.position(), 0); 354 try { 355 buf.get(array, -1, array.length); 356 fail("Should throw Exception"); //$NON-NLS-1$ 357 } catch (IndexOutOfBoundsException e) { 358 // expected 359 } 360 buf.get(array, array.length, 0); 361 try { 362 buf.get(array, array.length + 1, 1); 363 fail("Should throw Exception"); //$NON-NLS-1$ 364 } catch (IndexOutOfBoundsException e) { 365 // expected 366 } 367 assertEquals(buf.position(), 0); 368 try { 369 buf.get(array, 2, -1); 370 fail("Should throw Exception"); //$NON-NLS-1$ 371 } catch (IndexOutOfBoundsException e) { 372 // expected 373 } 374 try { 375 buf.get(array, 2, array.length); 376 fail("Should throw Exception"); //$NON-NLS-1$ 377 } catch (IndexOutOfBoundsException e) { 378 // expected 379 } 380 try { 381 buf.get((byte[])null, -1, 0); 382 fail("Should throw Exception"); //$NON-NLS-1$ 383 } catch (NullPointerException e) { 384 // expected 385 } 386 try { 387 buf.get(array, 1, Integer.MAX_VALUE); 388 fail("Should throw Exception"); //$NON-NLS-1$ 389 } catch (IndexOutOfBoundsException e) { 390 // expected 391 } 392 try { 393 buf.get(array, Integer.MAX_VALUE, 1); 394 fail("Should throw Exception"); //$NON-NLS-1$ 395 } catch (IndexOutOfBoundsException e) { 396 // expected 397 } 398 assertEquals(buf.position(), 0); 399 400 buf.clear(); 401 ByteBuffer ret = buf.get(array, 0, array.length); 402 assertEquals(buf.position(), buf.capacity()); 403 assertContentEquals(buf, array, 0, array.length); 404 assertSame(ret, buf); 405 } 406 407 /* 408 * Class under test for byte get(int) 409 */ 410 public void testGetint() { 411 buf.clear(); 412 for (int i = 0; i < buf.capacity(); i++) { 413 assertEquals(buf.position(), i); 414 assertEquals(buf.get(), buf.get(i)); 415 } 416 try { 417 buf.get(-1); 418 fail("Should throw Exception"); //$NON-NLS-1$ 419 } catch (IndexOutOfBoundsException e) { 420 // expected 421 } 422 try { 423 buf.get(buf.limit()); 424 fail("Should throw Exception"); //$NON-NLS-1$ 425 } catch (IndexOutOfBoundsException e) { 426 // expected 427 } 428 } 429 430 public void testHasArray() { 431 if (buf.hasArray()) { 432 assertNotNull(buf.array()); 433 } else { 434 if (buf.isReadOnly()) { 435 try { 436 buf.array(); 437 fail("Should throw Exception"); //$NON-NLS-1$ 438 } catch (UnsupportedOperationException e) { 439 // expected 440 // Note:can not tell when to throw 441 // UnsupportedOperationException 442 // or ReadOnlyBufferException, so catch all. 443 } 444 } else { 445 try { 446 buf.array(); 447 fail("Should throw Exception"); //$NON-NLS-1$ 448 } catch (UnsupportedOperationException e) { 449 // expected 450 } 451 } 452 } 453 } 454 455 public void testHashCode() { 456 buf.clear(); 457 loadTestData1(buf); 458 ByteBuffer readonly = buf.asReadOnlyBuffer(); 459 ByteBuffer duplicate = buf.duplicate(); 460 assertTrue(buf.hashCode() == readonly.hashCode()); 461 assertTrue(buf.capacity() > SMALL_TEST_LENGTH); 462 duplicate.position(buf.capacity()/2); 463 assertTrue(buf.hashCode()!= duplicate.hashCode()); 464 } 465 466 //for the testHashCode() method of readonly subclasses 467 protected void readOnlyHashCode() { 468 //create a new buffer initiated with some data 469 ByteBuffer buf = ByteBuffer.allocate(BUFFER_LENGTH); 470 loadTestData1(buf); 471 buf = buf.asReadOnlyBuffer(); 472 buf.clear(); 473 ByteBuffer readonly = buf.asReadOnlyBuffer(); 474 ByteBuffer duplicate = buf.duplicate(); 475 assertEquals(buf.hashCode(),readonly.hashCode()); 476 duplicate.position(buf.capacity()/2); 477 assertTrue(buf.hashCode()!= duplicate.hashCode()); 478 } 479 480 public void testIsDirect() { 481 buf.isDirect(); 482 } 483 484 public void testOrder() { 485 // BIG_ENDIAN is the default byte order 486 assertEquals(ByteOrder.BIG_ENDIAN, buf.order()); 487 488 buf.order(ByteOrder.LITTLE_ENDIAN); 489 assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order()); 490 491 buf.order(ByteOrder.BIG_ENDIAN); 492 assertEquals(ByteOrder.BIG_ENDIAN, buf.order()); 493 494 // Regression test for HARMONY-798 495 buf.order((ByteOrder)null); 496 assertEquals(ByteOrder.LITTLE_ENDIAN, buf.order()); 497 498 buf.order(ByteOrder.BIG_ENDIAN); 499 } 500 501 /* 502 * Class under test for java.nio.ByteBuffer put(byte) 503 */ 504 public void testPutbyte() { 505 if (buf.isReadOnly()) { 506 try { 507 buf.clear(); 508 buf.put((byte) 0); 509 fail("Should throw Exception"); //$NON-NLS-1$ 510 } catch (ReadOnlyBufferException e) { 511 // expected 512 } 513 return; 514 } 515 516 buf.clear(); 517 for (int i = 0; i < buf.capacity(); i++) { 518 assertEquals(buf.position(), i); 519 ByteBuffer ret = buf.put((byte) i); 520 assertEquals(buf.get(i), (byte) i); 521 assertSame(ret, buf); 522 } 523 try { 524 buf.put((byte) 0); 525 fail("Should throw Exception"); //$NON-NLS-1$ 526 } catch (BufferOverflowException e) { 527 // expected 528 } 529 } 530 531 /* 532 * Class under test for java.nio.ByteBuffer put(byte[]) 533 */ 534 public void testPutbyteArray() { 535 byte array[] = new byte[1]; 536 if (buf.isReadOnly()) { 537 try { 538 buf.put(array); 539 fail("Should throw Exception"); //$NON-NLS-1$ 540 } catch (ReadOnlyBufferException e) { 541 // expected 542 } 543 return; 544 } 545 546 buf.clear(); 547 for (int i = 0; i < buf.capacity(); i++) { 548 assertEquals(buf.position(), i); 549 array[0] = (byte) i; 550 ByteBuffer ret = buf.put(array); 551 assertEquals(buf.get(i), (byte) i); 552 assertSame(ret, buf); 553 } 554 try { 555 buf.put(array); 556 fail("Should throw Exception"); //$NON-NLS-1$ 557 } catch (BufferOverflowException e) { 558 // expected 559 } 560 try { 561 buf.put((byte[])null); 562 fail("Should throw Exception"); //$NON-NLS-1$ 563 } catch (NullPointerException e) { 564 // expected 565 } 566 } 567 568 /* 569 * Class under test for java.nio.ByteBuffer put(byte[], int, int) 570 */ 571 public void testPutbyteArrayintint() { 572 buf.clear(); 573 byte array[] = new byte[buf.capacity()]; 574 if (buf.isReadOnly()) { 575 try { 576 buf.put(array, 0, array.length); 577 fail("Should throw Exception"); //$NON-NLS-1$ 578 } catch (ReadOnlyBufferException e) { 579 // expected 580 } 581 return; 582 } 583 584 try { 585 buf.put(new byte[buf.capacity() + 1], 0, buf.capacity() + 1); 586 fail("Should throw Exception"); //$NON-NLS-1$ 587 } catch (BufferOverflowException e) { 588 // expected 589 } 590 assertEquals(buf.position(), 0); 591 try { 592 buf.put(array, -1, array.length); 593 fail("Should throw Exception"); //$NON-NLS-1$ 594 } catch (IndexOutOfBoundsException e) { 595 // expected 596 } 597 try { 598 buf.put(array, array.length + 1, 0); 599 fail("Should throw Exception"); //$NON-NLS-1$ 600 } catch (IndexOutOfBoundsException e) { 601 // expected 602 } 603 buf.put(array, array.length, 0); 604 assertEquals(buf.position(), 0); 605 try { 606 buf.put(array, 0, -1); 607 fail("Should throw Exception"); //$NON-NLS-1$ 608 } catch (IndexOutOfBoundsException e) { 609 // expected 610 } 611 try { 612 buf.put(array, 2, array.length); 613 fail("Should throw Exception"); //$NON-NLS-1$ 614 } catch (IndexOutOfBoundsException e) { 615 // expected 616 } 617 618 try { 619 buf.put(array, 2, Integer.MAX_VALUE); 620 fail("Should throw Exception"); //$NON-NLS-1$ 621 } catch (IndexOutOfBoundsException e) { 622 // expected 623 } 624 try { 625 buf.put(array, Integer.MAX_VALUE, 1); 626 fail("Should throw Exception"); //$NON-NLS-1$ 627 } catch (IndexOutOfBoundsException e) { 628 // expected 629 } 630 try { 631 buf.put((byte[])null, 2, Integer.MAX_VALUE); 632 fail("Should throw Exception"); //$NON-NLS-1$ 633 } catch (NullPointerException e) { 634 // expected 635 } 636 637 assertEquals(buf.position(), 0); 638 639 loadTestData2(array, 0, array.length); 640 ByteBuffer ret = buf.put(array, 0, array.length); 641 assertEquals(buf.position(), buf.capacity()); 642 assertContentEquals(buf, array, 0, array.length); 643 assertSame(ret, buf); 644 } 645 646 /* 647 * Class under test for java.nio.ByteBuffer put(java.nio.ByteBuffer) 648 */ 649 public void testPutByteBuffer() { 650 ByteBuffer other = ByteBuffer.allocate(buf.capacity()); 651 if (buf.isReadOnly()) { 652 try { 653 buf.clear(); 654 buf.put(other); 655 fail("Should throw Exception"); //$NON-NLS-1$ 656 } catch (ReadOnlyBufferException e) { 657 // expected 658 } 659 try { 660 buf.clear(); 661 buf.put((ByteBuffer)null); 662 fail("Should throw Exception"); //$NON-NLS-1$ 663 } catch (ReadOnlyBufferException e) { 664 // expected 665 } 666 return; 667 } 668 669 try { 670 buf.put(buf); 671 fail("Should throw Exception"); //$NON-NLS-1$ 672 } catch (IllegalArgumentException e) { 673 // expected 674 } 675 try { 676 buf.put(ByteBuffer.allocate(buf.capacity() + 1)); 677 fail("Should throw Exception"); //$NON-NLS-1$ 678 } catch (BufferOverflowException e) { 679 // expected 680 } 681 682 try { 683 buf.put((ByteBuffer)null); 684 fail("Should throw Exception"); //$NON-NLS-1$ 685 } catch (NullPointerException e) { 686 // expected 687 } 688 loadTestData2(other); 689 other.clear(); 690 buf.clear(); 691 ByteBuffer ret = buf.put(other); 692 assertEquals(other.position(), other.capacity()); 693 assertEquals(buf.position(), buf.capacity()); 694 assertContentEquals(other, buf); 695 assertSame(ret, buf); 696 } 697 698 /* 699 * Class under test for java.nio.ByteBuffer put(int, byte) 700 */ 701 public void testPutintbyte() { 702 if (buf.isReadOnly()) { 703 try { 704 buf.put(0, (byte) 0); 705 fail("Should throw Exception"); //$NON-NLS-1$ 706 } catch (ReadOnlyBufferException e) { 707 // expected 708 } 709 return; 710 } 711 712 buf.clear(); 713 for (int i = 0; i < buf.capacity(); i++) { 714 assertEquals(buf.position(), 0); 715 ByteBuffer ret = buf.put(i, (byte) i); 716 assertEquals(buf.get(i), (byte) i); 717 assertSame(ret, buf); 718 } 719 try { 720 buf.put(-1, (byte) 0); 721 fail("Should throw Exception"); //$NON-NLS-1$ 722 } catch (IndexOutOfBoundsException e) { 723 // expected 724 } 725 try { 726 buf.put(buf.limit(), (byte) 0); 727 fail("Should throw Exception"); //$NON-NLS-1$ 728 } catch (IndexOutOfBoundsException e) { 729 // expected 730 } 731 } 732 733 public void testSlice() { 734 assertTrue(buf.capacity() > SMALL_TEST_LENGTH); 735 buf.position(1); 736 buf.limit(buf.capacity() - 1); 737 738 ByteBuffer slice = buf.slice(); 739 assertEquals(buf.isReadOnly(), slice.isReadOnly()); 740 assertEquals(buf.isDirect(), slice.isDirect()); 741 assertEquals(buf.order(), slice.order()); 742 assertEquals(slice.position(), 0); 743 assertEquals(slice.limit(), buf.remaining()); 744 assertEquals(slice.capacity(), buf.remaining()); 745 try { 746 slice.reset(); 747 fail("Should throw Exception"); //$NON-NLS-1$ 748 } catch (InvalidMarkException e) { 749 // expected 750 } 751 752 // slice share the same content with buf 753 if (!slice.isReadOnly()) { 754 loadTestData1(slice); 755 assertContentLikeTestData1(buf, 1, (byte) 0, slice.capacity()); 756 buf.put(2, (byte) 100); 757 assertEquals(slice.get(1), 100); 758 } 759 } 760 761 public void testToString() { 762 String str = buf.toString(); 763 assertTrue(str.indexOf("Byte") >= 0 || str.indexOf("byte") >= 0); 764 assertTrue(str.indexOf("" + buf.position()) >= 0); 765 assertTrue(str.indexOf("" + buf.limit()) >= 0); 766 assertTrue(str.indexOf("" + buf.capacity()) >= 0); 767 } 768 769 public void testAsCharBuffer() { 770 CharBuffer charBuffer; 771 byte bytes[] = new byte[2]; 772 char value; 773 774 // test BIG_ENDIAN char buffer, read 775 buf.clear(); 776 buf.order(ByteOrder.BIG_ENDIAN); 777 charBuffer = buf.asCharBuffer(); 778 assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order()); 779 while (charBuffer.remaining() > 0) { 780 buf.get(bytes); 781 value = charBuffer.get(); 782 assertEquals(bytes2char(bytes, buf.order()), value); 783 } 784 785 // test LITTLE_ENDIAN char buffer, read 786 buf.clear(); 787 buf.order(ByteOrder.LITTLE_ENDIAN); 788 charBuffer = buf.asCharBuffer(); 789 assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order()); 790 while (charBuffer.remaining() > 0) { 791 buf.get(bytes); 792 value = charBuffer.get(); 793 assertEquals(bytes2char(bytes, buf.order()), value); 794 } 795 796 if (!buf.isReadOnly()) { 797 // test BIG_ENDIAN char buffer, write 798 buf.clear(); 799 buf.order(ByteOrder.BIG_ENDIAN); 800 charBuffer = buf.asCharBuffer(); 801 assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order()); 802 while (charBuffer.remaining() > 0) { 803 value = (char) charBuffer.remaining(); 804 charBuffer.put(value); 805 buf.get(bytes); 806 assertTrue(Arrays.equals(bytes, char2bytes(value, buf.order()))); 807 } 808 809 // test LITTLE_ENDIAN char buffer, write 810 buf.clear(); 811 buf.order(ByteOrder.LITTLE_ENDIAN); 812 charBuffer = buf.asCharBuffer(); 813 assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order()); 814 while (charBuffer.remaining() > 0) { 815 value = (char) charBuffer.remaining(); 816 charBuffer.put(value); 817 buf.get(bytes); 818 assertTrue(Arrays.equals(bytes, char2bytes(value, buf.order()))); 819 } 820 } 821 buf.clear(); 822 buf.order(ByteOrder.BIG_ENDIAN); 823 } 824 825 public void testAsDoubleBuffer() { 826 DoubleBuffer doubleBuffer; 827 byte bytes[] = new byte[8]; 828 double value; 829 830 // test BIG_ENDIAN double buffer, read 831 buf.clear(); 832 buf.order(ByteOrder.BIG_ENDIAN); 833 doubleBuffer = buf.asDoubleBuffer(); 834 assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order()); 835 while (doubleBuffer.remaining() > 0) { 836 buf.get(bytes); 837 value = doubleBuffer.get(); 838 if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double 839 .isNaN(value))) { 840 assertEquals(bytes2double(bytes, buf.order()), value, 0.00); 841 } 842 } 843 844 // test LITTLE_ENDIAN double buffer, read 845 buf.clear(); 846 buf.order(ByteOrder.LITTLE_ENDIAN); 847 doubleBuffer = buf.asDoubleBuffer(); 848 assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order()); 849 while (doubleBuffer.remaining() > 0) { 850 buf.get(bytes); 851 value = doubleBuffer.get(); 852 if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double 853 .isNaN(value))) { 854 assertEquals(bytes2double(bytes, buf.order()), value, 0.00); 855 } 856 } 857 858 if (!buf.isReadOnly()) { 859 // test BIG_ENDIAN double buffer, write 860 buf.clear(); 861 buf.order(ByteOrder.BIG_ENDIAN); 862 doubleBuffer = buf.asDoubleBuffer(); 863 assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order()); 864 while (doubleBuffer.remaining() > 0) { 865 value = (double) doubleBuffer.remaining(); 866 doubleBuffer.put(value); 867 buf.get(bytes); 868 assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order()))); 869 } 870 871 // test LITTLE_ENDIAN double buffer, write 872 buf.clear(); 873 buf.order(ByteOrder.LITTLE_ENDIAN); 874 doubleBuffer = buf.asDoubleBuffer(); 875 assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order()); 876 while (doubleBuffer.remaining() > 0) { 877 value = (double) doubleBuffer.remaining(); 878 doubleBuffer.put(value); 879 buf.get(bytes); 880 assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order()))); 881 } 882 } 883 884 buf.clear(); 885 buf.order(ByteOrder.BIG_ENDIAN); 886 } 887 888 public void testAsFloatBuffer() { 889 FloatBuffer floatBuffer; 890 byte bytes[] = new byte[4]; 891 float value; 892 893 // test BIG_ENDIAN float buffer, read 894 buf.clear(); 895 buf.order(ByteOrder.BIG_ENDIAN); 896 floatBuffer = buf.asFloatBuffer(); 897 assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order()); 898 while (floatBuffer.remaining() > 0) { 899 buf.get(bytes); 900 value = floatBuffer.get(); 901 if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float 902 .isNaN(value))) { 903 assertEquals(bytes2float(bytes, buf.order()), value, 0.00); 904 } 905 } 906 907 // test LITTLE_ENDIAN float buffer, read 908 buf.clear(); 909 buf.order(ByteOrder.LITTLE_ENDIAN); 910 floatBuffer = buf.asFloatBuffer(); 911 assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order()); 912 while (floatBuffer.remaining() > 0) { 913 buf.get(bytes); 914 value = floatBuffer.get(); 915 if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float 916 .isNaN(value))) { 917 assertEquals(bytes2float(bytes, buf.order()), value, 0.00); 918 } 919 } 920 921 if (!buf.isReadOnly()) { 922 // test BIG_ENDIAN float buffer, write 923 buf.clear(); 924 buf.order(ByteOrder.BIG_ENDIAN); 925 floatBuffer = buf.asFloatBuffer(); 926 assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order()); 927 while (floatBuffer.remaining() > 0) { 928 value = (float) floatBuffer.remaining(); 929 floatBuffer.put(value); 930 buf.get(bytes); 931 assertTrue(Arrays.equals(bytes, float2bytes(value, buf.order()))); 932 } 933 934 // test LITTLE_ENDIAN float buffer, write 935 buf.clear(); 936 buf.order(ByteOrder.LITTLE_ENDIAN); 937 floatBuffer = buf.asFloatBuffer(); 938 assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order()); 939 while (floatBuffer.remaining() > 0) { 940 value = (float) floatBuffer.remaining(); 941 floatBuffer.put(value); 942 buf.get(bytes); 943 assertTrue(Arrays.equals(bytes, float2bytes(value, buf.order()))); 944 } 945 } 946 947 buf.clear(); 948 buf.order(ByteOrder.BIG_ENDIAN); 949 } 950 951 public void testAsIntBuffer() { 952 IntBuffer intBuffer; 953 byte bytes[] = new byte[4]; 954 int value; 955 956 // test BIG_ENDIAN int buffer, read 957 buf.clear(); 958 buf.order(ByteOrder.BIG_ENDIAN); 959 intBuffer = buf.asIntBuffer(); 960 assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order()); 961 while (intBuffer.remaining() > 0) { 962 buf.get(bytes); 963 value = intBuffer.get(); 964 assertEquals(bytes2int(bytes, buf.order()), value); 965 } 966 967 // test LITTLE_ENDIAN int buffer, read 968 buf.clear(); 969 buf.order(ByteOrder.LITTLE_ENDIAN); 970 intBuffer = buf.asIntBuffer(); 971 assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order()); 972 while (intBuffer.remaining() > 0) { 973 buf.get(bytes); 974 value = intBuffer.get(); 975 assertEquals(bytes2int(bytes, buf.order()), value); 976 } 977 978 if (!buf.isReadOnly()) { 979 // test BIG_ENDIAN int buffer, write 980 buf.clear(); 981 buf.order(ByteOrder.BIG_ENDIAN); 982 intBuffer = buf.asIntBuffer(); 983 assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order()); 984 while (intBuffer.remaining() > 0) { 985 value = (int) intBuffer.remaining(); 986 intBuffer.put(value); 987 buf.get(bytes); 988 assertTrue(Arrays.equals(bytes, int2bytes(value, buf.order()))); 989 } 990 991 // test LITTLE_ENDIAN int buffer, write 992 buf.clear(); 993 buf.order(ByteOrder.LITTLE_ENDIAN); 994 intBuffer = buf.asIntBuffer(); 995 assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order()); 996 while (intBuffer.remaining() > 0) { 997 value = (int) intBuffer.remaining(); 998 intBuffer.put(value); 999 buf.get(bytes); 1000 assertTrue(Arrays.equals(bytes, int2bytes(value, buf.order()))); 1001 } 1002 } 1003 1004 buf.clear(); 1005 buf.order(ByteOrder.BIG_ENDIAN); 1006 } 1007 1008 public void testAsLongBuffer() { 1009 LongBuffer longBuffer; 1010 byte bytes[] = new byte[8]; 1011 long value; 1012 1013 // test BIG_ENDIAN long buffer, read 1014 buf.clear(); 1015 buf.order(ByteOrder.BIG_ENDIAN); 1016 longBuffer = buf.asLongBuffer(); 1017 assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order()); 1018 while (longBuffer.remaining() > 0) { 1019 buf.get(bytes); 1020 value = longBuffer.get(); 1021 assertEquals(bytes2long(bytes, buf.order()), value); 1022 } 1023 1024 // test LITTLE_ENDIAN long buffer, read 1025 buf.clear(); 1026 buf.order(ByteOrder.LITTLE_ENDIAN); 1027 longBuffer = buf.asLongBuffer(); 1028 assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order()); 1029 while (longBuffer.remaining() > 0) { 1030 buf.get(bytes); 1031 value = longBuffer.get(); 1032 assertEquals(bytes2long(bytes, buf.order()), value); 1033 } 1034 1035 if (!buf.isReadOnly()) { 1036 // test BIG_ENDIAN long buffer, write 1037 buf.clear(); 1038 buf.order(ByteOrder.BIG_ENDIAN); 1039 longBuffer = buf.asLongBuffer(); 1040 assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order()); 1041 while (longBuffer.remaining() > 0) { 1042 value = (long) longBuffer.remaining(); 1043 longBuffer.put(value); 1044 buf.get(bytes); 1045 assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order()))); 1046 } 1047 1048 // test LITTLE_ENDIAN long buffer, write 1049 buf.clear(); 1050 buf.order(ByteOrder.LITTLE_ENDIAN); 1051 longBuffer = buf.asLongBuffer(); 1052 assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order()); 1053 while (longBuffer.remaining() > 0) { 1054 value = (long) longBuffer.remaining(); 1055 longBuffer.put(value); 1056 buf.get(bytes); 1057 assertTrue(Arrays.equals(bytes, long2bytes(value, buf.order()))); 1058 } 1059 } 1060 1061 buf.clear(); 1062 buf.order(ByteOrder.BIG_ENDIAN); 1063 } 1064 1065 public void testAsShortBuffer() { 1066 ShortBuffer shortBuffer; 1067 byte bytes[] = new byte[2]; 1068 short value; 1069 1070 // test BIG_ENDIAN short buffer, read 1071 buf.clear(); 1072 buf.order(ByteOrder.BIG_ENDIAN); 1073 shortBuffer = buf.asShortBuffer(); 1074 assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order()); 1075 while (shortBuffer.remaining() > 0) { 1076 buf.get(bytes); 1077 value = shortBuffer.get(); 1078 assertEquals(bytes2short(bytes, buf.order()), value); 1079 } 1080 1081 // test LITTLE_ENDIAN short buffer, read 1082 buf.clear(); 1083 buf.order(ByteOrder.LITTLE_ENDIAN); 1084 shortBuffer = buf.asShortBuffer(); 1085 assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order()); 1086 while (shortBuffer.remaining() > 0) { 1087 buf.get(bytes); 1088 value = shortBuffer.get(); 1089 assertEquals(bytes2short(bytes, buf.order()), value); 1090 } 1091 1092 if (!buf.isReadOnly()) { 1093 // test BIG_ENDIAN short buffer, write 1094 buf.clear(); 1095 buf.order(ByteOrder.BIG_ENDIAN); 1096 shortBuffer = buf.asShortBuffer(); 1097 assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order()); 1098 while (shortBuffer.remaining() > 0) { 1099 value = (short) shortBuffer.remaining(); 1100 shortBuffer.put(value); 1101 buf.get(bytes); 1102 assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order()))); 1103 } 1104 1105 // test LITTLE_ENDIAN short buffer, write 1106 buf.clear(); 1107 buf.order(ByteOrder.LITTLE_ENDIAN); 1108 shortBuffer = buf.asShortBuffer(); 1109 assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order()); 1110 while (shortBuffer.remaining() > 0) { 1111 value = (short) shortBuffer.remaining(); 1112 shortBuffer.put(value); 1113 buf.get(bytes); 1114 assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order()))); 1115 } 1116 } 1117 1118 buf.clear(); 1119 buf.order(ByteOrder.BIG_ENDIAN); 1120 } 1121 1122 public void testGetChar() { 1123 int nbytes = 2; 1124 byte bytes[] = new byte[nbytes]; 1125 char value; 1126 buf.clear(); 1127 for (int i = 0; buf.remaining() >= nbytes; i++) { 1128 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1129 : ByteOrder.LITTLE_ENDIAN); 1130 assertEquals(i * nbytes, buf.position()); 1131 buf.mark(); 1132 buf.get(bytes); 1133 buf.reset(); 1134 value = buf.getChar(); 1135 assertEquals(bytes2char(bytes, buf.order()), value); 1136 } 1137 1138 try { 1139 buf.getChar(); 1140 fail("Should throw Exception"); //$NON-NLS-1$ 1141 } catch (BufferUnderflowException e) { 1142 // expected 1143 } 1144 1145 buf.order(ByteOrder.BIG_ENDIAN); 1146 } 1147 1148 public void testGetCharint() { 1149 int nbytes = 2; 1150 byte bytes[] = new byte[nbytes]; 1151 char value; 1152 buf.clear(); 1153 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1154 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1155 : ByteOrder.LITTLE_ENDIAN); 1156 buf.position(i); 1157 value = buf.getChar(i); 1158 assertEquals(i, buf.position()); 1159 buf.get(bytes); 1160 assertEquals(bytes2char(bytes, buf.order()), value); 1161 } 1162 1163 try { 1164 buf.getChar(-1); 1165 fail("Should throw Exception"); //$NON-NLS-1$ 1166 } catch (IndexOutOfBoundsException e) { 1167 // expected 1168 } 1169 try { 1170 buf.getChar(buf.limit() - nbytes + 1); 1171 fail("Should throw Exception"); //$NON-NLS-1$ 1172 } catch (IndexOutOfBoundsException e) { 1173 // expected 1174 } 1175 1176 buf.order(ByteOrder.BIG_ENDIAN); 1177 } 1178 1179 public void testPutChar() { 1180 if (buf.isReadOnly()) { 1181 try { 1182 buf.clear(); 1183 buf.putChar((char) 1); 1184 fail("Should throw Exception"); //$NON-NLS-1$ 1185 } catch (ReadOnlyBufferException e) { 1186 // expected 1187 } 1188 return; 1189 } 1190 1191 int nbytes = 2; 1192 byte bytes[] = new byte[nbytes]; 1193 char value = 0; 1194 buf.clear(); 1195 for (int i = 0; buf.remaining() >= nbytes; i++) { 1196 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1197 : ByteOrder.LITTLE_ENDIAN); 1198 value = (char) i; 1199 buf.mark(); 1200 buf.putChar(value); 1201 assertEquals((i + 1) * nbytes, buf.position()); 1202 buf.reset(); 1203 buf.get(bytes); 1204 assertTrue(Arrays.equals(char2bytes(value, buf.order()), bytes)); 1205 } 1206 1207 try { 1208 buf.putChar(value); 1209 fail("Should throw Exception"); //$NON-NLS-1$ 1210 } catch (BufferOverflowException e) { 1211 // expected 1212 } 1213 1214 buf.order(ByteOrder.BIG_ENDIAN); 1215 } 1216 1217 public void testPutCharint() { 1218 if (buf.isReadOnly()) { 1219 try { 1220 buf.putChar(0, (char) 1); 1221 fail("Should throw Exception"); //$NON-NLS-1$ 1222 } catch (ReadOnlyBufferException e) { 1223 // expected 1224 } 1225 return; 1226 } 1227 1228 int nbytes = 2; 1229 byte bytes[] = new byte[nbytes]; 1230 char value = 0; 1231 buf.clear(); 1232 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1233 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1234 : ByteOrder.LITTLE_ENDIAN); 1235 value = (char) i; 1236 buf.position(i); 1237 buf.putChar(i, value); 1238 assertEquals(i, buf.position()); 1239 buf.get(bytes); 1240 assertTrue(Arrays.equals(char2bytes(value, buf.order()), bytes)); 1241 } 1242 1243 try { 1244 buf.putChar(-1, value); 1245 fail("Should throw Exception"); //$NON-NLS-1$ 1246 } catch (IndexOutOfBoundsException e) { 1247 // expected 1248 } 1249 try { 1250 buf.putChar(buf.limit() - nbytes + 1, value); 1251 fail("Should throw Exception"); //$NON-NLS-1$ 1252 } catch (IndexOutOfBoundsException e) { 1253 // expected 1254 } 1255 1256 buf.order(ByteOrder.BIG_ENDIAN); 1257 1258 try { 1259 ByteBuffer.allocateDirect(16).putChar(Integer.MAX_VALUE, 'h'); 1260 } catch (IndexOutOfBoundsException e) { 1261 //expected 1262 } 1263 } 1264 1265 public void testGetDouble() { 1266 int nbytes = 8; 1267 byte bytes[] = new byte[nbytes]; 1268 double value; 1269 buf.clear(); 1270 for (int i = 0; buf.remaining() >= nbytes; i++) { 1271 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1272 : ByteOrder.LITTLE_ENDIAN); 1273 assertEquals(i * nbytes, buf.position()); 1274 buf.mark(); 1275 buf.get(bytes); 1276 buf.reset(); 1277 value = buf.getDouble(); 1278 if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double 1279 .isNaN(value))) { 1280 assertEquals(bytes2double(bytes, buf.order()), value, 0.00); 1281 } 1282 } 1283 1284 try { 1285 buf.getDouble(); 1286 fail("Should throw Exception"); //$NON-NLS-1$ 1287 } catch (BufferUnderflowException e) { 1288 // expected 1289 } 1290 1291 buf.order(ByteOrder.BIG_ENDIAN); 1292 } 1293 1294 public void testGetDoubleint() { 1295 int nbytes = 8; 1296 byte bytes[] = new byte[nbytes]; 1297 double value; 1298 buf.clear(); 1299 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1300 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1301 : ByteOrder.LITTLE_ENDIAN); 1302 buf.position(i); 1303 value = buf.getDouble(i); 1304 assertEquals(i, buf.position()); 1305 buf.get(bytes); 1306 if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double 1307 .isNaN(value))) { 1308 assertEquals(bytes2double(bytes, buf.order()), value, 0.00); 1309 } 1310 } 1311 1312 try { 1313 buf.getDouble(-1); 1314 fail("Should throw Exception"); //$NON-NLS-1$ 1315 } catch (IndexOutOfBoundsException e) { 1316 // expected 1317 } 1318 try { 1319 buf.getDouble(buf.limit() - nbytes + 1); 1320 fail("Should throw Exception"); //$NON-NLS-1$ 1321 } catch (IndexOutOfBoundsException e) { 1322 // expected 1323 } 1324 1325 buf.order(ByteOrder.BIG_ENDIAN); 1326 1327 try { 1328 ByteBuffer.allocateDirect(16).getDouble(Integer.MAX_VALUE); 1329 } catch (IndexOutOfBoundsException e) { 1330 //expected 1331 } 1332 } 1333 1334 public void testPutDouble() { 1335 if (buf.isReadOnly()) { 1336 try { 1337 buf.clear(); 1338 buf.putDouble((double) 1); 1339 fail("Should throw Exception"); //$NON-NLS-1$ 1340 } catch (ReadOnlyBufferException e) { 1341 // expected 1342 } 1343 return; 1344 } 1345 1346 int nbytes = 8; 1347 byte bytes[] = new byte[nbytes]; 1348 double value = 0; 1349 buf.clear(); 1350 for (int i = 0; buf.remaining() >= nbytes; i++) { 1351 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1352 : ByteOrder.LITTLE_ENDIAN); 1353 value = (double) i; 1354 buf.mark(); 1355 buf.putDouble(value); 1356 assertEquals((i + 1) * nbytes, buf.position()); 1357 buf.reset(); 1358 buf.get(bytes); 1359 assertTrue(Arrays.equals(double2bytes(value, buf.order()), bytes)); 1360 } 1361 1362 try { 1363 buf.putDouble(value); 1364 fail("Should throw Exception"); //$NON-NLS-1$ 1365 } catch (BufferOverflowException e) { 1366 // expected 1367 } 1368 1369 buf.order(ByteOrder.BIG_ENDIAN); 1370 } 1371 1372 public void testPutDoubleint() { 1373 if (buf.isReadOnly()) { 1374 try { 1375 buf.putDouble(0, (double) 1); 1376 fail("Should throw Exception"); //$NON-NLS-1$ 1377 } catch (ReadOnlyBufferException e) { 1378 // expected 1379 } 1380 return; 1381 } 1382 1383 int nbytes = 8; 1384 byte bytes[] = new byte[nbytes]; 1385 double value = 0; 1386 buf.clear(); 1387 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1388 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1389 : ByteOrder.LITTLE_ENDIAN); 1390 value = (double) i; 1391 buf.position(i); 1392 buf.putDouble(i, value); 1393 assertEquals(i, buf.position()); 1394 buf.get(bytes); 1395 assertTrue(Arrays.equals(double2bytes(value, buf.order()), bytes)); 1396 } 1397 1398 try { 1399 buf.putDouble(-1, value); 1400 fail("Should throw Exception"); //$NON-NLS-1$ 1401 } catch (IndexOutOfBoundsException e) { 1402 // expected 1403 } 1404 try { 1405 buf.putDouble(buf.limit() - nbytes + 1, value); 1406 fail("Should throw Exception"); //$NON-NLS-1$ 1407 } catch (IndexOutOfBoundsException e) { 1408 // expected 1409 } 1410 1411 buf.order(ByteOrder.BIG_ENDIAN); 1412 } 1413 1414 public void testGetFloat() { 1415 int nbytes = 4; 1416 byte bytes[] = new byte[nbytes]; 1417 float value; 1418 buf.clear(); 1419 for (int i = 0; buf.remaining() >= nbytes; i++) { 1420 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1421 : ByteOrder.LITTLE_ENDIAN); 1422 assertEquals(i * nbytes, buf.position()); 1423 buf.mark(); 1424 buf.get(bytes); 1425 buf.reset(); 1426 value = buf.getFloat(); 1427 if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float 1428 .isNaN(value))) { 1429 assertEquals(bytes2float(bytes, buf.order()), value, 0.00); 1430 } 1431 } 1432 1433 try { 1434 buf.getFloat(); 1435 fail("Should throw Exception"); //$NON-NLS-1$ 1436 } catch (BufferUnderflowException e) { 1437 // expected 1438 } 1439 1440 buf.order(ByteOrder.BIG_ENDIAN); 1441 } 1442 1443 public void testGetFloatint() { 1444 int nbytes = 4; 1445 byte bytes[] = new byte[nbytes]; 1446 float value; 1447 buf.clear(); 1448 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1449 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1450 : ByteOrder.LITTLE_ENDIAN); 1451 buf.position(i); 1452 value = buf.getFloat(i); 1453 assertEquals(i, buf.position()); 1454 buf.get(bytes); 1455 if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float 1456 .isNaN(value))) { 1457 assertEquals(bytes2float(bytes, buf.order()), value, 0.00); 1458 } 1459 } 1460 1461 try { 1462 buf.getFloat(-1); 1463 fail("Should throw Exception"); //$NON-NLS-1$ 1464 } catch (IndexOutOfBoundsException e) { 1465 // expected 1466 } 1467 try { 1468 buf.getFloat(buf.limit() - nbytes + 1); 1469 fail("Should throw Exception"); //$NON-NLS-1$ 1470 } catch (IndexOutOfBoundsException e) { 1471 // expected 1472 } 1473 1474 buf.order(ByteOrder.BIG_ENDIAN); 1475 } 1476 1477 public void testPutFloat() { 1478 if (buf.isReadOnly()) { 1479 try { 1480 buf.clear(); 1481 buf.putFloat((float) 1); 1482 fail("Should throw Exception"); //$NON-NLS-1$ 1483 } catch (ReadOnlyBufferException e) { 1484 // expected 1485 } 1486 return; 1487 } 1488 1489 int nbytes = 4; 1490 byte bytes[] = new byte[nbytes]; 1491 float value = 0; 1492 buf.clear(); 1493 for (int i = 0; buf.remaining() >= nbytes; i++) { 1494 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1495 : ByteOrder.LITTLE_ENDIAN); 1496 value = (float) i; 1497 buf.mark(); 1498 buf.putFloat(value); 1499 assertEquals((i + 1) * nbytes, buf.position()); 1500 buf.reset(); 1501 buf.get(bytes); 1502 assertTrue(Arrays.equals(float2bytes(value, buf.order()), bytes)); 1503 } 1504 1505 try { 1506 buf.putFloat(value); 1507 fail("Should throw Exception"); //$NON-NLS-1$ 1508 } catch (BufferOverflowException e) { 1509 // expected 1510 } 1511 1512 buf.order(ByteOrder.BIG_ENDIAN); 1513 } 1514 1515 public void testPutFloatint() { 1516 if (buf.isReadOnly()) { 1517 try { 1518 buf.putFloat(0, (float) 1); 1519 fail("Should throw Exception"); //$NON-NLS-1$ 1520 } catch (ReadOnlyBufferException e) { 1521 // expected 1522 } 1523 return; 1524 } 1525 1526 int nbytes = 4; 1527 byte bytes[] = new byte[nbytes]; 1528 float value = 0; 1529 buf.clear(); 1530 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1531 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1532 : ByteOrder.LITTLE_ENDIAN); 1533 value = (float) i; 1534 buf.position(i); 1535 buf.putFloat(i, value); 1536 assertEquals(i, buf.position()); 1537 buf.get(bytes); 1538 assertTrue(Arrays.equals(float2bytes(value, buf.order()), bytes)); 1539 } 1540 1541 try { 1542 buf.putFloat(-1, value); 1543 fail("Should throw Exception"); //$NON-NLS-1$ 1544 } catch (IndexOutOfBoundsException e) { 1545 // expected 1546 } 1547 try { 1548 buf.putFloat(buf.limit() - nbytes + 1, value); 1549 fail("Should throw Exception"); //$NON-NLS-1$ 1550 } catch (IndexOutOfBoundsException e) { 1551 // expected 1552 } 1553 1554 buf.order(ByteOrder.BIG_ENDIAN); 1555 } 1556 1557 public void testGetInt() { 1558 int nbytes = 4; 1559 byte bytes[] = new byte[nbytes]; 1560 int value; 1561 buf.clear(); 1562 for (int i = 0; buf.remaining() >= nbytes; i++) { 1563 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1564 : ByteOrder.LITTLE_ENDIAN); 1565 assertEquals(i * nbytes, buf.position()); 1566 buf.mark(); 1567 buf.get(bytes); 1568 buf.reset(); 1569 value = buf.getInt(); 1570 assertEquals(bytes2int(bytes, buf.order()), value); 1571 } 1572 1573 try { 1574 buf.getInt(); 1575 fail("Should throw Exception"); //$NON-NLS-1$ 1576 } catch (BufferUnderflowException e) { 1577 // expected 1578 } 1579 1580 buf.order(ByteOrder.BIG_ENDIAN); 1581 } 1582 1583 public void testGetIntint() { 1584 int nbytes = 4; 1585 byte bytes[] = new byte[nbytes]; 1586 int value; 1587 buf.clear(); 1588 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1589 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1590 : ByteOrder.LITTLE_ENDIAN); 1591 buf.position(i); 1592 value = buf.getInt(i); 1593 assertEquals(i, buf.position()); 1594 buf.get(bytes); 1595 assertEquals(bytes2int(bytes, buf.order()), value); 1596 } 1597 1598 try { 1599 buf.getInt(-1); 1600 fail("Should throw Exception"); //$NON-NLS-1$ 1601 } catch (IndexOutOfBoundsException e) { 1602 // expected 1603 } 1604 try { 1605 buf.getInt(buf.limit() - nbytes + 1); 1606 fail("Should throw Exception"); //$NON-NLS-1$ 1607 } catch (IndexOutOfBoundsException e) { 1608 // expected 1609 } 1610 1611 buf.order(ByteOrder.BIG_ENDIAN); 1612 try { 1613 ByteBuffer.allocateDirect(16).getInt(Integer.MAX_VALUE); 1614 } catch (IndexOutOfBoundsException e) { 1615 //expected 1616 } 1617 } 1618 1619 public void testPutInt() { 1620 if (buf.isReadOnly()) { 1621 try { 1622 buf.clear(); 1623 buf.putInt((int) 1); 1624 fail("Should throw Exception"); //$NON-NLS-1$ 1625 } catch (ReadOnlyBufferException e) { 1626 // expected 1627 } 1628 return; 1629 } 1630 1631 int nbytes = 4; 1632 byte bytes[] = new byte[nbytes]; 1633 int value = 0; 1634 buf.clear(); 1635 for (int i = 0; buf.remaining() >= nbytes; i++) { 1636 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1637 : ByteOrder.LITTLE_ENDIAN); 1638 value = (int) i; 1639 buf.mark(); 1640 buf.putInt(value); 1641 assertEquals((i + 1) * nbytes, buf.position()); 1642 buf.reset(); 1643 buf.get(bytes); 1644 assertTrue(Arrays.equals(int2bytes(value, buf.order()), bytes)); 1645 } 1646 1647 try { 1648 buf.putInt(value); 1649 fail("Should throw Exception"); //$NON-NLS-1$ 1650 } catch (BufferOverflowException e) { 1651 // expected 1652 } 1653 1654 buf.order(ByteOrder.BIG_ENDIAN); 1655 } 1656 1657 public void testPutIntint() { 1658 if (buf.isReadOnly()) { 1659 try { 1660 buf.putInt(0, (int) 1); 1661 fail("Should throw Exception"); //$NON-NLS-1$ 1662 } catch (ReadOnlyBufferException e) { 1663 // expected 1664 } 1665 return; 1666 } 1667 1668 int nbytes = 4; 1669 byte bytes[] = new byte[nbytes]; 1670 int value = 0; 1671 buf.clear(); 1672 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1673 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1674 : ByteOrder.LITTLE_ENDIAN); 1675 value = (int) i; 1676 buf.position(i); 1677 buf.putInt(i, value); 1678 assertEquals(i, buf.position()); 1679 buf.get(bytes); 1680 assertTrue(Arrays.equals(int2bytes(value, buf.order()), bytes)); 1681 } 1682 1683 try { 1684 buf.putInt(-1, value); 1685 fail("Should throw Exception"); //$NON-NLS-1$ 1686 } catch (IndexOutOfBoundsException e) { 1687 // expected 1688 } 1689 try { 1690 buf.putInt(buf.limit() - nbytes + 1, value); 1691 fail("Should throw Exception"); //$NON-NLS-1$ 1692 } catch (IndexOutOfBoundsException e) { 1693 // expected 1694 } 1695 1696 buf.order(ByteOrder.BIG_ENDIAN); 1697 } 1698 1699 public void testGetLong() { 1700 int nbytes = 8; 1701 byte bytes[] = new byte[nbytes]; 1702 long value; 1703 buf.clear(); 1704 for (int i = 0; buf.remaining() >= nbytes; i++) { 1705 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1706 : ByteOrder.LITTLE_ENDIAN); 1707 assertEquals(i * nbytes, buf.position()); 1708 buf.mark(); 1709 buf.get(bytes); 1710 buf.reset(); 1711 value = buf.getLong(); 1712 assertEquals(bytes2long(bytes, buf.order()), value); 1713 } 1714 1715 try { 1716 buf.getLong(); 1717 fail("Should throw Exception"); //$NON-NLS-1$ 1718 } catch (BufferUnderflowException e) { 1719 // expected 1720 } 1721 1722 buf.order(ByteOrder.BIG_ENDIAN); 1723 } 1724 1725 public void testGetLongint() { 1726 int nbytes = 8; 1727 byte bytes[] = new byte[nbytes]; 1728 long value; 1729 buf.clear(); 1730 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1731 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1732 : ByteOrder.LITTLE_ENDIAN); 1733 buf.position(i); 1734 value = buf.getLong(i); 1735 assertEquals(i, buf.position()); 1736 buf.get(bytes); 1737 assertEquals(bytes2long(bytes, buf.order()), value); 1738 } 1739 1740 try { 1741 buf.getLong(-1); 1742 fail("Should throw Exception"); //$NON-NLS-1$ 1743 } catch (IndexOutOfBoundsException e) { 1744 // expected 1745 } 1746 try { 1747 buf.getLong(buf.limit() - nbytes + 1); 1748 fail("Should throw Exception"); //$NON-NLS-1$ 1749 } catch (IndexOutOfBoundsException e) { 1750 // expected 1751 } 1752 1753 buf.order(ByteOrder.BIG_ENDIAN); 1754 } 1755 1756 public void testPutLong() { 1757 if (buf.isReadOnly()) { 1758 try { 1759 buf.clear(); 1760 buf.putLong((long) 1); 1761 fail("Should throw Exception"); //$NON-NLS-1$ 1762 } catch (ReadOnlyBufferException e) { 1763 // expected 1764 } 1765 return; 1766 } 1767 1768 int nbytes = 8; 1769 byte bytes[] = new byte[nbytes]; 1770 long value = 0; 1771 buf.clear(); 1772 for (int i = 0; buf.remaining() >= nbytes; i++) { 1773 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1774 : ByteOrder.LITTLE_ENDIAN); 1775 value = (long) i; 1776 buf.mark(); 1777 buf.putLong(value); 1778 assertEquals((i + 1) * nbytes, buf.position()); 1779 buf.reset(); 1780 buf.get(bytes); 1781 assertTrue(Arrays.equals(long2bytes(value, buf.order()), bytes)); 1782 } 1783 1784 try { 1785 buf.putLong(value); 1786 fail("Should throw Exception"); //$NON-NLS-1$ 1787 } catch (BufferOverflowException e) { 1788 // expected 1789 } 1790 1791 buf.order(ByteOrder.BIG_ENDIAN); 1792 } 1793 1794 public void testPutLongint() { 1795 if (buf.isReadOnly()) { 1796 try { 1797 buf.putLong(0, (long) 1); 1798 fail("Should throw Exception"); //$NON-NLS-1$ 1799 } catch (ReadOnlyBufferException e) { 1800 // expected 1801 } 1802 return; 1803 } 1804 1805 int nbytes = 8; 1806 byte bytes[] = new byte[nbytes]; 1807 long value = 0; 1808 buf.clear(); 1809 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1810 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1811 : ByteOrder.LITTLE_ENDIAN); 1812 value = (long) i; 1813 buf.position(i); 1814 buf.putLong(i, value); 1815 assertEquals(i, buf.position()); 1816 buf.get(bytes); 1817 assertTrue(Arrays.equals(long2bytes(value, buf.order()), bytes)); 1818 } 1819 1820 try { 1821 buf.putLong(-1, value); 1822 fail("Should throw Exception"); //$NON-NLS-1$ 1823 } catch (IndexOutOfBoundsException e) { 1824 // expected 1825 } 1826 try { 1827 buf.putLong(buf.limit() - nbytes + 1, value); 1828 fail("Should throw Exception"); //$NON-NLS-1$ 1829 } catch (IndexOutOfBoundsException e) { 1830 // expected 1831 } 1832 1833 buf.order(ByteOrder.BIG_ENDIAN); 1834 } 1835 1836 public void testGetShort() { 1837 int nbytes = 2; 1838 byte bytes[] = new byte[nbytes]; 1839 short value; 1840 buf.clear(); 1841 for (int i = 0; buf.remaining() >= nbytes; i++) { 1842 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1843 : ByteOrder.LITTLE_ENDIAN); 1844 assertEquals(i * nbytes, buf.position()); 1845 buf.mark(); 1846 buf.get(bytes); 1847 buf.reset(); 1848 value = buf.getShort(); 1849 assertEquals(bytes2short(bytes, buf.order()), value); 1850 } 1851 1852 try { 1853 buf.getShort(); 1854 fail("Should throw Exception"); //$NON-NLS-1$ 1855 } catch (BufferUnderflowException e) { 1856 // expected 1857 } 1858 1859 buf.order(ByteOrder.BIG_ENDIAN); 1860 } 1861 1862 public void testGetShortint() { 1863 int nbytes = 2; 1864 byte bytes[] = new byte[nbytes]; 1865 short value; 1866 buf.clear(); 1867 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1868 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1869 : ByteOrder.LITTLE_ENDIAN); 1870 buf.position(i); 1871 value = buf.getShort(i); 1872 assertEquals(i, buf.position()); 1873 buf.get(bytes); 1874 assertEquals(bytes2short(bytes, buf.order()), value); 1875 } 1876 1877 try { 1878 buf.getShort(-1); 1879 fail("Should throw Exception"); //$NON-NLS-1$ 1880 } catch (IndexOutOfBoundsException e) { 1881 // expected 1882 } 1883 try { 1884 buf.getShort(buf.limit() - nbytes + 1); 1885 fail("Should throw Exception"); //$NON-NLS-1$ 1886 } catch (IndexOutOfBoundsException e) { 1887 // expected 1888 } 1889 1890 buf.order(ByteOrder.BIG_ENDIAN); 1891 } 1892 1893 public void testPutShort() { 1894 if (buf.isReadOnly()) { 1895 try { 1896 buf.clear(); 1897 buf.putShort((short) 1); 1898 fail("Should throw Exception"); //$NON-NLS-1$ 1899 } catch (ReadOnlyBufferException e) { 1900 // expected 1901 } 1902 return; 1903 } 1904 1905 int nbytes = 2; 1906 byte bytes[] = new byte[nbytes]; 1907 short value = 0; 1908 buf.clear(); 1909 for (int i = 0; buf.remaining() >= nbytes; i++) { 1910 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1911 : ByteOrder.LITTLE_ENDIAN); 1912 value = (short) i; 1913 buf.mark(); 1914 buf.putShort(value); 1915 assertEquals((i + 1) * nbytes, buf.position()); 1916 buf.reset(); 1917 buf.get(bytes); 1918 assertTrue(Arrays.equals(short2bytes(value, buf.order()), bytes)); 1919 } 1920 1921 try { 1922 buf.putShort(value); 1923 fail("Should throw Exception"); //$NON-NLS-1$ 1924 } catch (BufferOverflowException e) { 1925 // expected 1926 } 1927 1928 buf.order(ByteOrder.BIG_ENDIAN); 1929 } 1930 1931 public void testPutShortint() { 1932 if (buf.isReadOnly()) { 1933 try { 1934 buf.putShort(0, (short) 1); 1935 fail("Should throw Exception"); //$NON-NLS-1$ 1936 } catch (ReadOnlyBufferException e) { 1937 // expected 1938 } 1939 return; 1940 } 1941 1942 int nbytes = 2; 1943 byte bytes[] = new byte[nbytes]; 1944 short value = 0; 1945 buf.clear(); 1946 for (int i = 0; i <= buf.limit() - nbytes; i++) { 1947 buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN 1948 : ByteOrder.LITTLE_ENDIAN); 1949 value = (short) i; 1950 buf.position(i); 1951 buf.putShort(i, value); 1952 assertEquals(i, buf.position()); 1953 buf.get(bytes); 1954 assertTrue(Arrays.equals(short2bytes(value, buf.order()), bytes)); 1955 } 1956 1957 try { 1958 buf.putShort(-1, value); 1959 fail("Should throw Exception"); //$NON-NLS-1$ 1960 } catch (IndexOutOfBoundsException e) { 1961 // expected 1962 } 1963 try { 1964 buf.putShort(buf.limit() - nbytes + 1, value); 1965 fail("Should throw Exception"); //$NON-NLS-1$ 1966 } catch (IndexOutOfBoundsException e) { 1967 // expected 1968 } 1969 1970 buf.order(ByteOrder.BIG_ENDIAN); 1971 } 1972 1973 /** 1974 * @tests java.nio.ByteBuffer.wrap(byte[],int,int) 1975 */ 1976 public void testWrappedByteBuffer_null_array() { 1977 // Regression for HARMONY-264 1978 byte array[] = null; 1979 try { 1980 ByteBuffer.wrap(array, -1, 0); 1981 fail("Should throw NPE"); //$NON-NLS-1$ 1982 } catch (NullPointerException e) { 1983 } 1984 try { 1985 ByteBuffer.wrap(new byte[10], Integer.MAX_VALUE, 2); 1986 fail("Should throw IndexOutOfBoundsException"); //$NON-NLS-1$ 1987 } catch (IndexOutOfBoundsException e) { 1988 } 1989 } 1990 1991 private void loadTestData1(byte array[], int offset, int length) { 1992 for (int i = 0; i < length; i++) { 1993 array[offset + i] = (byte) i; 1994 } 1995 } 1996 1997 private void loadTestData2(byte array[], int offset, int length) { 1998 for (int i = 0; i < length; i++) { 1999 array[offset + i] = (byte) (length - i); 2000 } 2001 } 2002 2003 private void loadTestData1(ByteBuffer buf) { 2004 buf.clear(); 2005 for (int i = 0; i < buf.capacity(); i++) { 2006 buf.put(i, (byte) i); 2007 } 2008 } 2009 2010 private void loadTestData2(ByteBuffer buf) { 2011 buf.clear(); 2012 for (int i = 0; i < buf.capacity(); i++) { 2013 buf.put(i, (byte) (buf.capacity() - i)); 2014 } 2015 } 2016 2017 private void assertContentEquals(ByteBuffer buf, byte array[], 2018 int offset, int length) { 2019 for (int i = 0; i < length; i++) { 2020 assertEquals(buf.get(i), array[offset + i]); 2021 } 2022 } 2023 2024 private void assertContentEquals(ByteBuffer buf, ByteBuffer other) { 2025 assertEquals(buf.capacity(), other.capacity()); 2026 for (int i = 0; i < buf.capacity(); i++) { 2027 assertEquals(buf.get(i), other.get(i)); 2028 } 2029 } 2030 2031 private void assertContentLikeTestData1(ByteBuffer buf, 2032 int startIndex, byte startValue, int length) { 2033 byte value = startValue; 2034 for (int i = 0; i < length; i++) { 2035 assertEquals(buf.get(startIndex + i), value); 2036 value = (byte) (value + 1); 2037 } 2038 } 2039 2040 private int bytes2int(byte bytes[], ByteOrder order) { 2041 int nbytes = 4, bigHead, step; 2042 if (order == ByteOrder.BIG_ENDIAN) { 2043 bigHead = 0; 2044 step = 1; 2045 } else { 2046 bigHead = nbytes - 1; 2047 step = -1; 2048 } 2049 int result = 0; 2050 int p = bigHead; 2051 for (int i = 0; i < nbytes; i++) { 2052 result = result << 8; 2053 result = result | (bytes[p] & 0xff); 2054 p += step; 2055 } 2056 return result; 2057 } 2058 2059 private long bytes2long(byte bytes[], ByteOrder order) { 2060 int nbytes = 8, bigHead, step; 2061 if (order == ByteOrder.BIG_ENDIAN) { 2062 bigHead = 0; 2063 step = 1; 2064 } else { 2065 bigHead = nbytes - 1; 2066 step = -1; 2067 } 2068 long result = 0; 2069 int p = bigHead; 2070 for (int i = 0; i < nbytes; i++) { 2071 result = result << 8; 2072 result = result | (bytes[p] & 0xff); 2073 p += step; 2074 } 2075 return result; 2076 } 2077 2078 private short bytes2short(byte bytes[], ByteOrder order) { 2079 int nbytes = 2, bigHead, step; 2080 if (order == ByteOrder.BIG_ENDIAN) { 2081 bigHead = 0; 2082 step = 1; 2083 } else { 2084 bigHead = nbytes - 1; 2085 step = -1; 2086 } 2087 short result = 0; 2088 int p = bigHead; 2089 for (int i = 0; i < nbytes; i++) { 2090 result = (short) (result << 8); 2091 result = (short) (result | (bytes[p] & 0xff)); 2092 p += step; 2093 } 2094 return result; 2095 } 2096 2097 private char bytes2char(byte bytes[], ByteOrder order) { 2098 return (char) bytes2short(bytes, order); 2099 } 2100 2101 private float bytes2float(byte bytes[], ByteOrder order) { 2102 return Float.intBitsToFloat(bytes2int(bytes, order)); 2103 } 2104 2105 private double bytes2double(byte bytes[], ByteOrder order) { 2106 return Double.longBitsToDouble(bytes2long(bytes, order)); 2107 } 2108 2109 private byte[] int2bytes(int value, ByteOrder order) { 2110 int nbytes = 4, smallHead, step; 2111 if (order == ByteOrder.BIG_ENDIAN) { 2112 smallHead = nbytes - 1; 2113 step = -1; 2114 } else { 2115 smallHead = 0; 2116 step = 1; 2117 } 2118 byte bytes[] = new byte[nbytes]; 2119 int p = smallHead; 2120 for (int i = 0; i < nbytes; i++) { 2121 bytes[p] = (byte) (value & 0xff); 2122 value = value >> 8; 2123 p += step; 2124 } 2125 return bytes; 2126 } 2127 2128 private byte[] long2bytes(long value, ByteOrder order) { 2129 int nbytes = 8, smallHead, step; 2130 if (order == ByteOrder.BIG_ENDIAN) { 2131 smallHead = nbytes - 1; 2132 step = -1; 2133 } else { 2134 smallHead = 0; 2135 step = 1; 2136 } 2137 byte bytes[] = new byte[nbytes]; 2138 int p = smallHead; 2139 for (int i = 0; i < nbytes; i++) { 2140 bytes[p] = (byte) (value & 0xff); 2141 value = value >> 8; 2142 p += step; 2143 } 2144 return bytes; 2145 } 2146 2147 private byte[] short2bytes(short value, ByteOrder order) { 2148 int nbytes = 2, smallHead, step; 2149 if (order == ByteOrder.BIG_ENDIAN) { 2150 smallHead = nbytes - 1; 2151 step = -1; 2152 } else { 2153 smallHead = 0; 2154 step = 1; 2155 } 2156 byte bytes[] = new byte[nbytes]; 2157 int p = smallHead; 2158 for (int i = 0; i < nbytes; i++) { 2159 bytes[p] = (byte) (value & 0xff); 2160 value = (short) (value >> 8); 2161 p += step; 2162 } 2163 return bytes; 2164 } 2165 2166 private byte[] char2bytes(char value, ByteOrder order) { 2167 return short2bytes((short) value, order); 2168 } 2169 2170 private byte[] float2bytes(float value, ByteOrder order) { 2171 return int2bytes(Float.floatToRawIntBits(value), order); 2172 } 2173 2174 private byte[] double2bytes(double value, ByteOrder order) { 2175 return long2bytes(Double.doubleToRawLongBits(value), order); 2176 } 2177} 2178