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 DirectByteBuffer 19 20 extends MappedByteBuffer 21 22 23 24 implements DirectBuffer 25 { 26 27 28 29 protected static final Unsafe unsafe = Bits.unsafe(); 31 32 protected static final boolean unaligned = Bits.unaligned(); 34 35 39 protected Object viewedBuffer = null; 42 43 public Object viewedBuffer() { 44 return viewedBuffer; 45 } 46 47 48 49 private static class Deallocator 50 implements Runnable 51 { 52 53 private static Unsafe unsafe = Unsafe.getUnsafe(); 54 55 private long address; 56 private int capacity; 57 58 private Deallocator(long address, int capacity) { 59 assert (address != 0); 60 this.address = address; 61 this.capacity = capacity; 62 } 63 64 public void run() { 65 if (address == 0) { 66 return; 68 } 69 unsafe.freeMemory(address); 70 address = 0; 71 Bits.unreserveMemory(capacity); 72 } 73 74 } 75 76 private final Cleaner cleaner; 77 78 public Cleaner cleaner() { return cleaner; } 79 80 81 82 83 84 85 86 87 88 89 90 DirectByteBuffer(int cap) { 94 super(-1, 0, cap, cap, false); 95 Bits.reserveMemory(cap); 96 int ps = Bits.pageSize(); 97 long base = 0; 98 try { 99 base = unsafe.allocateMemory(cap + ps); 100 } catch (OutOfMemoryError x) { 101 Bits.unreserveMemory(cap); 102 throw x; 103 } 104 unsafe.setMemory(base, cap + ps, (byte) 0); 105 if (base % ps != 0) { 106 address = base + ps - (base & (ps - 1)); 108 } else { 109 address = base; 110 } 111 cleaner = Cleaner.create(this, new Deallocator(base, cap)); 112 113 114 115 } 116 117 118 119 private DirectByteBuffer(long addr, int cap) { 122 super(-1, 0, cap, cap, false); 123 address = addr; 124 cleaner = null; 125 } 126 127 128 129 protected DirectByteBuffer(int cap, long addr, Runnable unmapper) { 132 133 super(-1, 0, cap, cap, true); 134 address = addr; 135 viewedBuffer = null; 136 cleaner = Cleaner.create(this, unmapper); 137 138 139 140 } 141 142 143 144 DirectByteBuffer(DirectBuffer db, int mark, int pos, int lim, int cap, 148 int off) 149 { 150 151 super(mark, pos, lim, cap); 152 address = db.address() + off; 153 viewedBuffer = db; 154 155 cleaner = null; 156 157 158 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 DirectByteBuffer (this, -1, 0, rem, rem, off); 170 } 171 172 public ByteBuffer duplicate() { 173 return new DirectByteBuffer (this, 174 this.markValue(), 175 this.position(), 176 this.limit(), 177 this.capacity(), 178 0); 179 } 180 181 public ByteBuffer asReadOnlyBuffer() { 182 183 return new DirectByteBufferR (this, 184 this.markValue(), 185 this.position(), 186 this.limit(), 187 this.capacity(), 188 0); 189 190 191 192 } 193 194 195 196 public long address() { 197 return address; 198 } 199 200 private long ix(int i) { 201 return address + (i << 0); 202 } 203 204 public byte get() { 205 return ((unsafe.getByte(ix(nextGetIndex())))); 206 } 207 208 public byte get(int i) { 209 return ((unsafe.getByte(ix(checkIndex(i))))); 210 } 211 212 public ByteBuffer get(byte[] dst, int offset, int length) { 213 214 if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { 215 checkBounds(offset, length, dst.length); 216 int pos = position(); 217 int lim = limit(); 218 assert (pos <= lim); 219 int rem = (pos <= lim ? lim - pos : 0); 220 if (length > rem) 221 throw new BufferUnderflowException (); 222 223 if (order() != ByteOrder.nativeOrder()) 224 Bits.copyToByteArray(ix(pos), dst, 225 offset << 0, 226 length << 0); 227 else 228 Bits.copyToByteArray(ix(pos), dst, 229 offset << 0, 230 length << 0); 231 position(pos + length); 232 } else { 233 super.get(dst, offset, length); 234 } 235 return this; 236 237 238 239 } 240 241 242 243 public ByteBuffer put(byte x) { 244 245 unsafe.putByte(ix(nextPutIndex()), ((x))); 246 return this; 247 248 249 250 } 251 252 public ByteBuffer put(int i, byte x) { 253 254 unsafe.putByte(ix(checkIndex(i)), ((x))); 255 return this; 256 257 258 259 } 260 261 public ByteBuffer put(ByteBuffer src) { 262 263 if (src instanceof DirectByteBuffer ) { 264 if (src == this) 265 throw new IllegalArgumentException (); 266 DirectByteBuffer sb = (DirectByteBuffer )src; 267 268 int spos = sb.position(); 269 int slim = sb.limit(); 270 assert (spos <= slim); 271 int srem = (spos <= slim ? slim - spos : 0); 272 273 int pos = position(); 274 int lim = limit(); 275 assert (pos <= lim); 276 int rem = (pos <= lim ? lim - pos : 0); 277 278 if (srem > rem) 279 throw new BufferOverflowException (); 280 unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 0); 281 sb.position(spos + srem); 282 position(pos + srem); 283 } else if (!src.isDirect()) { 284 285 int spos = src.position(); 286 int slim = src.limit(); 287 assert (spos <= slim); 288 int srem = (spos <= slim ? slim - spos : 0); 289 290 put(src.hb, src.offset + spos, srem); 291 src.position(spos + srem); 292 293 } else { 294 super.put(src); 295 } 296 return this; 297 298 299 300 } 301 302 public ByteBuffer put(byte[] src, int offset, int length) { 303 304 if ((length << 0) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { 305 checkBounds(offset, length, src.length); 306 int pos = position(); 307 int lim = limit(); 308 assert (pos <= lim); 309 int rem = (pos <= lim ? lim - pos : 0); 310 if (length > rem) 311 throw new BufferOverflowException (); 312 313 if (order() != ByteOrder.nativeOrder()) 314 Bits.copyFromByteArray(src, offset << 0, 315 ix(pos), length << 0); 316 else 317 Bits.copyFromByteArray(src, offset << 0, 318 ix(pos), length << 0); 319 position(pos + length); 320 } else { 321 super.put(src, offset, length); 322 } 323 return this; 324 325 326 327 } 328 329 public ByteBuffer compact() { 330 331 int pos = position(); 332 int lim = limit(); 333 assert (pos <= lim); 334 int rem = (pos <= lim ? lim - pos : 0); 335 336 unsafe.copyMemory(ix(pos), ix(0), rem << 0); 337 position(rem); 338 limit(capacity()); 339 return this; 340 341 342 343 } 344 345 public boolean isDirect() { 346 return true; 347 } 348 349 public boolean isReadOnly() { 350 return false; 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 unsafe.putByte(address + i, b); 421 422 423 424 } 425 426 427 428 429 private char getChar(long a) { 430 if (unaligned) { 431 char x = unsafe.getChar(a); 432 return (nativeByteOrder ? x : Bits.swap(x)); 433 } 434 return Bits.getChar(a, bigEndian); 435 } 436 437 public char getChar() { 438 return getChar(ix(nextGetIndex((1 << 1)))); 439 } 440 441 public char getChar(int i) { 442 return getChar(ix(checkIndex(i, (1 << 1)))); 443 } 444 445 446 447 private ByteBuffer putChar(long a, char x) { 448 449 if (unaligned) { 450 char y = (x); 451 unsafe.putChar(a, (nativeByteOrder ? y : Bits.swap(y))); 452 } else { 453 Bits.putChar(a, x, bigEndian); 454 } 455 return this; 456 457 458 459 } 460 461 public ByteBuffer putChar(char x) { 462 463 putChar(ix(nextPutIndex((1 << 1))), x); 464 return this; 465 466 467 468 } 469 470 public ByteBuffer putChar(int i, char x) { 471 472 putChar(ix(checkIndex(i, (1 << 1))), x); 473 return this; 474 475 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 ByteBufferAsCharBufferB (this, 489 -1, 490 0, 491 size, 492 size, 493 off)) 494 : (CharBuffer )(new ByteBufferAsCharBufferL (this, 495 -1, 496 0, 497 size, 498 size, 499 off))); 500 } else { 501 return (nativeByteOrder 502 ? (CharBuffer )(new DirectCharBufferU (this, 503 -1, 504 0, 505 size, 506 size, 507 off)) 508 : (CharBuffer )(new DirectCharBufferS (this, 509 -1, 510 0, 511 size, 512 size, 513 off))); 514 } 515 } 516 517 518 519 520 private short getShort(long a) { 521 if (unaligned) { 522 short x = unsafe.getShort(a); 523 return (nativeByteOrder ? x : Bits.swap(x)); 524 } 525 return Bits.getShort(a, bigEndian); 526 } 527 528 public short getShort() { 529 return getShort(ix(nextGetIndex((1 << 1)))); 530 } 531 532 public short getShort(int i) { 533 return getShort(ix(checkIndex(i, (1 << 1)))); 534 } 535 536 537 538 private ByteBuffer putShort(long a, short x) { 539 540 if (unaligned) { 541 short y = (x); 542 unsafe.putShort(a, (nativeByteOrder ? y : Bits.swap(y))); 543 } else { 544 Bits.putShort(a, x, bigEndian); 545 } 546 return this; 547 548 549 550 } 551 552 public ByteBuffer putShort(short x) { 553 554 putShort(ix(nextPutIndex((1 << 1))), x); 555 return this; 556 557 558 559 } 560 561 public ByteBuffer putShort(int i, short x) { 562 563 putShort(ix(checkIndex(i, (1 << 1))), x); 564 return this; 565 566 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 ByteBufferAsShortBufferB (this, 580 -1, 581 0, 582 size, 583 size, 584 off)) 585 : (ShortBuffer )(new ByteBufferAsShortBufferL (this, 586 -1, 587 0, 588 size, 589 size, 590 off))); 591 } else { 592 return (nativeByteOrder 593 ? (ShortBuffer )(new DirectShortBufferU (this, 594 -1, 595 0, 596 size, 597 size, 598 off)) 599 : (ShortBuffer )(new DirectShortBufferS (this, 600 -1, 601 0, 602 size, 603 size, 604 off))); 605 } 606 } 607 608 609 610 611 private int getInt(long a) { 612 if (unaligned) { 613 int x = unsafe.getInt(a); 614 return (nativeByteOrder ? x : Bits.swap(x)); 615 } 616 return Bits.getInt(a, bigEndian); 617 } 618 619 public int getInt() { 620 return getInt(ix(nextGetIndex((1 << 2)))); 621 } 622 623 public int getInt(int i) { 624 return getInt(ix(checkIndex(i, (1 << 2)))); 625 } 626 627 628 629 private ByteBuffer putInt(long a, int x) { 630 631 if (unaligned) { 632 int y = (x); 633 unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y))); 634 } else { 635 Bits.putInt(a, x, bigEndian); 636 } 637 return this; 638 639 640 641 } 642 643 public ByteBuffer putInt(int x) { 644 645 putInt(ix(nextPutIndex((1 << 2))), x); 646 return this; 647 648 649 650 } 651 652 public ByteBuffer putInt(int i, int x) { 653 654 putInt(ix(checkIndex(i, (1 << 2))), x); 655 return this; 656 657 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 ByteBufferAsIntBufferB (this, 671 -1, 672 0, 673 size, 674 size, 675 off)) 676 : (IntBuffer )(new ByteBufferAsIntBufferL (this, 677 -1, 678 0, 679 size, 680 size, 681 off))); 682 } else { 683 return (nativeByteOrder 684 ? (IntBuffer )(new DirectIntBufferU (this, 685 -1, 686 0, 687 size, 688 size, 689 off)) 690 : (IntBuffer )(new DirectIntBufferS (this, 691 -1, 692 0, 693 size, 694 size, 695 off))); 696 } 697 } 698 699 700 701 702 private long getLong(long a) { 703 if (unaligned) { 704 long x = unsafe.getLong(a); 705 return (nativeByteOrder ? x : Bits.swap(x)); 706 } 707 return Bits.getLong(a, bigEndian); 708 } 709 710 public long getLong() { 711 return getLong(ix(nextGetIndex((1 << 3)))); 712 } 713 714 public long getLong(int i) { 715 return getLong(ix(checkIndex(i, (1 << 3)))); 716 } 717 718 719 720 private ByteBuffer putLong(long a, long x) { 721 722 if (unaligned) { 723 long y = (x); 724 unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y))); 725 } else { 726 Bits.putLong(a, x, bigEndian); 727 } 728 return this; 729 730 731 732 } 733 734 public ByteBuffer putLong(long x) { 735 736 putLong(ix(nextPutIndex((1 << 3))), x); 737 return this; 738 739 740 741 } 742 743 public ByteBuffer putLong(int i, long x) { 744 745 putLong(ix(checkIndex(i, (1 << 3))), x); 746 return this; 747 748 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 ByteBufferAsLongBufferB (this, 762 -1, 763 0, 764 size, 765 size, 766 off)) 767 : (LongBuffer )(new ByteBufferAsLongBufferL (this, 768 -1, 769 0, 770 size, 771 size, 772 off))); 773 } else { 774 return (nativeByteOrder 775 ? (LongBuffer )(new DirectLongBufferU (this, 776 -1, 777 0, 778 size, 779 size, 780 off)) 781 : (LongBuffer )(new DirectLongBufferS (this, 782 -1, 783 0, 784 size, 785 size, 786 off))); 787 } 788 } 789 790 791 792 793 private float getFloat(long a) { 794 if (unaligned) { 795 int x = unsafe.getInt(a); 796 return Float.intBitsToFloat(nativeByteOrder ? x : Bits.swap(x)); 797 } 798 return Bits.getFloat(a, bigEndian); 799 } 800 801 public float getFloat() { 802 return getFloat(ix(nextGetIndex((1 << 2)))); 803 } 804 805 public float getFloat(int i) { 806 return getFloat(ix(checkIndex(i, (1 << 2)))); 807 } 808 809 810 811 private ByteBuffer putFloat(long a, float x) { 812 813 if (unaligned) { 814 int y = Float.floatToRawIntBits(x); 815 unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y))); 816 } else { 817 Bits.putFloat(a, x, bigEndian); 818 } 819 return this; 820 821 822 823 } 824 825 public ByteBuffer putFloat(float x) { 826 827 putFloat(ix(nextPutIndex((1 << 2))), x); 828 return this; 829 830 831 832 } 833 834 public ByteBuffer putFloat(int i, float x) { 835 836 putFloat(ix(checkIndex(i, (1 << 2))), x); 837 return this; 838 839 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 ByteBufferAsFloatBufferB (this, 853 -1, 854 0, 855 size, 856 size, 857 off)) 858 : (FloatBuffer )(new ByteBufferAsFloatBufferL (this, 859 -1, 860 0, 861 size, 862 size, 863 off))); 864 } else { 865 return (nativeByteOrder 866 ? (FloatBuffer )(new DirectFloatBufferU (this, 867 -1, 868 0, 869 size, 870 size, 871 off)) 872 : (FloatBuffer )(new DirectFloatBufferS (this, 873 -1, 874 0, 875 size, 876 size, 877 off))); 878 } 879 } 880 881 882 883 884 private double getDouble(long a) { 885 if (unaligned) { 886 long x = unsafe.getLong(a); 887 return Double.longBitsToDouble(nativeByteOrder ? x : Bits.swap(x)); 888 } 889 return Bits.getDouble(a, bigEndian); 890 } 891 892 public double getDouble() { 893 return getDouble(ix(nextGetIndex((1 << 3)))); 894 } 895 896 public double getDouble(int i) { 897 return getDouble(ix(checkIndex(i, (1 << 3)))); 898 } 899 900 901 902 private ByteBuffer putDouble(long a, double x) { 903 904 if (unaligned) { 905 long y = Double.doubleToRawLongBits(x); 906 unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y))); 907 } else { 908 Bits.putDouble(a, x, bigEndian); 909 } 910 return this; 911 912 913 914 } 915 916 public ByteBuffer putDouble(double x) { 917 918 putDouble(ix(nextPutIndex((1 << 3))), x); 919 return this; 920 921 922 923 } 924 925 public ByteBuffer putDouble(int i, double x) { 926 927 putDouble(ix(checkIndex(i, (1 << 3))), x); 928 return this; 929 930 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 ByteBufferAsDoubleBufferB (this, 944 -1, 945 0, 946 size, 947 size, 948 off)) 949 : (DoubleBuffer )(new ByteBufferAsDoubleBufferL (this, 950 -1, 951 0, 952 size, 953 size, 954 off))); 955 } else { 956 return (nativeByteOrder 957 ? (DoubleBuffer )(new DirectDoubleBufferU (this, 958 -1, 959 0, 960 size, 961 size, 962 off)) 963 : (DoubleBuffer )(new DirectDoubleBufferS (this, 964 -1, 965 0, 966 size, 967 size, 968 off))); 969 } 970 } 971 972 } 973 | Popular Tags |