1 7 8 10 package java.nio; 11 12 import sun.misc.Cleaner; 13 import sun.misc.Unsafe; 14 import sun.nio.ch.DirectBuffer; 15 import sun.nio.ch.FileChannelImpl; 16 17 18 class DirectByteBufferR 19 20 21 22 extends DirectByteBuffer 23 24 implements DirectBuffer 25 { 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 DirectByteBufferR(int cap) { 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 super(cap); 114 115 } 116 117 118 119 120 121 122 123 124 125 126 127 128 129 protected DirectByteBufferR(int cap, long addr, Runnable unmapper) { 132 133 134 135 136 137 138 super(cap, addr, unmapper); 139 140 } 141 142 143 144 DirectByteBufferR(DirectBuffer db, int mark, int pos, int lim, int cap, 148 int off) 149 { 150 151 152 153 154 155 156 157 158 super(db, mark, pos, lim, cap, off); 159 160 } 161 162 public ByteBuffer slice() { 163 int pos = this.position(); 164 int lim = this.limit(); 165 assert (pos <= lim); 166 int rem = (pos <= lim ? lim - pos : 0); 167 int off = (pos << 0); 168 assert (off >= 0); 169 return new DirectByteBufferR (this, -1, 0, rem, rem, off); 170 } 171 172 public ByteBuffer duplicate() { 173 return new DirectByteBufferR (this, 174 this.markValue(), 175 this.position(), 176 this.limit(), 177 this.capacity(), 178 0); 179 } 180 181 public ByteBuffer asReadOnlyBuffer() { 182 183 184 185 186 187 188 189 190 return duplicate(); 191 192 } 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 public ByteBuffer put(byte x) { 244 245 246 247 248 throw new ReadOnlyBufferException (); 249 250 } 251 252 public ByteBuffer put(int i, byte x) { 253 254 255 256 257 throw new ReadOnlyBufferException (); 258 259 } 260 261 public ByteBuffer put(ByteBuffer src) { 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 throw new ReadOnlyBufferException (); 299 300 } 301 302 public ByteBuffer put(byte[] src, int offset, int length) { 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 throw new ReadOnlyBufferException (); 326 327 } 328 329 public ByteBuffer compact() { 330 331 332 333 334 335 336 337 338 339 340 341 throw new ReadOnlyBufferException (); 342 343 } 344 345 public boolean isDirect() { 346 return true; 347 } 348 349 public boolean isReadOnly() { 350 return true; 351 } 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 byte _get(int i) { return unsafe.getByte(address + i); 416 } 417 418 void _put(int i, byte b) { 420 421 422 throw new ReadOnlyBufferException (); 423 424 } 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 private ByteBuffer putChar(long a, char x) { 448 449 450 451 452 453 454 455 456 457 throw new ReadOnlyBufferException (); 458 459 } 460 461 public ByteBuffer putChar(char x) { 462 463 464 465 466 throw new ReadOnlyBufferException (); 467 468 } 469 470 public ByteBuffer putChar(int i, char x) { 471 472 473 474 475 throw new ReadOnlyBufferException (); 476 477 } 478 479 public CharBuffer asCharBuffer() { 480 int off = this.position(); 481 int lim = this.limit(); 482 assert (off <= lim); 483 int rem = (off <= lim ? lim - off : 0); 484 485 int size = rem >> 1; 486 if (!unaligned && ((address + off) % (1 << 1) != 0)) { 487 return (bigEndian 488 ? (CharBuffer )(new ByteBufferAsCharBufferRB (this, 489 -1, 490 0, 491 size, 492 size, 493 off)) 494 : (CharBuffer )(new ByteBufferAsCharBufferRL (this, 495 -1, 496 0, 497 size, 498 size, 499 off))); 500 } else { 501 return (nativeByteOrder 502 ? (CharBuffer )(new DirectCharBufferRU (this, 503 -1, 504 0, 505 size, 506 size, 507 off)) 508 : (CharBuffer )(new DirectCharBufferRS (this, 509 -1, 510 0, 511 size, 512 size, 513 off))); 514 } 515 } 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 private ByteBuffer putShort(long a, short x) { 539 540 541 542 543 544 545 546 547 548 throw new ReadOnlyBufferException (); 549 550 } 551 552 public ByteBuffer putShort(short x) { 553 554 555 556 557 throw new ReadOnlyBufferException (); 558 559 } 560 561 public ByteBuffer putShort(int i, short x) { 562 563 564 565 566 throw new ReadOnlyBufferException (); 567 568 } 569 570 public ShortBuffer asShortBuffer() { 571 int off = this.position(); 572 int lim = this.limit(); 573 assert (off <= lim); 574 int rem = (off <= lim ? lim - off : 0); 575 576 int size = rem >> 1; 577 if (!unaligned && ((address + off) % (1 << 1) != 0)) { 578 return (bigEndian 579 ? (ShortBuffer )(new ByteBufferAsShortBufferRB (this, 580 -1, 581 0, 582 size, 583 size, 584 off)) 585 : (ShortBuffer )(new ByteBufferAsShortBufferRL (this, 586 -1, 587 0, 588 size, 589 size, 590 off))); 591 } else { 592 return (nativeByteOrder 593 ? (ShortBuffer )(new DirectShortBufferRU (this, 594 -1, 595 0, 596 size, 597 size, 598 off)) 599 : (ShortBuffer )(new DirectShortBufferRS (this, 600 -1, 601 0, 602 size, 603 size, 604 off))); 605 } 606 } 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 private ByteBuffer putInt(long a, int x) { 630 631 632 633 634 635 636 637 638 639 throw new ReadOnlyBufferException (); 640 641 } 642 643 public ByteBuffer putInt(int x) { 644 645 646 647 648 throw new ReadOnlyBufferException (); 649 650 } 651 652 public ByteBuffer putInt(int i, int x) { 653 654 655 656 657 throw new ReadOnlyBufferException (); 658 659 } 660 661 public IntBuffer asIntBuffer() { 662 int off = this.position(); 663 int lim = this.limit(); 664 assert (off <= lim); 665 int rem = (off <= lim ? lim - off : 0); 666 667 int size = rem >> 2; 668 if (!unaligned && ((address + off) % (1 << 2) != 0)) { 669 return (bigEndian 670 ? (IntBuffer )(new ByteBufferAsIntBufferRB (this, 671 -1, 672 0, 673 size, 674 size, 675 off)) 676 : (IntBuffer )(new ByteBufferAsIntBufferRL (this, 677 -1, 678 0, 679 size, 680 size, 681 off))); 682 } else { 683 return (nativeByteOrder 684 ? (IntBuffer )(new DirectIntBufferRU (this, 685 -1, 686 0, 687 size, 688 size, 689 off)) 690 : (IntBuffer )(new DirectIntBufferRS (this, 691 -1, 692 0, 693 size, 694 size, 695 off))); 696 } 697 } 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 private ByteBuffer putLong(long a, long x) { 721 722 723 724 725 726 727 728 729 730 throw new ReadOnlyBufferException (); 731 732 } 733 734 public ByteBuffer putLong(long x) { 735 736 737 738 739 throw new ReadOnlyBufferException (); 740 741 } 742 743 public ByteBuffer putLong(int i, long x) { 744 745 746 747 748 throw new ReadOnlyBufferException (); 749 750 } 751 752 public LongBuffer asLongBuffer() { 753 int off = this.position(); 754 int lim = this.limit(); 755 assert (off <= lim); 756 int rem = (off <= lim ? lim - off : 0); 757 758 int size = rem >> 3; 759 if (!unaligned && ((address + off) % (1 << 3) != 0)) { 760 return (bigEndian 761 ? (LongBuffer )(new ByteBufferAsLongBufferRB (this, 762 -1, 763 0, 764 size, 765 size, 766 off)) 767 : (LongBuffer )(new ByteBufferAsLongBufferRL (this, 768 -1, 769 0, 770 size, 771 size, 772 off))); 773 } else { 774 return (nativeByteOrder 775 ? (LongBuffer )(new DirectLongBufferRU (this, 776 -1, 777 0, 778 size, 779 size, 780 off)) 781 : (LongBuffer )(new DirectLongBufferRS (this, 782 -1, 783 0, 784 size, 785 size, 786 off))); 787 } 788 } 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 private ByteBuffer putFloat(long a, float x) { 812 813 814 815 816 817 818 819 820 821 throw new ReadOnlyBufferException (); 822 823 } 824 825 public ByteBuffer putFloat(float x) { 826 827 828 829 830 throw new ReadOnlyBufferException (); 831 832 } 833 834 public ByteBuffer putFloat(int i, float x) { 835 836 837 838 839 throw new ReadOnlyBufferException (); 840 841 } 842 843 public FloatBuffer asFloatBuffer() { 844 int off = this.position(); 845 int lim = this.limit(); 846 assert (off <= lim); 847 int rem = (off <= lim ? lim - off : 0); 848 849 int size = rem >> 2; 850 if (!unaligned && ((address + off) % (1 << 2) != 0)) { 851 return (bigEndian 852 ? (FloatBuffer )(new ByteBufferAsFloatBufferRB (this, 853 -1, 854 0, 855 size, 856 size, 857 off)) 858 : (FloatBuffer )(new ByteBufferAsFloatBufferRL (this, 859 -1, 860 0, 861 size, 862 size, 863 off))); 864 } else { 865 return (nativeByteOrder 866 ? (FloatBuffer )(new DirectFloatBufferRU (this, 867 -1, 868 0, 869 size, 870 size, 871 off)) 872 : (FloatBuffer )(new DirectFloatBufferRS (this, 873 -1, 874 0, 875 size, 876 size, 877 off))); 878 } 879 } 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 private ByteBuffer putDouble(long a, double x) { 903 904 905 906 907 908 909 910 911 912 throw new ReadOnlyBufferException (); 913 914 } 915 916 public ByteBuffer putDouble(double x) { 917 918 919 920 921 throw new ReadOnlyBufferException (); 922 923 } 924 925 public ByteBuffer putDouble(int i, double x) { 926 927 928 929 930 throw new ReadOnlyBufferException (); 931 932 } 933 934 public DoubleBuffer asDoubleBuffer() { 935 int off = this.position(); 936 int lim = this.limit(); 937 assert (off <= lim); 938 int rem = (off <= lim ? lim - off : 0); 939 940 int size = rem >> 3; 941 if (!unaligned && ((address + off) % (1 << 3) != 0)) { 942 return (bigEndian 943 ? (DoubleBuffer )(new ByteBufferAsDoubleBufferRB (this, 944 -1, 945 0, 946 size, 947 size, 948 off)) 949 : (DoubleBuffer )(new ByteBufferAsDoubleBufferRL (this, 950 -1, 951 0, 952 size, 953 size, 954 off))); 955 } else { 956 return (nativeByteOrder 957 ? (DoubleBuffer )(new DirectDoubleBufferRU (this, 958 -1, 959 0, 960 size, 961 size, 962 off)) 963 : (DoubleBuffer )(new DirectDoubleBufferRS (this, 964 -1, 965 0, 966 size, 967 size, 968 off))); 969 } 970 } 971 972 } 973 | Popular Tags |