1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 // -- This file was mechanically generated: Do not edit! -- // 28 // Android-note: This file is generated by ojluni/src/tools/gensrc_android.sh. 29 30 package java.nio; 31 32 33 34 35 import java.lang.ref.Reference; 36 37 38 39 40 41 42 import java.util.Objects; 43 import jdk.internal.misc.Unsafe; 44 import jdk.internal.util.ArraysSupport; 45 import libcore.io.Memory; 46 import dalvik.annotation.codegen.CovariantReturnType; 47 48 // Android-changed: Fix that if[byte] isn't processed by the SppTool. Upstream doc has the same bug. 49 /** 50 * A long buffer. 51 * 52 * <p> This class defines four categories of operations upon 53 * long buffers: 54 * 55 * <ul> 56 * 57 * <li><p> Absolute and relative {@link #get() <i>get</i>} and 58 * {@link #put(long) <i>put</i>} methods that read and write 59 * single longs; </p></li> 60 * 61 * <li><p> Absolute and relative {@link #get(long[]) <i>bulk get</i>} 62 * methods that transfer contiguous sequences of longs from this buffer 63 * into an array; and</p></li> 64 * 65 * <li><p> Absolute and relative {@link #put(long[]) <i>bulk put</i>} 66 * methods that transfer contiguous sequences of longs from a 67 * long array or some other long 68 * buffer into this buffer; and </p></li> 69 * 70 71 72 73 74 75 76 77 78 79 80 81 82 * 83 * <li><p> A method for {@link #compact compacting} 84 * a long buffer. </p></li> 85 * 86 * </ul> 87 * 88 * <p> Long buffers can be created either by {@link #allocate 89 * <i>allocation</i>}, which allocates space for the buffer's 90 * 91 92 93 94 95 96 97 * 98 * content, by {@link #wrap(long[]) <i>wrapping</i>} an existing 99 * long array into a buffer, or by creating a 100 * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer. 101 * 102 103 * 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 * 203 204 * 205 * <p> Like a byte buffer, a long buffer is either <a 206 * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A 207 * long buffer created via the {@code wrap} methods of this class will 208 * be non-direct. A long buffer created as a view of a byte buffer will 209 * be direct if, and only if, the byte buffer itself is direct. Whether or not 210 * a long buffer is direct may be determined by invoking the {@link 211 * #isDirect isDirect} method. </p> 212 * 213 214 * 215 216 217 218 219 220 221 222 223 224 225 * 226 227 228 229 * 230 * <p> Methods in this class that do not otherwise have a value to return are 231 * specified to return the buffer upon which they are invoked. This allows 232 * method invocations to be chained. 233 * 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 * 266 * 267 * @author Mark Reinhold 268 * @author JSR-51 Expert Group 269 * @since 1.4 270 */ 271 272 public abstract class LongBuffer 273 extends Buffer 274 implements Comparable<LongBuffer> 275 { 276 // Cached array base offset 277 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(long[].class); 278 279 // These fields are declared here rather than in Heap-X-Buffer in order to 280 // reduce the number of virtual method invocations needed to access these 281 // values, which is especially costly when coding small buffers. 282 // 283 final long[] hb; // Non-null only for heap buffers 284 final int offset; 285 boolean isReadOnly; 286 287 // Android-added: Added ELEMENT_SIZE_SHIFT for NIOAccess class and @UnsupportedAppUsage. 288 private static final int ELEMENT_SIZE_SHIFT = 3; 289 290 // Creates a new buffer with the given mark, position, limit, capacity, 291 // backing array, and array offset 292 // 293 // Android-removed: Removed MemorySegmentProxy to be supported yet./ LongBuffer(int mark, int pos, int lim, int cap, long[] hb, int offset)294 LongBuffer(int mark, int pos, int lim, int cap, // package-private 295 long[] hb, int offset) 296 { 297 // Android-added: elementSizeShift parameter (log2 of element size). 298 super(mark, pos, lim, cap, ELEMENT_SIZE_SHIFT); 299 this.hb = hb; 300 this.offset = offset; 301 } 302 303 // Creates a new buffer with the given mark, position, limit, and capacity 304 // LongBuffer(int mark, int pos, int lim, int cap)305 LongBuffer(int mark, int pos, int lim, int cap) { // package-private 306 this(mark, pos, lim, cap, null, 0); 307 } 308 309 // Android-removed: Unused constructor. 310 /* 311 // Creates a new buffer with given base, address and capacity 312 // 313 LongBuffer(long[] hb, long addr, int cap) { // package-private 314 super(addr, cap); 315 this.hb = hb; 316 this.offset = 0; 317 } 318 */ 319 320 @Override base()321 Object base() { 322 return hb; 323 } 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 /** 354 * Allocates a new long buffer. 355 * 356 * <p> The new buffer's position will be zero, its limit will be its 357 * capacity, its mark will be undefined, each of its elements will be 358 * initialized to zero, and its byte order will be 359 360 361 362 * the {@link ByteOrder#nativeOrder native order} of the underlying 363 * hardware. 364 365 * It will have a {@link #array backing array}, and its 366 * {@link #arrayOffset array offset} will be zero. 367 * 368 * @param capacity 369 * The new buffer's capacity, in longs 370 * 371 * @return The new long buffer 372 * 373 * @throws IllegalArgumentException 374 * If the {@code capacity} is a negative integer 375 */ allocate(int capacity)376 public static LongBuffer allocate(int capacity) { 377 if (capacity < 0) 378 throw createCapacityException(capacity); 379 // Android-removed: Removed MemorySegmentProxy not supported yet. 380 return new HeapLongBuffer(capacity, capacity); 381 } 382 383 /** 384 * Wraps a long array into a buffer. 385 * 386 * <p> The new buffer will be backed by the given long array; 387 * that is, modifications to the buffer will cause the array to be modified 388 * and vice versa. The new buffer's capacity will be 389 * {@code array.length}, its position will be {@code offset}, its limit 390 * will be {@code offset + length}, its mark will be undefined, and its 391 * byte order will be 392 393 394 395 * the {@link ByteOrder#nativeOrder native order} of the underlying 396 * hardware. 397 398 * Its {@link #array backing array} will be the given array, and 399 * its {@link #arrayOffset array offset} will be zero. </p> 400 * 401 * @param array 402 * The array that will back the new buffer 403 * 404 * @param offset 405 * The offset of the subarray to be used; must be non-negative and 406 * no larger than {@code array.length}. The new buffer's position 407 * will be set to this value. 408 * 409 * @param length 410 * The length of the subarray to be used; 411 * must be non-negative and no larger than 412 * {@code array.length - offset}. 413 * The new buffer's limit will be set to {@code offset + length}. 414 * 415 * @return The new long buffer 416 * 417 * @throws IndexOutOfBoundsException 418 * If the preconditions on the {@code offset} and {@code length} 419 * parameters do not hold 420 */ wrap(long[] array, int offset, int length)421 public static LongBuffer wrap(long[] array, 422 int offset, int length) 423 { 424 try { 425 // Android-removed: Removed MemorySegmentProxy not supported yet. 426 return new HeapLongBuffer(array, offset, length); 427 } catch (IllegalArgumentException x) { 428 throw new IndexOutOfBoundsException(); 429 } 430 } 431 432 /** 433 * Wraps a long array into a buffer. 434 * 435 * <p> The new buffer will be backed by the given long array; 436 * that is, modifications to the buffer will cause the array to be modified 437 * and vice versa. The new buffer's capacity and limit will be 438 * {@code array.length}, its position will be zero, its mark will be 439 * undefined, and its byte order will be 440 441 442 443 * the {@link ByteOrder#nativeOrder native order} of the underlying 444 * hardware. 445 446 * Its {@link #array backing array} will be the given array, and its 447 * {@link #arrayOffset array offset} will be zero. </p> 448 * 449 * @param array 450 * The array that will back this buffer 451 * 452 * @return The new long buffer 453 */ wrap(long[] array)454 public static LongBuffer wrap(long[] array) { 455 return wrap(array, 0, array.length); 456 } 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 /** 562 * Creates a new long buffer whose content is a shared subsequence of 563 * this buffer's content. 564 * 565 * <p> The content of the new buffer will start at this buffer's current 566 * position. Changes to this buffer's content will be visible in the new 567 * buffer, and vice versa; the two buffers' position, limit, and mark 568 * values will be independent. 569 * 570 * <p> The new buffer's position will be zero, its capacity and its limit 571 * will be the number of longs remaining in this buffer, its mark will be 572 * undefined, and its byte order will be 573 574 575 576 * identical to that of this buffer. 577 578 * The new buffer will be direct if, and only if, this buffer is direct, and 579 * it will be read-only if, and only if, this buffer is read-only. </p> 580 * 581 * @return The new long buffer 582 583 584 585 586 */ 587 @Override slice()588 public abstract LongBuffer slice(); 589 590 /** 591 * Creates a new long buffer whose content is a shared subsequence of 592 * this buffer's content. 593 * 594 * <p> The content of the new buffer will start at position {@code index} 595 * in this buffer, and will contain {@code length} elements. Changes to 596 * this buffer's content will be visible in the new buffer, and vice versa; 597 * the two buffers' position, limit, and mark values will be independent. 598 * 599 * <p> The new buffer's position will be zero, its capacity and its limit 600 * will be {@code length}, its mark will be undefined, and its byte order 601 * will be 602 603 604 605 * identical to that of this buffer. 606 607 * The new buffer will be direct if, and only if, this buffer is direct, 608 * and it will be read-only if, and only if, this buffer is read-only. </p> 609 * 610 * @param index 611 * The position in this buffer at which the content of the new 612 * buffer will start; must be non-negative and no larger than 613 * {@link #limit() limit()} 614 * 615 * @param length 616 * The number of elements the new buffer will contain; must be 617 * non-negative and no larger than {@code limit() - index} 618 * 619 * @return The new buffer 620 * 621 * @throws IndexOutOfBoundsException 622 * If {@code index} is negative or greater than {@code limit()}, 623 * {@code length} is negative, or {@code length > limit() - index} 624 * 625 * @since 13 626 */ 627 @Override slice(int index, int length)628 public abstract LongBuffer slice(int index, int length); 629 630 /** 631 * Creates a new long buffer that shares this buffer's content. 632 * 633 * <p> The content of the new buffer will be that of this buffer. Changes 634 * to this buffer's content will be visible in the new buffer, and vice 635 * versa; the two buffers' position, limit, and mark values will be 636 * independent. 637 * 638 * <p> The new buffer's capacity, limit, position, 639 640 641 642 643 * mark values, and byte order will be identical to those of this buffer. 644 645 * The new buffer will be direct if, and only if, this buffer is direct, and 646 * it will be read-only if, and only if, this buffer is read-only. </p> 647 * 648 * @return The new long buffer 649 */ 650 @Override duplicate()651 public abstract LongBuffer duplicate(); 652 653 /** 654 * Creates a new, read-only long buffer that shares this buffer's 655 * content. 656 * 657 * <p> The content of the new buffer will be that of this buffer. Changes 658 * to this buffer's content will be visible in the new buffer; the new 659 * buffer itself, however, will be read-only and will not allow the shared 660 * content to be modified. The two buffers' position, limit, and mark 661 * values will be independent. 662 * 663 * <p> The new buffer's capacity, limit, position, 664 665 666 667 668 * mark values, and byte order will be identical to those of this buffer. 669 670 * 671 * <p> If this buffer is itself read-only then this method behaves in 672 * exactly the same way as the {@link #duplicate duplicate} method. </p> 673 * 674 * @return The new, read-only long buffer 675 */ asReadOnlyBuffer()676 public abstract LongBuffer asReadOnlyBuffer(); 677 678 679 // -- Singleton get/put methods -- 680 681 /** 682 * Relative <i>get</i> method. Reads the long at this buffer's 683 * current position, and then increments the position. 684 * 685 * @return The long at the buffer's current position 686 * 687 * @throws BufferUnderflowException 688 * If the buffer's current position is not smaller than its limit 689 */ get()690 public abstract long get(); 691 692 /** 693 * Relative <i>put</i> method <i>(optional operation)</i>. 694 * 695 * <p> Writes the given long into this buffer at the current 696 * position, and then increments the position. </p> 697 * 698 * @param l 699 * The long to be written 700 * 701 * @return This buffer 702 * 703 * @throws BufferOverflowException 704 * If this buffer's current position is not smaller than its limit 705 * 706 * @throws ReadOnlyBufferException 707 * If this buffer is read-only 708 */ put(long l)709 public abstract LongBuffer put(long l); 710 711 /** 712 * Absolute <i>get</i> method. Reads the long at the given 713 * index. 714 * 715 * @param index 716 * The index from which the long will be read 717 * 718 * @return The long at the given index 719 * 720 * @throws IndexOutOfBoundsException 721 * If {@code index} is negative 722 * or not smaller than the buffer's limit 723 */ get(int index)724 public abstract long get(int index); 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 /** 740 * Absolute <i>put</i> method <i>(optional operation)</i>. 741 * 742 * <p> Writes the given long into this buffer at the given 743 * index. </p> 744 * 745 * @param index 746 * The index at which the long will be written 747 * 748 * @param l 749 * The long value to be written 750 * 751 * @return This buffer 752 * 753 * @throws IndexOutOfBoundsException 754 * If {@code index} is negative 755 * or not smaller than the buffer's limit 756 * 757 * @throws ReadOnlyBufferException 758 * If this buffer is read-only 759 */ put(int index, long l)760 public abstract LongBuffer put(int index, long l); 761 762 763 // -- Bulk get operations -- 764 765 /** 766 * Relative bulk <i>get</i> method. 767 * 768 * <p> This method transfers longs from this buffer into the given 769 * destination array. If there are fewer longs remaining in the 770 * buffer than are required to satisfy the request, that is, if 771 * {@code length} {@code >} {@code remaining()}, then no 772 * longs are transferred and a {@link BufferUnderflowException} is 773 * thrown. 774 * 775 * <p> Otherwise, this method copies {@code length} longs from this 776 * buffer into the given array, starting at the current position of this 777 * buffer and at the given offset in the array. The position of this 778 * buffer is then incremented by {@code length}. 779 * 780 * <p> In other words, an invocation of this method of the form 781 * <code>src.get(dst, off, len)</code> has exactly the same effect as 782 * the loop 783 * 784 * <pre>{@code 785 * for (int i = off; i < off + len; i++) 786 * dst[i] = src.get(); 787 * }</pre> 788 * 789 * except that it first checks that there are sufficient longs in 790 * this buffer and it is potentially much more efficient. 791 * 792 * @param dst 793 * The array into which longs are to be written 794 * 795 * @param offset 796 * The offset within the array of the first long to be 797 * written; must be non-negative and no larger than 798 * {@code dst.length} 799 * 800 * @param length 801 * The maximum number of longs to be written to the given 802 * array; must be non-negative and no larger than 803 * {@code dst.length - offset} 804 * 805 * @return This buffer 806 * 807 * @throws BufferUnderflowException 808 * If there are fewer than {@code length} longs 809 * remaining in this buffer 810 * 811 * @throws IndexOutOfBoundsException 812 * If the preconditions on the {@code offset} and {@code length} 813 * parameters do not hold 814 */ get(long[] dst, int offset, int length)815 public LongBuffer get(long[] dst, int offset, int length) { 816 Objects.checkFromIndexSize(offset, length, dst.length); 817 int pos = position(); 818 if (length > limit() - pos) 819 throw new BufferUnderflowException(); 820 821 getArray(pos, dst, offset, length); 822 823 position(pos + length); 824 return this; 825 } 826 827 /** 828 * Relative bulk <i>get</i> method. 829 * 830 * <p> This method transfers longs from this buffer into the given 831 * destination array. An invocation of this method of the form 832 * {@code src.get(a)} behaves in exactly the same way as the invocation 833 * 834 * <pre> 835 * src.get(a, 0, a.length) </pre> 836 * 837 * @param dst 838 * The destination array 839 * 840 * @return This buffer 841 * 842 * @throws BufferUnderflowException 843 * If there are fewer than {@code length} longs 844 * remaining in this buffer 845 */ get(long[] dst)846 public LongBuffer get(long[] dst) { 847 return get(dst, 0, dst.length); 848 } 849 850 /** 851 * Absolute bulk <i>get</i> method. 852 * 853 * <p> This method transfers {@code length} longs from this 854 * buffer into the given array, starting at the given index in this 855 * buffer and at the given offset in the array. The position of this 856 * buffer is unchanged. 857 * 858 * <p> An invocation of this method of the form 859 * <code>src.get(index, dst, offset, length)</code> 860 * has exactly the same effect as the following loop except that it first 861 * checks the consistency of the supplied parameters and it is potentially 862 * much more efficient: 863 * 864 * <pre>{@code 865 * for (int i = offset, j = index; i < offset + length; i++, j++) 866 * dst[i] = src.get(j); 867 * }</pre> 868 * 869 * @param index 870 * The index in this buffer from which the first long will be 871 * read; must be non-negative and less than {@code limit()} 872 * 873 * @param dst 874 * The destination array 875 * 876 * @param offset 877 * The offset within the array of the first long to be 878 * written; must be non-negative and less than 879 * {@code dst.length} 880 * 881 * @param length 882 * The number of longs to be written to the given array; 883 * must be non-negative and no larger than the smaller of 884 * {@code limit() - index} and {@code dst.length - offset} 885 * 886 * @return This buffer 887 * 888 * @throws IndexOutOfBoundsException 889 * If the preconditions on the {@code index}, {@code offset}, and 890 * {@code length} parameters do not hold 891 * 892 * @since 13 893 */ get(int index, long[] dst, int offset, int length)894 public LongBuffer get(int index, long[] dst, int offset, int length) { 895 Objects.checkFromIndexSize(index, length, limit()); 896 Objects.checkFromIndexSize(offset, length, dst.length); 897 898 getArray(index, dst, offset, length); 899 900 return this; 901 } 902 903 /** 904 * Absolute bulk <i>get</i> method. 905 * 906 * <p> This method transfers longs from this buffer into the given 907 * destination array. The position of this buffer is unchanged. An 908 * invocation of this method of the form 909 * <code>src.get(index, dst)</code> behaves in exactly the same 910 * way as the invocation: 911 * 912 * <pre> 913 * src.get(index, dst, 0, dst.length) </pre> 914 * 915 * @param index 916 * The index in this buffer from which the first long will be 917 * read; must be non-negative and less than {@code limit()} 918 * 919 * @param dst 920 * The destination array 921 * 922 * @return This buffer 923 * 924 * @throws IndexOutOfBoundsException 925 * If {@code index} is negative, not smaller than {@code limit()}, 926 * or {@code limit() - index < dst.length} 927 * 928 * @since 13 929 */ get(int index, long[] dst)930 public LongBuffer get(int index, long[] dst) { 931 return get(index, dst, 0, dst.length); 932 } 933 getArray(int index, long[] dst, int offset, int length)934 private LongBuffer getArray(int index, long[] dst, int offset, int length) { 935 // Android-changed: ScopedMemoryAccess is not yet supported. 936 /* 937 if ( 938 939 940 941 ((long)length << 3) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { 942 long bufAddr = address + ((long)index << 3); 943 long dstOffset = 944 ARRAY_BASE_OFFSET + ((long)offset << 3); 945 long len = (long)length << 3; 946 947 try { 948 949 if (order() != ByteOrder.nativeOrder()) 950 SCOPED_MEMORY_ACCESS.copySwapMemory( 951 scope(), null, base(), bufAddr, 952 dst, dstOffset, len, Long.BYTES); 953 else 954 955 SCOPED_MEMORY_ACCESS.copyMemory( 956 scope(), null, base(), bufAddr, 957 dst, dstOffset, len); 958 } finally { 959 Reference.reachabilityFence(this); 960 } 961 } else { 962 int end = offset + length; 963 for (int i = offset, j = index; i < end; i++, j++) { 964 dst[i] = get(j); 965 } 966 } 967 */ 968 int end = offset + length; 969 for (int i = offset, j = index; i < end; i++, j++) { 970 dst[i] = get(j); 971 } 972 return this; 973 } 974 975 // -- Bulk put operations -- 976 977 /** 978 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 979 * 980 * <p> This method transfers the longs remaining in the given source 981 * buffer into this buffer. If there are more longs remaining in the 982 * source buffer than in this buffer, that is, if 983 * {@code src.remaining()} {@code >} {@code remaining()}, 984 * then no longs are transferred and a {@link 985 * BufferOverflowException} is thrown. 986 * 987 * <p> Otherwise, this method copies 988 * <i>n</i> = {@code src.remaining()} longs from the given 989 * buffer into this buffer, starting at each buffer's current position. 990 * The positions of both buffers are then incremented by <i>n</i>. 991 * 992 * <p> In other words, an invocation of this method of the form 993 * {@code dst.put(src)} has exactly the same effect as the loop 994 * 995 * <pre> 996 * while (src.hasRemaining()) 997 * dst.put(src.get()); </pre> 998 * 999 * except that it first checks that there is sufficient space in this 1000 * buffer and it is potentially much more efficient. If this buffer and 1001 * the source buffer share the same backing array or memory, then the 1002 * result will be as if the source elements were first copied to an 1003 * intermediate location before being written into this buffer. 1004 * 1005 * @param src 1006 * The source buffer from which longs are to be read; 1007 * must not be this buffer 1008 * 1009 * @return This buffer 1010 * 1011 * @throws BufferOverflowException 1012 * If there is insufficient space in this buffer 1013 * for the remaining longs in the source buffer 1014 * 1015 * @throws IllegalArgumentException 1016 * If the source buffer is this buffer 1017 * 1018 * @throws ReadOnlyBufferException 1019 * If this buffer is read-only 1020 */ put(LongBuffer src)1021 public LongBuffer put(LongBuffer src) { 1022 if (src == this) 1023 throw createSameBufferException(); 1024 if (isReadOnly()) 1025 throw new ReadOnlyBufferException(); 1026 1027 int srcPos = src.position(); 1028 int srcLim = src.limit(); 1029 int srcRem = (srcPos <= srcLim ? srcLim - srcPos : 0); 1030 int pos = position(); 1031 int lim = limit(); 1032 int rem = (pos <= lim ? lim - pos : 0); 1033 1034 if (srcRem > rem) 1035 throw new BufferOverflowException(); 1036 1037 putBuffer(pos, src, srcPos, srcRem); 1038 1039 position(pos + srcRem); 1040 src.position(srcPos + srcRem); 1041 1042 return this; 1043 } 1044 1045 /** 1046 * Absolute bulk <i>put</i> method <i>(optional operation)</i>. 1047 * 1048 * <p> This method transfers {@code length} longs into this buffer from 1049 * the given source buffer, starting at the given {@code offset} in the 1050 * source buffer and the given {@code index} in this buffer. The positions 1051 * of both buffers are unchanged. 1052 * 1053 * <p> In other words, an invocation of this method of the form 1054 * <code>dst.put(index, src, offset, length)</code> 1055 * has exactly the same effect as the loop 1056 * 1057 * <pre>{@code 1058 * for (int i = offset, j = index; i < offset + length; i++, j++) 1059 * dst.put(j, src.get(i)); 1060 * }</pre> 1061 * 1062 * except that it first checks the consistency of the supplied parameters 1063 * and it is potentially much more efficient. If this buffer and 1064 * the source buffer share the same backing array or memory, then the 1065 * result will be as if the source elements were first copied to an 1066 * intermediate location before being written into this buffer. 1067 * 1068 * @param index 1069 * The index in this buffer at which the first long will be 1070 * written; must be non-negative and less than {@code limit()} 1071 * 1072 * @param src 1073 * The buffer from which longs are to be read 1074 * 1075 * @param offset 1076 * The index within the source buffer of the first long to be 1077 * read; must be non-negative and less than {@code src.limit()} 1078 * 1079 * @param length 1080 * The number of longs to be read from the given buffer; 1081 * must be non-negative and no larger than the smaller of 1082 * {@code limit() - index} and {@code src.limit() - offset} 1083 * 1084 * @return This buffer 1085 * 1086 * @throws IndexOutOfBoundsException 1087 * If the preconditions on the {@code index}, {@code offset}, and 1088 * {@code length} parameters do not hold 1089 * 1090 * @throws ReadOnlyBufferException 1091 * If this buffer is read-only 1092 * 1093 * @since 16 1094 */ put(int index, LongBuffer src, int offset, int length)1095 public LongBuffer put(int index, LongBuffer src, int offset, int length) { 1096 Objects.checkFromIndexSize(index, length, limit()); 1097 Objects.checkFromIndexSize(offset, length, src.limit()); 1098 if (isReadOnly()) 1099 throw new ReadOnlyBufferException(); 1100 1101 putBuffer(index, src, offset, length); 1102 1103 return this; 1104 } 1105 putBuffer(int pos, LongBuffer src, int srcPos, int n)1106 void putBuffer(int pos, LongBuffer src, int srcPos, int n) { 1107 1108 // Android-changed: ScopedMemoryAccess is not yet supported. 1109 1110 1111 1112 /* 1113 Object srcBase = src.base(); 1114 1115 1116 1117 assert srcBase != null || src.isDirect(); 1118 1119 1120 Object base = base(); 1121 assert base != null || isDirect(); 1122 1123 long srcAddr = src.address + ((long)srcPos << 3); 1124 long addr = address + ((long)pos << 3); 1125 long len = (long)n << 3; 1126 1127 try { 1128 1129 if (this.order() != src.order()) 1130 SCOPED_MEMORY_ACCESS.copySwapMemory( 1131 src.scope(), scope(), srcBase, srcAddr, 1132 base, addr, len, Long.BYTES); 1133 else 1134 1135 SCOPED_MEMORY_ACCESS.copyMemory( 1136 src.scope(), scope(), srcBase, srcAddr, 1137 base, addr, len); 1138 } finally { 1139 Reference.reachabilityFence(src); 1140 Reference.reachabilityFence(this); 1141 } 1142 1143 1144 1145 1146 1147 1148 1149 1150 */ 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 if (this.hb != null) { 1179 if (src.hb != null) { 1180 System.arraycopy(src.hb, srcPos + src.offset, hb, pos + offset, n); 1181 } else { 1182 // this and src don't share the same backed long[]. 1183 src.get(srcPos, this.hb, pos + offset, n); 1184 } 1185 return; 1186 } else if (src.hb != null) { 1187 // this and src don't share the same backed long[]. 1188 this.put(pos, src.hb, srcPos + src.offset, n); 1189 return; 1190 } 1191 1192 // Slow path using get(int). 1193 int posMax = pos + n; 1194 Object thisBase = base(); 1195 // If this buffer and the source buffer share the same backing array or memory, then the 1196 // result will be as if the source elements were first copied to an intermediate location 1197 // before being written into this buffer. 1198 // Instead of copying to an intermediate location, we change the writing order. 1199 boolean ascendingOrder; 1200 if (isDirect() && src.isDirect()) { 1201 // Both src and dst should be ByteBufferAsLongBuffer classes. 1202 // this.offset and src.offset should be zero, and can be ignored. 1203 long dstStart = this.address + ((long) pos << 3); 1204 long srcStart = src.address + ((long) srcPos << 3); 1205 // The second condition is optional, but the ascending order is the preferred behavior. 1206 ascendingOrder = (dstStart <= srcStart) || (srcStart + ((long) n << 3) < dstStart); 1207 // We may just do memmove here if both buffer uses the same byte order. 1208 } else if (thisBase != null && thisBase == src.base()) { // Share the same long[] or byte[] 1209 if (thisBase == this.hb) { // Both this and src should be HeapLongBuffer 1210 int dstStart = this.offset + pos; 1211 int srcStart = src.offset + srcPos; 1212 ascendingOrder = (dstStart <= srcStart) || (srcStart + n < dstStart); 1213 } else if (this instanceof ByteBufferAsLongBuffer asDst && 1214 src instanceof ByteBufferAsLongBuffer asSrc && thisBase instanceof byte[]) { 1215 // this.offset and src.offset should be zero, and can be ignored. 1216 long dstStart = asDst.byteOffset + asDst.bb.offset + ((long) pos << 3); 1217 long srcStart = asSrc.byteOffset + asSrc.bb.offset + ((long) srcPos << 3); 1218 ascendingOrder = (dstStart <= srcStart) || (srcStart + ((long) n << 3) < dstStart); 1219 } else { 1220 // There isn't a known case following into this condition. We should add a DCHECK here. 1221 ascendingOrder = true; 1222 } 1223 } else { 1224 ascendingOrder = true; 1225 } 1226 if (ascendingOrder) { 1227 for (int i = pos, j = srcPos; i < posMax; i++, j++) { 1228 put(i, src.get(j)); 1229 } 1230 } else { 1231 for (int i = posMax - 1, j = srcPos + n - 1; i >= pos; i--, j--) { 1232 put(i, src.get(j)); 1233 } 1234 } 1235 1236 1237 1238 1239 } 1240 1241 /** 1242 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 1243 * 1244 * <p> This method transfers longs into this buffer from the given 1245 * source array. If there are more longs to be copied from the array 1246 * than remain in this buffer, that is, if 1247 * {@code length} {@code >} {@code remaining()}, then no 1248 * longs are transferred and a {@link BufferOverflowException} is 1249 * thrown. 1250 * 1251 * <p> Otherwise, this method copies {@code length} longs from the 1252 * given array into this buffer, starting at the given offset in the array 1253 * and at the current position of this buffer. The position of this buffer 1254 * is then incremented by {@code length}. 1255 * 1256 * <p> In other words, an invocation of this method of the form 1257 * <code>dst.put(src, off, len)</code> has exactly the same effect as 1258 * the loop 1259 * 1260 * <pre>{@code 1261 * for (int i = off; i < off + len; i++) 1262 * dst.put(src[i]); 1263 * }</pre> 1264 * 1265 * except that it first checks that there is sufficient space in this 1266 * buffer and it is potentially much more efficient. 1267 * 1268 * @param src 1269 * The array from which longs are to be read 1270 * 1271 * @param offset 1272 * The offset within the array of the first long to be read; 1273 * must be non-negative and no larger than {@code src.length} 1274 * 1275 * @param length 1276 * The number of longs to be read from the given array; 1277 * must be non-negative and no larger than 1278 * {@code src.length - offset} 1279 * 1280 * @return This buffer 1281 * 1282 * @throws BufferOverflowException 1283 * If there is insufficient space in this buffer 1284 * 1285 * @throws IndexOutOfBoundsException 1286 * If the preconditions on the {@code offset} and {@code length} 1287 * parameters do not hold 1288 * 1289 * @throws ReadOnlyBufferException 1290 * If this buffer is read-only 1291 */ put(long[] src, int offset, int length)1292 public LongBuffer put(long[] src, int offset, int length) { 1293 if (isReadOnly()) 1294 throw new ReadOnlyBufferException(); 1295 Objects.checkFromIndexSize(offset, length, src.length); 1296 int pos = position(); 1297 if (length > limit() - pos) 1298 throw new BufferOverflowException(); 1299 1300 putArray(pos, src, offset, length); 1301 1302 position(pos + length); 1303 return this; 1304 } 1305 1306 /** 1307 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 1308 * 1309 * <p> This method transfers the entire content of the given source 1310 * long array into this buffer. An invocation of this method of the 1311 * form {@code dst.put(a)} behaves in exactly the same way as the 1312 * invocation 1313 * 1314 * <pre> 1315 * dst.put(a, 0, a.length) </pre> 1316 * 1317 * @param src 1318 * The source array 1319 * 1320 * @return This buffer 1321 * 1322 * @throws BufferOverflowException 1323 * If there is insufficient space in this buffer 1324 * 1325 * @throws ReadOnlyBufferException 1326 * If this buffer is read-only 1327 */ put(long[] src)1328 public final LongBuffer put(long[] src) { 1329 return put(src, 0, src.length); 1330 } 1331 1332 /** 1333 * Absolute bulk <i>put</i> method <i>(optional operation)</i>. 1334 * 1335 * <p> This method transfers {@code length} longs from the given 1336 * array, starting at the given offset in the array and at the given index 1337 * in this buffer. The position of this buffer is unchanged. 1338 * 1339 * <p> An invocation of this method of the form 1340 * <code>dst.put(index, src, offset, length)</code> 1341 * has exactly the same effect as the following loop except that it first 1342 * checks the consistency of the supplied parameters and it is potentially 1343 * much more efficient: 1344 * 1345 * <pre>{@code 1346 * for (int i = offset, j = index; i < offset + length; i++, j++) 1347 * dst.put(j, src[i]); 1348 * }</pre> 1349 * 1350 * @param index 1351 * The index in this buffer at which the first long will be 1352 * written; must be non-negative and less than {@code limit()} 1353 * 1354 * @param src 1355 * The array from which longs are to be read 1356 * 1357 * @param offset 1358 * The offset within the array of the first long to be read; 1359 * must be non-negative and less than {@code src.length} 1360 * 1361 * @param length 1362 * The number of longs to be read from the given array; 1363 * must be non-negative and no larger than the smaller of 1364 * {@code limit() - index} and {@code src.length - offset} 1365 * 1366 * @return This buffer 1367 * 1368 * @throws IndexOutOfBoundsException 1369 * If the preconditions on the {@code index}, {@code offset}, and 1370 * {@code length} parameters do not hold 1371 * 1372 * @throws ReadOnlyBufferException 1373 * If this buffer is read-only 1374 * 1375 * @since 13 1376 */ put(int index, long[] src, int offset, int length)1377 public LongBuffer put(int index, long[] src, int offset, int length) { 1378 if (isReadOnly()) 1379 throw new ReadOnlyBufferException(); 1380 Objects.checkFromIndexSize(index, length, limit()); 1381 Objects.checkFromIndexSize(offset, length, src.length); 1382 1383 putArray(index, src, offset, length); 1384 1385 return this; 1386 } 1387 1388 /** 1389 * Absolute bulk <i>put</i> method <i>(optional operation)</i>. 1390 * 1391 * <p> This method copies longs into this buffer from the given source 1392 * array. The position of this buffer is unchanged. An invocation of this 1393 * method of the form <code>dst.put(index, src)</code> 1394 * behaves in exactly the same way as the invocation: 1395 * 1396 * <pre> 1397 * dst.put(index, src, 0, src.length); </pre> 1398 * 1399 * @param index 1400 * The index in this buffer at which the first long will be 1401 * written; must be non-negative and less than {@code limit()} 1402 * 1403 * @param src 1404 * The array from which longs are to be read 1405 * 1406 * @return This buffer 1407 * 1408 * @throws IndexOutOfBoundsException 1409 * If {@code index} is negative, not smaller than {@code limit()}, 1410 * or {@code limit() - index < src.length} 1411 * 1412 * @throws ReadOnlyBufferException 1413 * If this buffer is read-only 1414 * 1415 * @since 13 1416 */ put(int index, long[] src)1417 public LongBuffer put(int index, long[] src) { 1418 return put(index, src, 0, src.length); 1419 } 1420 putArray(int index, long[] src, int offset, int length)1421 private LongBuffer putArray(int index, long[] src, int offset, int length) { 1422 1423 // Android-changed: ScopedMemoryAccess is not yet supported. 1424 /* 1425 if ( 1426 1427 1428 1429 ((long)length << 3) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { 1430 long bufAddr = address + ((long)index << 3); 1431 long srcOffset = 1432 ARRAY_BASE_OFFSET + ((long)offset << 3); 1433 long len = (long)length << 3; 1434 1435 try { 1436 1437 if (order() != ByteOrder.nativeOrder()) 1438 SCOPED_MEMORY_ACCESS.copySwapMemory( 1439 null, scope(), src, srcOffset, 1440 base(), bufAddr, len, Long.BYTES); 1441 else 1442 1443 SCOPED_MEMORY_ACCESS.copyMemory( 1444 null, scope(), src, srcOffset, 1445 base(), bufAddr, len); 1446 } finally { 1447 Reference.reachabilityFence(this); 1448 } 1449 } else { 1450 int end = offset + length; 1451 for (int i = offset, j = index; i < end; i++, j++) 1452 this.put(j, src[i]); 1453 } 1454 */ 1455 int end = offset + length; 1456 for (int i = offset, j = index; i < end; i++, j++) { 1457 this.put(j, src[i]); 1458 } 1459 return this; 1460 1461 1462 1463 1464 } 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 // -- Other stuff -- 1569 1570 /** 1571 * Tells whether or not this buffer is backed by an accessible long 1572 * array. 1573 * 1574 * <p> If this method returns {@code true} then the {@link #array() array} 1575 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 1576 * </p> 1577 * 1578 * @return {@code true} if, and only if, this buffer 1579 * is backed by an array and is not read-only 1580 */ hasArray()1581 public final boolean hasArray() { 1582 return (hb != null) && !isReadOnly; 1583 } 1584 1585 /** 1586 * Returns the long array that backs this 1587 * buffer <i>(optional operation)</i>. 1588 * 1589 * <p> Modifications to this buffer's content will cause the returned 1590 * array's content to be modified, and vice versa. 1591 * 1592 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 1593 * method in order to ensure that this buffer has an accessible backing 1594 * array. </p> 1595 * 1596 * @return The array that backs this buffer 1597 * 1598 * @throws ReadOnlyBufferException 1599 * If this buffer is backed by an array but is read-only 1600 * 1601 * @throws UnsupportedOperationException 1602 * If this buffer is not backed by an accessible array 1603 */ array()1604 public final long[] array() { 1605 if (hb == null) 1606 throw new UnsupportedOperationException(); 1607 if (isReadOnly) 1608 throw new ReadOnlyBufferException(); 1609 return hb; 1610 } 1611 1612 /** 1613 * Returns the offset within this buffer's backing array of the first 1614 * element of the buffer <i>(optional operation)</i>. 1615 * 1616 * <p> If this buffer is backed by an array then buffer position <i>p</i> 1617 * corresponds to array index <i>p</i> + {@code arrayOffset()}. 1618 * 1619 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 1620 * method in order to ensure that this buffer has an accessible backing 1621 * array. </p> 1622 * 1623 * @return The offset within this buffer's array 1624 * of the first element of the buffer 1625 * 1626 * @throws ReadOnlyBufferException 1627 * If this buffer is backed by an array but is read-only 1628 * 1629 * @throws UnsupportedOperationException 1630 * If this buffer is not backed by an accessible array 1631 */ arrayOffset()1632 public final int arrayOffset() { 1633 if (hb == null) 1634 throw new UnsupportedOperationException(); 1635 if (isReadOnly) 1636 throw new ReadOnlyBufferException(); 1637 return offset; 1638 } 1639 1640 // -- Covariant return type overrides 1641 1642 // BEGIN Android-added: covariant overloads of *Buffer methods that return this. 1643 /** 1644 * {@inheritDoc} 1645 */ 1646 // Android-changed: Un-final the method until confirmation of causing no app compat. 1647 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1648 @Override 1649 public position(int newPosition)1650 Buffer position(int newPosition) { 1651 super.position(newPosition); 1652 return this; 1653 } 1654 1655 /** 1656 * {@inheritDoc} 1657 */ 1658 // Android-changed: Un-final the method until confirmation of causing no app compat. 1659 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1660 @Override 1661 public limit(int newLimit)1662 Buffer limit(int newLimit) { 1663 super.limit(newLimit); 1664 return this; 1665 } 1666 1667 /** 1668 * {@inheritDoc} 1669 */ 1670 // Android-changed: Un-final the method until confirmation of causing no app compat. 1671 @Override 1672 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1673 public mark()1674 Buffer mark() { 1675 super.mark(); 1676 return this; 1677 } 1678 1679 /** 1680 * {@inheritDoc} 1681 */ 1682 // Android-changed: Un-final the method until confirmation of causing no app compat. 1683 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1684 @Override 1685 public reset()1686 Buffer reset() { 1687 super.reset(); 1688 return this; 1689 } 1690 1691 /** 1692 * {@inheritDoc} 1693 */ 1694 // Android-changed: Un-final the method until confirmation of causing no app compat. 1695 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1696 @Override 1697 public clear()1698 Buffer clear() { 1699 super.clear(); 1700 return this; 1701 } 1702 1703 /** 1704 * {@inheritDoc} 1705 */ 1706 // Android-changed: Un-final the method until confirmation of causing no app compat. 1707 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1708 @Override 1709 public flip()1710 Buffer flip() { 1711 super.flip(); 1712 return this; 1713 } 1714 1715 /** 1716 * {@inheritDoc} 1717 */ 1718 // Android-changed: Un-final the method until confirmation of causing no app compat. 1719 @Override 1720 @CovariantReturnType(returnType = LongBuffer.class, presentAfter = 28) 1721 public rewind()1722 Buffer rewind() { 1723 super.rewind(); 1724 return this; 1725 } 1726 // END Android-added: covariant overloads of *Buffer methods that return this. 1727 1728 /** 1729 * Compacts this buffer <i>(optional operation)</i>. 1730 * 1731 * <p> The longs between the buffer's current position and its limit, 1732 * if any, are copied to the beginning of the buffer. That is, the 1733 * long at index <i>p</i> = {@code position()} is copied 1734 * to index zero, the long at index <i>p</i> + 1 is copied 1735 * to index one, and so forth until the long at index 1736 * {@code limit()} - 1 is copied to index 1737 * <i>n</i> = {@code limit()} - {@code 1} - <i>p</i>. 1738 * The buffer's position is then set to <i>n+1</i> and its limit is set to 1739 * its capacity. The mark, if defined, is discarded. 1740 * 1741 * <p> The buffer's position is set to the number of longs copied, 1742 * rather than to zero, so that an invocation of this method can be 1743 * followed immediately by an invocation of another relative <i>put</i> 1744 * method. </p> 1745 * 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 * 1763 * @return This buffer 1764 * 1765 * @throws ReadOnlyBufferException 1766 * If this buffer is read-only 1767 */ compact()1768 public abstract LongBuffer compact(); 1769 1770 /** 1771 * Tells whether or not this long buffer is direct. 1772 * 1773 * @return {@code true} if, and only if, this buffer is direct 1774 */ isDirect()1775 public abstract boolean isDirect(); 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 /** 1794 * Returns a string summarizing the state of this buffer. 1795 * 1796 * @return A summary string 1797 */ toString()1798 public String toString() { 1799 return getClass().getName() 1800 + "[pos=" + position() 1801 + " lim=" + limit() 1802 + " cap=" + capacity() 1803 + "]"; 1804 } 1805 1806 1807 1808 1809 1810 1811 /** 1812 * Returns the current hash code of this buffer. 1813 * 1814 * <p> The hash code of a long buffer depends only upon its remaining 1815 * elements; that is, upon the elements from {@code position()} up to, and 1816 * including, the element at {@code limit()} - {@code 1}. 1817 * 1818 * <p> Because buffer hash codes are content-dependent, it is inadvisable 1819 * to use buffers as keys in hash maps or similar data structures unless it 1820 * is known that their contents will not change. </p> 1821 * 1822 * @return The current hash code of this buffer 1823 */ hashCode()1824 public int hashCode() { 1825 int h = 1; 1826 int p = position(); 1827 for (int i = limit() - 1; i >= p; i--) 1828 1829 1830 1831 h = 31 * h + (int)get(i); 1832 1833 return h; 1834 } 1835 1836 /** 1837 * Tells whether or not this buffer is equal to another object. 1838 * 1839 * <p> Two long buffers are equal if, and only if, 1840 * 1841 * <ol> 1842 * 1843 * <li><p> They have the same element type, </p></li> 1844 * 1845 * <li><p> They have the same number of remaining elements, and 1846 * </p></li> 1847 * 1848 * <li><p> The two sequences of remaining elements, considered 1849 * independently of their starting positions, are pointwise equal. 1850 1851 1852 1853 1854 1855 1856 1857 * </p></li> 1858 * 1859 * </ol> 1860 * 1861 * <p> A long buffer is not equal to any other type of object. </p> 1862 * 1863 * @param ob The object to which this buffer is to be compared 1864 * 1865 * @return {@code true} if, and only if, this buffer is equal to the 1866 * given object 1867 */ equals(Object ob)1868 public boolean equals(Object ob) { 1869 if (this == ob) 1870 return true; 1871 if (!(ob instanceof LongBuffer)) 1872 return false; 1873 LongBuffer that = (LongBuffer)ob; 1874 int thisPos = this.position(); 1875 int thisRem = this.limit() - thisPos; 1876 int thatPos = that.position(); 1877 int thatRem = that.limit() - thatPos; 1878 if (thisRem < 0 || thisRem != thatRem) 1879 return false; 1880 return BufferMismatch.mismatch(this, thisPos, 1881 that, thatPos, 1882 thisRem) < 0; 1883 } 1884 1885 /** 1886 * Compares this buffer to another. 1887 * 1888 * <p> Two long buffers are compared by comparing their sequences of 1889 * remaining elements lexicographically, without regard to the starting 1890 * position of each sequence within its corresponding buffer. 1891 1892 1893 1894 1895 1896 1897 1898 1899 * Pairs of {@code long} elements are compared as if by invoking 1900 * {@link Long#compare(long,long)}. 1901 1902 * 1903 * <p> A long buffer is not comparable to any other type of object. 1904 * 1905 * @return A negative integer, zero, or a positive integer as this buffer 1906 * is less than, equal to, or greater than the given buffer 1907 */ compareTo(LongBuffer that)1908 public int compareTo(LongBuffer that) { 1909 int thisPos = this.position(); 1910 int thisRem = this.limit() - thisPos; 1911 int thatPos = that.position(); 1912 int thatRem = that.limit() - thatPos; 1913 int length = Math.min(thisRem, thatRem); 1914 if (length < 0) 1915 return -1; 1916 int i = BufferMismatch.mismatch(this, thisPos, 1917 that, thatPos, 1918 length); 1919 if (i >= 0) { 1920 return compare(this.get(thisPos + i), that.get(thatPos + i)); 1921 } 1922 return thisRem - thatRem; 1923 } 1924 compare(long x, long y)1925 private static int compare(long x, long y) { 1926 1927 1928 1929 1930 1931 1932 return Long.compare(x, y); 1933 1934 } 1935 1936 /** 1937 * Finds and returns the relative index of the first mismatch between this 1938 * buffer and a given buffer. The index is relative to the 1939 * {@link #position() position} of each buffer and will be in the range of 1940 * 0 (inclusive) up to the smaller of the {@link #remaining() remaining} 1941 * elements in each buffer (exclusive). 1942 * 1943 * <p> If the two buffers share a common prefix then the returned index is 1944 * the length of the common prefix and it follows that there is a mismatch 1945 * between the two buffers at that index within the respective buffers. 1946 * If one buffer is a proper prefix of the other then the returned index is 1947 * the smaller of the remaining elements in each buffer, and it follows that 1948 * the index is only valid for the buffer with the larger number of 1949 * remaining elements. 1950 * Otherwise, there is no mismatch. 1951 * 1952 * @param that 1953 * The byte buffer to be tested for a mismatch with this buffer 1954 * 1955 * @return The relative index of the first mismatch between this and the 1956 * given buffer, otherwise -1 if no mismatch. 1957 * 1958 * @since 11 1959 */ mismatch(LongBuffer that)1960 public int mismatch(LongBuffer that) { 1961 int thisPos = this.position(); 1962 int thisRem = this.limit() - thisPos; 1963 int thatPos = that.position(); 1964 int thatRem = that.limit() - thatPos; 1965 int length = Math.min(thisRem, thatRem); 1966 if (length < 0) 1967 return -1; 1968 int r = BufferMismatch.mismatch(this, thisPos, 1969 that, thatPos, 1970 length); 1971 return (r == -1 && thisRem != thatRem) ? length : r; 1972 } 1973 1974 // -- Other char stuff -- 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 // -- Other byte stuff: Access to binary data -- 2183 2184 2185 2186 /** 2187 * Retrieves this buffer's byte order. 2188 * 2189 * <p> The byte order of a long buffer created by allocation or by 2190 * wrapping an existing {@code long} array is the {@link 2191 * ByteOrder#nativeOrder native order} of the underlying 2192 * hardware. The byte order of a long buffer created as a <a 2193 * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the 2194 * byte buffer at the moment that the view is created. </p> 2195 * 2196 * @return This buffer's byte order 2197 */ order()2198 public abstract ByteOrder order(); 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 } 2441