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 byte buffer. 51 * 52 * <p> This class defines six categories of operations upon 53 * byte buffers: 54 * 55 * <ul> 56 * 57 * <li><p> Absolute and relative {@link #get() <i>get</i>} and 58 * {@link #put(byte) <i>put</i>} methods that read and write 59 * single bytes; </p></li> 60 * 61 * <li><p> Absolute and relative {@link #get(byte[]) <i>bulk get</i>} 62 * methods that transfer contiguous sequences of bytes from this buffer 63 * into an array; </p></li> 64 * 65 * <li><p> Absolute and relative {@link #put(byte[]) <i>bulk put</i>} 66 * methods that transfer contiguous sequences of bytes from a 67 * byte array or some other byte 68 * buffer into this buffer; </p></li> 69 * 70 71 * 72 * <li><p> Absolute and relative {@link #getChar() <i>get</i>} 73 * and {@link #putChar(char) <i>put</i>} methods that read and 74 * write values of other primitive types, translating them to and from 75 * sequences of bytes in a particular byte order; </p></li> 76 * 77 * <li><p> Methods for creating <i><a href="#views">view buffers</a></i>, 78 * which allow a byte buffer to be viewed as a buffer containing values of 79 * some other primitive type; and </p></li> 80 * 81 82 * 83 * <li><p> A method for {@link #compact compacting} 84 * a byte buffer. </p></li> 85 * 86 * </ul> 87 * 88 * <p> Byte buffers can be created either by {@link #allocate 89 * <i>allocation</i>}, which allocates space for the buffer's 90 * 91 92 * 93 * content, or by {@link #wrap(byte[]) <i>wrapping</i>} an 94 * existing byte array {#if[char]?or string} into a buffer. 95 * 96 97 98 99 100 101 102 103 * 104 105 * 106 * <a id="direct"></a> 107 * <h2> Direct <i>vs.</i> non-direct buffers </h2> 108 * 109 * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a 110 * direct byte buffer, the Java virtual machine will make a best effort to 111 * perform native I/O operations directly upon it. That is, it will attempt to 112 * avoid copying the buffer's content to (or from) an intermediate buffer 113 * before (or after) each invocation of one of the underlying operating 114 * system's native I/O operations. 115 * 116 * <p> A direct byte buffer may be created by invoking the {@link 117 * #allocateDirect(int) allocateDirect} factory method of this class. The 118 * buffers returned by this method typically have somewhat higher allocation 119 * and deallocation costs than non-direct buffers. The contents of direct 120 * buffers may reside outside of the normal garbage-collected heap, and so 121 * their impact upon the memory footprint of an application might not be 122 * obvious. It is therefore recommended that direct buffers be allocated 123 * primarily for large, long-lived buffers that are subject to the underlying 124 * system's native I/O operations. In general it is best to allocate direct 125 * buffers only when they yield a measurable gain in program performance. 126 * 127 * <p> A direct byte buffer may also be created by {@link 128 * java.nio.channels.FileChannel#map mapping} a region of a file 129 * directly into memory. An implementation of the Java platform may optionally 130 * support the creation of direct byte buffers from native code via JNI. If an 131 * instance of one of these kinds of buffers refers to an inaccessible region 132 * of memory then an attempt to access that region will not change the buffer's 133 * content and will cause an unspecified exception to be thrown either at the 134 * time of the access or at some later time. 135 * 136 * <p> Whether a byte buffer is direct or non-direct may be determined by 137 * invoking its {@link #isDirect isDirect} method. This method is provided so 138 * that explicit buffer management can be done in performance-critical code. 139 * 140 * 141 * <a id="bin"></a> 142 * <h2> Access to binary data </h2> 143 * 144 * <p> This class defines methods for reading and writing values of all other 145 * primitive types, except {@code boolean}. Primitive values are translated 146 * to (or from) sequences of bytes according to the buffer's current byte 147 * order, which may be retrieved and modified via the {@link #order order} 148 * methods. Specific byte orders are represented by instances of the {@link 149 * ByteOrder} class. The initial order of a byte buffer is always {@link 150 * ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 151 * 152 * <p> For access to heterogeneous binary data, that is, sequences of values of 153 * different types, this class defines a family of absolute and relative 154 * <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point 155 * values, for example, this class defines: 156 * 157 * <blockquote><pre> 158 * float {@link #getFloat()} 159 * float {@link #getFloat(int) getFloat(int index)} 160 * void {@link #putFloat(float) putFloat(float f)} 161 * void {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote> 162 * 163 * <p> Corresponding methods are defined for the types {@code char, 164 * short, int, long}, and {@code double}. The index 165 * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of 166 * bytes rather than of the type being read or written. 167 * 168 * <a id="views"></a> 169 * 170 * <p> For access to homogeneous binary data, that is, sequences of values of 171 * the same type, this class defines methods that can create <i>views</i> of a 172 * given byte buffer. A <i>view buffer</i> is simply another buffer whose 173 * content is backed by the byte buffer. Changes to the byte buffer's content 174 * will be visible in the view buffer, and vice versa; the two buffers' 175 * position, limit, and mark values are independent. The {@link 176 * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of 177 * the {@link FloatBuffer} class that is backed by the byte buffer upon which 178 * the method is invoked. Corresponding view-creation methods are defined for 179 * the types {@code char, short, int, long}, and {@code double}. 180 * 181 * <p> View buffers have three important advantages over the families of 182 * type-specific <i>get</i> and <i>put</i> methods described above: 183 * 184 * <ul> 185 * 186 * <li><p> A view buffer is indexed not in terms of bytes but rather in terms 187 * of the type-specific size of its values; </p></li> 188 * 189 * <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i> 190 * methods that can transfer contiguous sequences of values between a buffer 191 * and an array or some other buffer of the same type; and </p></li> 192 * 193 * <li><p> A view buffer is potentially much more efficient because it will 194 * be direct if, and only if, its backing byte buffer is direct. </p></li> 195 * 196 * </ul> 197 * 198 * <p> The byte order of a view buffer is fixed to be that of its byte buffer 199 * at the time that the view is created. </p> 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 * <h2> Invocation chaining </h2> 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 * The sequence of statements 237 * 238 * <blockquote><pre> 239 * bb.putInt(0xCAFEBABE); 240 * bb.putShort(3); 241 * bb.putShort(45);</pre></blockquote> 242 * 243 * can, for example, be replaced by the single statement 244 * 245 * <blockquote><pre> 246 * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote> 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 ByteBuffer 273 extends Buffer 274 implements Comparable<ByteBuffer> 275 { 276 // Cached array base offset 277 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].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 byte[] 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 = 0; 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./ ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset)294 ByteBuffer(int mark, int pos, int lim, int cap, // package-private 295 byte[] 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 // ByteBuffer(int mark, int pos, int lim, int cap)305 ByteBuffer(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 ByteBuffer(byte[] 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 * Allocates a new direct byte buffer. 329 * 330 * <p> The new buffer's position will be zero, its limit will be its 331 * capacity, its mark will be undefined, each of its elements will be 332 * initialized to zero, and its byte order will be 333 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. Whether or not it has a 334 * {@link #hasArray backing array} is unspecified. 335 * 336 * @param capacity 337 * The new buffer's capacity, in bytes 338 * 339 * @return The new byte buffer 340 * 341 * @throws IllegalArgumentException 342 * If the {@code capacity} is a negative integer 343 */ allocateDirect(int capacity)344 public static ByteBuffer allocateDirect(int capacity) { 345 // Android-changed: Android's DirectByteBuffers carry a MemoryRef. 346 // return new DirectByteBuffer(capacity); 347 DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity); 348 return new DirectByteBuffer(capacity, memoryRef); 349 } 350 351 352 353 /** 354 * Allocates a new byte 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 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 361 362 363 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 bytes 370 * 371 * @return The new byte buffer 372 * 373 * @throws IllegalArgumentException 374 * If the {@code capacity} is a negative integer 375 */ allocate(int capacity)376 public static ByteBuffer allocate(int capacity) { 377 if (capacity < 0) 378 throw createCapacityException(capacity); 379 // Android-removed: Removed MemorySegmentProxy not supported yet. 380 return new HeapByteBuffer(capacity, capacity); 381 } 382 383 /** 384 * Wraps a byte array into a buffer. 385 * 386 * <p> The new buffer will be backed by the given byte 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 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 394 395 396 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 byte buffer 416 * 417 * @throws IndexOutOfBoundsException 418 * If the preconditions on the {@code offset} and {@code length} 419 * parameters do not hold 420 */ wrap(byte[] array, int offset, int length)421 public static ByteBuffer wrap(byte[] array, 422 int offset, int length) 423 { 424 try { 425 // Android-removed: Removed MemorySegmentProxy not supported yet. 426 return new HeapByteBuffer(array, offset, length); 427 } catch (IllegalArgumentException x) { 428 throw new IndexOutOfBoundsException(); 429 } 430 } 431 432 /** 433 * Wraps a byte array into a buffer. 434 * 435 * <p> The new buffer will be backed by the given byte 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 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 442 443 444 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 byte buffer 453 */ wrap(byte[] array)454 public static ByteBuffer wrap(byte[] 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 byte 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 bytes remaining in this buffer, its mark will be 572 * undefined, and its byte order will be 573 574 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 575 576 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 byte buffer 582 583 * 584 * @see #alignedSlice(int) 585 586 */ 587 @Override slice()588 public abstract ByteBuffer slice(); 589 590 /** 591 * Creates a new byte 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 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 604 605 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 ByteBuffer slice(int index, int length); 629 630 /** 631 * Creates a new byte 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 * and mark values will be identical to those of this buffer, and its byte 641 * order will be {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 642 643 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 byte buffer 649 */ 650 @Override duplicate()651 public abstract ByteBuffer duplicate(); 652 653 /** 654 * Creates a new, read-only byte 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 * and mark values will be identical to those of this buffer, and its byte 666 * order will be {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 667 668 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 byte buffer 675 */ asReadOnlyBuffer()676 public abstract ByteBuffer asReadOnlyBuffer(); 677 678 679 // -- Singleton get/put methods -- 680 681 /** 682 * Relative <i>get</i> method. Reads the byte at this buffer's 683 * current position, and then increments the position. 684 * 685 * @return The byte 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 byte get(); 691 692 /** 693 * Relative <i>put</i> method <i>(optional operation)</i>. 694 * 695 * <p> Writes the given byte into this buffer at the current 696 * position, and then increments the position. </p> 697 * 698 * @param b 699 * The byte 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(byte b)709 public abstract ByteBuffer put(byte b); 710 711 /** 712 * Absolute <i>get</i> method. Reads the byte at the given 713 * index. 714 * 715 * @param index 716 * The index from which the byte will be read 717 * 718 * @return The byte 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 byte 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 byte into this buffer at the given 743 * index. </p> 744 * 745 * @param index 746 * The index at which the byte will be written 747 * 748 * @param b 749 * The byte 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, byte b)760 public abstract ByteBuffer put(int index, byte b); 761 762 763 // -- Bulk get operations -- 764 765 /** 766 * Relative bulk <i>get</i> method. 767 * 768 * <p> This method transfers bytes from this buffer into the given 769 * destination array. If there are fewer bytes remaining in the 770 * buffer than are required to satisfy the request, that is, if 771 * {@code length} {@code >} {@code remaining()}, then no 772 * bytes are transferred and a {@link BufferUnderflowException} is 773 * thrown. 774 * 775 * <p> Otherwise, this method copies {@code length} bytes 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 bytes in 790 * this buffer and it is potentially much more efficient. 791 * 792 * @param dst 793 * The array into which bytes are to be written 794 * 795 * @param offset 796 * The offset within the array of the first byte 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 bytes 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} bytes 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(byte[] dst, int offset, int length)815 public ByteBuffer get(byte[] 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 bytes 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} bytes 844 * remaining in this buffer 845 */ get(byte[] dst)846 public ByteBuffer get(byte[] 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} bytes 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 byte 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 byte to be 878 * written; must be non-negative and less than 879 * {@code dst.length} 880 * 881 * @param length 882 * The number of bytes 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, byte[] dst, int offset, int length)894 public ByteBuffer get(int index, byte[] 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 bytes 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 byte 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, byte[] dst)930 public ByteBuffer get(int index, byte[] dst) { 931 return get(index, dst, 0, dst.length); 932 } 933 getArray(int index, byte[] dst, int offset, int length)934 private ByteBuffer getArray(int index, byte[] dst, int offset, int length) { 935 // Android-changed: ScopedMemoryAccess is not yet supported. 936 /* 937 if ( 938 939 940 941 ((long)length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { 942 long bufAddr = address + ((long)index << 0); 943 long dstOffset = 944 ARRAY_BASE_OFFSET + ((long)offset << 0); 945 long len = (long)length << 0; 946 947 try { 948 949 950 951 952 953 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 bytes remaining in the given source 981 * buffer into this buffer. If there are more bytes remaining in the 982 * source buffer than in this buffer, that is, if 983 * {@code src.remaining()} {@code >} {@code remaining()}, 984 * then no bytes are transferred and a {@link 985 * BufferOverflowException} is thrown. 986 * 987 * <p> Otherwise, this method copies 988 * <i>n</i> = {@code src.remaining()} bytes 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 bytes 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 bytes 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(ByteBuffer src)1021 public ByteBuffer put(ByteBuffer 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} bytes 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 byte will be 1070 * written; must be non-negative and less than {@code limit()} 1071 * 1072 * @param src 1073 * The buffer from which bytes are to be read 1074 * 1075 * @param offset 1076 * The index within the source buffer of the first byte to be 1077 * read; must be non-negative and less than {@code src.limit()} 1078 * 1079 * @param length 1080 * The number of bytes 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, ByteBuffer src, int offset, int length)1095 public ByteBuffer put(int index, ByteBuffer 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, ByteBuffer src, int srcPos, int n)1106 void putBuffer(int pos, ByteBuffer src, int srcPos, int n) { 1107 1108 // Android-changed: ScopedMemoryAccess is not yet supported. 1109 1110 // Android-changed: improve ByteBuffer.put(ByteBuffer) performance through bulk copy. 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 << 0); 1124 long addr = address + ((long)pos << 0); 1125 long len = (long)n << 0; 1126 1127 try { 1128 1129 1130 1131 1132 1133 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 // Note that we use offset instead of arrayOffset because arrayOffset is specified to 1153 // throw for read only buffers. Our use of arrayOffset here is provably safe, we only 1154 // use it to read *from* readOnly buffers. 1155 if (this.hb != null && src.hb != null) { 1156 // System.arraycopy is intrinsified by ART and therefore tiny bit faster than memmove 1157 System.arraycopy(src.hb, srcPos + src.offset, hb, pos + offset, n); 1158 } else { 1159 // Use the buffer object (and the raw memory address) if it's a direct buffer. Note that 1160 // isDirect() doesn't imply !hasArray(), ByteBuffer.allocateDirect allocated buffer will 1161 // have a backing, non-gc-movable byte array. JNI allocated direct byte buffers WILL NOT 1162 // have a backing array. 1163 final Object srcObject = src.isDirect() ? src : src.hb; 1164 int srcOffset = srcPos; 1165 if (!src.isDirect()) { 1166 srcOffset += src.offset; 1167 } 1168 1169 final ByteBuffer dst = this; 1170 final Object dstObject = dst.isDirect() ? dst : dst.hb; 1171 int dstOffset = pos; 1172 if (!dst.isDirect()) { 1173 dstOffset += dst.offset; 1174 } 1175 Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, n); 1176 } 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 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 bytes into this buffer from the given 1245 * source array. If there are more bytes to be copied from the array 1246 * than remain in this buffer, that is, if 1247 * {@code length} {@code >} {@code remaining()}, then no 1248 * bytes are transferred and a {@link BufferOverflowException} is 1249 * thrown. 1250 * 1251 * <p> Otherwise, this method copies {@code length} bytes 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 bytes are to be read 1270 * 1271 * @param offset 1272 * The offset within the array of the first byte to be read; 1273 * must be non-negative and no larger than {@code src.length} 1274 * 1275 * @param length 1276 * The number of bytes 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(byte[] src, int offset, int length)1292 public ByteBuffer put(byte[] 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 * byte 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(byte[] src)1328 public final ByteBuffer put(byte[] 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} bytes 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 byte will be 1352 * written; must be non-negative and less than {@code limit()} 1353 * 1354 * @param src 1355 * The array from which bytes are to be read 1356 * 1357 * @param offset 1358 * The offset within the array of the first byte to be read; 1359 * must be non-negative and less than {@code src.length} 1360 * 1361 * @param length 1362 * The number of bytes 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, byte[] src, int offset, int length)1377 public ByteBuffer put(int index, byte[] 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 bytes 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 byte will be 1401 * written; must be non-negative and less than {@code limit()} 1402 * 1403 * @param src 1404 * The array from which bytes 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, byte[] src)1417 public ByteBuffer put(int index, byte[] src) { 1418 return put(index, src, 0, src.length); 1419 } 1420 putArray(int index, byte[] src, int offset, int length)1421 private ByteBuffer putArray(int index, byte[] src, int offset, int length) { 1422 1423 // Android-changed: ScopedMemoryAccess is not yet supported. 1424 /* 1425 if ( 1426 1427 1428 1429 ((long)length << 0) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { 1430 long bufAddr = address + ((long)index << 0); 1431 long srcOffset = 1432 ARRAY_BASE_OFFSET + ((long)offset << 0); 1433 long len = (long)length << 0; 1434 1435 try { 1436 1437 1438 1439 1440 1441 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 byte 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 byte 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 byte[] 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 = ByteBuffer.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 = ByteBuffer.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 = ByteBuffer.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 = ByteBuffer.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 = ByteBuffer.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 = ByteBuffer.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 = ByteBuffer.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 bytes between the buffer's current position and its limit, 1732 * if any, are copied to the beginning of the buffer. That is, the 1733 * byte at index <i>p</i> = {@code position()} is copied 1734 * to index zero, the byte at index <i>p</i> + 1 is copied 1735 * to index one, and so forth until the byte 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 bytes 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 * <p> Invoke this method after writing data from a buffer in case the 1749 * write was incomplete. The following loop, for example, copies bytes 1750 * from one channel to another via the buffer {@code buf}: 1751 * 1752 * <blockquote><pre>{@code 1753 * buf.clear(); // Prepare buffer for use 1754 * while (in.read(buf) >= 0 || buf.position != 0) { 1755 * buf.flip(); 1756 * out.write(buf); 1757 * buf.compact(); // In case of partial write 1758 * } 1759 * }</pre></blockquote> 1760 * 1761 1762 * 1763 * @return This buffer 1764 * 1765 * @throws ReadOnlyBufferException 1766 * If this buffer is read-only 1767 */ compact()1768 public abstract ByteBuffer compact(); 1769 1770 /** 1771 * Tells whether or not this byte 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 byte 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 byte 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 byte 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 ByteBuffer)) 1872 return false; 1873 ByteBuffer that = (ByteBuffer)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 byte 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 byte} elements are compared as if by invoking 1900 * {@link Byte#compare(byte,byte)}. 1901 1902 * 1903 * <p> A byte 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(ByteBuffer that)1908 public int compareTo(ByteBuffer 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(byte x, byte y)1925 private static int compare(byte x, byte y) { 1926 1927 1928 1929 1930 1931 1932 return Byte.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(ByteBuffer that)1960 public int mismatch(ByteBuffer 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 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 boolean bigEndian // package-private 2211 = true; 2212 boolean nativeByteOrder // package-private 2213 = (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN); 2214 2215 /** 2216 * Retrieves this buffer's byte order. 2217 * 2218 * <p> The byte order is used when reading or writing multibyte values, and 2219 * when creating buffers that are views of this byte buffer. The order of 2220 * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN 2221 * BIG_ENDIAN}. </p> 2222 * 2223 * @return This buffer's byte order 2224 */ order()2225 public final ByteOrder order() { 2226 return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; 2227 } 2228 2229 /** 2230 * Modifies this buffer's byte order. 2231 * 2232 * @param bo 2233 * The new byte order, 2234 * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN} 2235 * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN} 2236 * 2237 * @return This buffer 2238 */ order(ByteOrder bo)2239 public final ByteBuffer order(ByteOrder bo) { 2240 bigEndian = (bo == ByteOrder.BIG_ENDIAN); 2241 nativeByteOrder = 2242 (bigEndian == (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)); 2243 return this; 2244 } 2245 2246 /** 2247 * Returns the memory address, pointing to the byte at the given index, 2248 * modulo the given unit size. 2249 * 2250 * <p> The return value is non-negative in the range of {@code 0} 2251 * (inclusive) up to {@code unitSize} (exclusive), with zero indicating 2252 * that the address of the byte at the index is aligned for the unit size, 2253 * and a positive value that the address is misaligned for the unit size. 2254 * If the address of the byte at the index is misaligned, the return value 2255 * represents how much the index should be adjusted to locate a byte at an 2256 * aligned address. Specifically, the index should either be decremented by 2257 * the return value if the latter is not greater than {@code index}, or be 2258 * incremented by the unit size minus the return value. Therefore given 2259 * <blockquote><pre> 2260 * int value = alignmentOffset(index, unitSize)</pre></blockquote> 2261 * then the identities 2262 * <blockquote><pre> 2263 * alignmentOffset(index - value, unitSize) == 0, value ≤ index</pre></blockquote> 2264 * and 2265 * <blockquote><pre> 2266 * alignmentOffset(index + (unitSize - value), unitSize) == 0</pre></blockquote> 2267 * must hold. 2268 * 2269 * @apiNote 2270 * This method may be utilized to determine if unit size bytes from an 2271 * index can be accessed atomically, if supported by the native platform. 2272 * 2273 * @implNote 2274 * This implementation throws {@code UnsupportedOperationException} for 2275 * non-direct buffers when the given unit size is greater than {@code 8}. 2276 * 2277 * @param index 2278 * The index to query for alignment offset, must be non-negative, no 2279 * upper bounds check is performed 2280 * 2281 * @param unitSize 2282 * The unit size in bytes, must be a power of {@code 2} 2283 * 2284 * @return The indexed byte's memory address modulo the unit size 2285 * 2286 * @throws IllegalArgumentException 2287 * If the index is negative or the unit size is not a power of 2288 * {@code 2} 2289 * 2290 * @throws UnsupportedOperationException 2291 * If the native platform does not guarantee stable alignment offset 2292 * values for the given unit size when managing the memory regions 2293 * of buffers of the same kind as this buffer (direct or 2294 * non-direct). For example, if garbage collection would result 2295 * in the moving of a memory region covered by a non-direct buffer 2296 * from one location to another and both locations have different 2297 * alignment characteristics. 2298 * 2299 * @see #alignedSlice(int) 2300 * @since 9 2301 */ alignmentOffset(int index, int unitSize)2302 public final int alignmentOffset(int index, int unitSize) { 2303 if (index < 0) 2304 throw new IllegalArgumentException("Index less than zero: " + index); 2305 if (unitSize < 1 || (unitSize & (unitSize - 1)) != 0) 2306 throw new IllegalArgumentException("Unit size not a power of two: " + unitSize); 2307 if (unitSize > 8 && !isDirect()) 2308 throw new UnsupportedOperationException("Unit size unsupported for non-direct buffers: " + unitSize); 2309 2310 // BEGIN Android-changed: Android specific alignment calculation. 2311 // return (int) ((address + index) & (unitSize - 1)); 2312 final long baseAddress = isDirect() ? address : (ARRAY_BASE_OFFSET + offset); 2313 2314 final long elementAddress = baseAddress + index; 2315 return (int) (elementAddress & (unitSize - 1)); 2316 // END Android-changed: Android specific alignment calculation. 2317 } 2318 2319 /** 2320 * Creates a new byte buffer whose content is a shared and aligned 2321 * subsequence of this buffer's content. 2322 * 2323 * <p> The content of the new buffer will start at this buffer's current 2324 * position rounded up to the index of the nearest aligned byte for the 2325 * given unit size, and end at this buffer's limit rounded down to the index 2326 * of the nearest aligned byte for the given unit size. 2327 * If rounding results in out-of-bound values then the new buffer's capacity 2328 * and limit will be zero. If rounding is within bounds the following 2329 * expressions will be true for a new buffer {@code nb} and unit size 2330 * {@code unitSize}: 2331 * <pre>{@code 2332 * nb.alignmentOffset(0, unitSize) == 0 2333 * nb.alignmentOffset(nb.limit(), unitSize) == 0 2334 * }</pre> 2335 * 2336 * <p> Changes to this buffer's content will be visible in the new 2337 * buffer, and vice versa; the two buffers' position, limit, and mark 2338 * values will be independent. 2339 * 2340 * <p> The new buffer's position will be zero, its capacity and its limit 2341 * will be the number of bytes remaining in this buffer or fewer subject to 2342 * alignment, its mark will be undefined, and its byte order will be 2343 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 2344 * 2345 * The new buffer will be direct if, and only if, this buffer is direct, and 2346 * it will be read-only if, and only if, this buffer is read-only. </p> 2347 * 2348 * @apiNote 2349 * This method may be utilized to create a new buffer where unit size bytes 2350 * from index, that is a multiple of the unit size, may be accessed 2351 * atomically, if supported by the native platform. 2352 * 2353 * @implNote 2354 * This implementation throws {@code UnsupportedOperationException} for 2355 * non-direct buffers when the given unit size is greater than {@code 8}. 2356 * 2357 * @param unitSize 2358 * The unit size in bytes, must be a power of {@code 2} 2359 * 2360 * @return The new byte buffer 2361 * 2362 * @throws IllegalArgumentException 2363 * If the unit size not a power of {@code 2} 2364 * 2365 * @throws UnsupportedOperationException 2366 * If the native platform does not guarantee stable aligned slices 2367 * for the given unit size when managing the memory regions 2368 * of buffers of the same kind as this buffer (direct or 2369 * non-direct). For example, if garbage collection would result 2370 * in the moving of a memory region covered by a non-direct buffer 2371 * from one location to another and both locations have different 2372 * alignment characteristics. 2373 * 2374 * @see #alignmentOffset(int, int) 2375 * @see #slice() 2376 * @since 9 2377 */ alignedSlice(int unitSize)2378 public final ByteBuffer alignedSlice(int unitSize) { 2379 int pos = position(); 2380 int lim = limit(); 2381 2382 int pos_mod = alignmentOffset(pos, unitSize); 2383 int lim_mod = alignmentOffset(lim, unitSize); 2384 2385 // Round up the position to align with unit size 2386 int aligned_pos = (pos_mod > 0) 2387 ? pos + (unitSize - pos_mod) 2388 : pos; 2389 2390 // Round down the limit to align with unit size 2391 int aligned_lim = lim - lim_mod; 2392 2393 if (aligned_pos > lim || aligned_lim < pos) { 2394 aligned_pos = aligned_lim = pos; 2395 } 2396 2397 return slice(aligned_pos, aligned_lim - aligned_pos); 2398 } 2399 2400 2401 // Android-added: Unchecked accessors, for use by ByteBufferAs-X-Buffer and Bits classes _get(int i)2402 abstract byte _get(int i); // package-private _put(int i, byte b)2403 abstract void _put(int i, byte b); // package-private 2404 2405 // BEGIN Android-added: isAccessible(), setAccessible(), for use by frameworks (MediaCodec). 2406 /** 2407 * @hide 2408 */ isAccessible()2409 public boolean isAccessible() { 2410 return true; 2411 } 2412 2413 /** 2414 * @hide 2415 */ setAccessible(boolean value)2416 public void setAccessible(boolean value) { 2417 throw new UnsupportedOperationException(); 2418 } 2419 // END Android-added: isAccessible(), setAccessible(), for use by frameworks (MediaCodec). 2420 2421 2422 /** 2423 * Relative <i>get</i> method for reading a char value. 2424 * 2425 * <p> Reads the next two bytes at this buffer's current position, 2426 * composing them into a char value according to the current byte order, 2427 * and then increments the position by two. </p> 2428 * 2429 * @return The char value at the buffer's current position 2430 * 2431 * @throws BufferUnderflowException 2432 * If there are fewer than two bytes 2433 * remaining in this buffer 2434 */ getChar()2435 public abstract char getChar(); 2436 2437 /** 2438 * Relative <i>put</i> method for writing a char 2439 * value <i>(optional operation)</i>. 2440 * 2441 * <p> Writes two bytes containing the given char value, in the 2442 * current byte order, into this buffer at the current position, and then 2443 * increments the position by two. </p> 2444 * 2445 * @param value 2446 * The char value to be written 2447 * 2448 * @return This buffer 2449 * 2450 * @throws BufferOverflowException 2451 * If there are fewer than two bytes 2452 * remaining in this buffer 2453 * 2454 * @throws ReadOnlyBufferException 2455 * If this buffer is read-only 2456 */ putChar(char value)2457 public abstract ByteBuffer putChar(char value); 2458 2459 /** 2460 * Absolute <i>get</i> method for reading a char value. 2461 * 2462 * <p> Reads two bytes at the given index, composing them into a 2463 * char value according to the current byte order. </p> 2464 * 2465 * @param index 2466 * The index from which the bytes will be read 2467 * 2468 * @return The char value at the given index 2469 * 2470 * @throws IndexOutOfBoundsException 2471 * If {@code index} is negative 2472 * or not smaller than the buffer's limit, 2473 * minus one 2474 */ getChar(int index)2475 public abstract char getChar(int index); 2476 2477 // BEGIN Android-added: {get,put}*Unchecked() accessors. getCharUnchecked(int index)2478 abstract char getCharUnchecked(int index); getUnchecked(int pos, char[] dst, int dstOffset, int length)2479 abstract void getUnchecked(int pos, char[] dst, int dstOffset, int length); 2480 // END Android-added: {get,put}*Unchecked() accessors. 2481 2482 /** 2483 * Absolute <i>put</i> method for writing a char 2484 * value <i>(optional operation)</i>. 2485 * 2486 * <p> Writes two bytes containing the given char value, in the 2487 * current byte order, into this buffer at the given index. </p> 2488 * 2489 * @param index 2490 * The index at which the bytes will be written 2491 * 2492 * @param value 2493 * The char value to be written 2494 * 2495 * @return This buffer 2496 * 2497 * @throws IndexOutOfBoundsException 2498 * If {@code index} is negative 2499 * or not smaller than the buffer's limit, 2500 * minus one 2501 * 2502 * @throws ReadOnlyBufferException 2503 * If this buffer is read-only 2504 */ putChar(int index, char value)2505 public abstract ByteBuffer putChar(int index, char value); 2506 2507 // BEGIN Android-added: {get,put}*Unchecked() accessors. putCharUnchecked(int index, char value)2508 abstract void putCharUnchecked(int index, char value); putUnchecked(int pos, char[] dst, int srcOffset, int length)2509 abstract void putUnchecked(int pos, char[] dst, int srcOffset, int length); 2510 // END Android-added: {get,put}*Unchecked() accessors. 2511 2512 /** 2513 * Creates a view of this byte buffer as a char buffer. 2514 * 2515 * <p> The content of the new buffer will start at this buffer's current 2516 * position. Changes to this buffer's content will be visible in the new 2517 * buffer, and vice versa; the two buffers' position, limit, and mark 2518 * values will be independent. 2519 * 2520 * <p> The new buffer's position will be zero, its capacity and its limit 2521 * will be the number of bytes remaining in this buffer divided by 2522 * two, its mark will be undefined, and its byte order will be that 2523 * of the byte buffer at the moment the view is created. The new buffer 2524 * will be direct if, and only if, this buffer is direct, and it will be 2525 * read-only if, and only if, this buffer is read-only. </p> 2526 * 2527 * @return A new char buffer 2528 */ asCharBuffer()2529 public abstract CharBuffer asCharBuffer(); 2530 2531 2532 /** 2533 * Relative <i>get</i> method for reading a short value. 2534 * 2535 * <p> Reads the next two bytes at this buffer's current position, 2536 * composing them into a short value according to the current byte order, 2537 * and then increments the position by two. </p> 2538 * 2539 * @return The short value at the buffer's current position 2540 * 2541 * @throws BufferUnderflowException 2542 * If there are fewer than two bytes 2543 * remaining in this buffer 2544 */ getShort()2545 public abstract short getShort(); 2546 2547 /** 2548 * Relative <i>put</i> method for writing a short 2549 * value <i>(optional operation)</i>. 2550 * 2551 * <p> Writes two bytes containing the given short value, in the 2552 * current byte order, into this buffer at the current position, and then 2553 * increments the position by two. </p> 2554 * 2555 * @param value 2556 * The short value to be written 2557 * 2558 * @return This buffer 2559 * 2560 * @throws BufferOverflowException 2561 * If there are fewer than two bytes 2562 * remaining in this buffer 2563 * 2564 * @throws ReadOnlyBufferException 2565 * If this buffer is read-only 2566 */ putShort(short value)2567 public abstract ByteBuffer putShort(short value); 2568 2569 /** 2570 * Absolute <i>get</i> method for reading a short value. 2571 * 2572 * <p> Reads two bytes at the given index, composing them into a 2573 * short value according to the current byte order. </p> 2574 * 2575 * @param index 2576 * The index from which the bytes will be read 2577 * 2578 * @return The short value at the given index 2579 * 2580 * @throws IndexOutOfBoundsException 2581 * If {@code index} is negative 2582 * or not smaller than the buffer's limit, 2583 * minus one 2584 */ getShort(int index)2585 public abstract short getShort(int index); 2586 2587 // BEGIN Android-added: {get,put}*Unchecked() accessors. getShortUnchecked(int index)2588 abstract short getShortUnchecked(int index); getUnchecked(int pos, short[] dst, int dstOffset, int length)2589 abstract void getUnchecked(int pos, short[] dst, int dstOffset, int length); 2590 // END Android-added: {get,put}*Unchecked() accessors. 2591 2592 /** 2593 * Absolute <i>put</i> method for writing a short 2594 * value <i>(optional operation)</i>. 2595 * 2596 * <p> Writes two bytes containing the given short value, in the 2597 * current byte order, into this buffer at the given index. </p> 2598 * 2599 * @param index 2600 * The index at which the bytes will be written 2601 * 2602 * @param value 2603 * The short value to be written 2604 * 2605 * @return This buffer 2606 * 2607 * @throws IndexOutOfBoundsException 2608 * If {@code index} is negative 2609 * or not smaller than the buffer's limit, 2610 * minus one 2611 * 2612 * @throws ReadOnlyBufferException 2613 * If this buffer is read-only 2614 */ putShort(int index, short value)2615 public abstract ByteBuffer putShort(int index, short value); 2616 2617 // BEGIN Android-added: {get,put}*Unchecked() accessors. putShortUnchecked(int index, short value)2618 abstract void putShortUnchecked(int index, short value); putUnchecked(int pos, short[] dst, int srcOffset, int length)2619 abstract void putUnchecked(int pos, short[] dst, int srcOffset, int length); 2620 // END Android-added: {get,put}*Unchecked() accessors. 2621 2622 /** 2623 * Creates a view of this byte buffer as a short buffer. 2624 * 2625 * <p> The content of the new buffer will start at this buffer's current 2626 * position. Changes to this buffer's content will be visible in the new 2627 * buffer, and vice versa; the two buffers' position, limit, and mark 2628 * values will be independent. 2629 * 2630 * <p> The new buffer's position will be zero, its capacity and its limit 2631 * will be the number of bytes remaining in this buffer divided by 2632 * two, its mark will be undefined, and its byte order will be that 2633 * of the byte buffer at the moment the view is created. The new buffer 2634 * will be direct if, and only if, this buffer is direct, and it will be 2635 * read-only if, and only if, this buffer is read-only. </p> 2636 * 2637 * @return A new short buffer 2638 */ asShortBuffer()2639 public abstract ShortBuffer asShortBuffer(); 2640 2641 2642 /** 2643 * Relative <i>get</i> method for reading an int value. 2644 * 2645 * <p> Reads the next four bytes at this buffer's current position, 2646 * composing them into an int value according to the current byte order, 2647 * and then increments the position by four. </p> 2648 * 2649 * @return The int value at the buffer's current position 2650 * 2651 * @throws BufferUnderflowException 2652 * If there are fewer than four bytes 2653 * remaining in this buffer 2654 */ getInt()2655 public abstract int getInt(); 2656 2657 /** 2658 * Relative <i>put</i> method for writing an int 2659 * value <i>(optional operation)</i>. 2660 * 2661 * <p> Writes four bytes containing the given int value, in the 2662 * current byte order, into this buffer at the current position, and then 2663 * increments the position by four. </p> 2664 * 2665 * @param value 2666 * The int value to be written 2667 * 2668 * @return This buffer 2669 * 2670 * @throws BufferOverflowException 2671 * If there are fewer than four bytes 2672 * remaining in this buffer 2673 * 2674 * @throws ReadOnlyBufferException 2675 * If this buffer is read-only 2676 */ putInt(int value)2677 public abstract ByteBuffer putInt(int value); 2678 2679 /** 2680 * Absolute <i>get</i> method for reading an int value. 2681 * 2682 * <p> Reads four bytes at the given index, composing them into a 2683 * int value according to the current byte order. </p> 2684 * 2685 * @param index 2686 * The index from which the bytes will be read 2687 * 2688 * @return The int value at the given index 2689 * 2690 * @throws IndexOutOfBoundsException 2691 * If {@code index} is negative 2692 * or not smaller than the buffer's limit, 2693 * minus three 2694 */ getInt(int index)2695 public abstract int getInt(int index); 2696 2697 // BEGIN Android-added: {get,put}*Unchecked() accessors. getIntUnchecked(int index)2698 abstract int getIntUnchecked(int index); getUnchecked(int pos, int[] dst, int dstOffset, int length)2699 abstract void getUnchecked(int pos, int[] dst, int dstOffset, int length); 2700 // END Android-added: {get,put}*Unchecked() accessors. 2701 2702 /** 2703 * Absolute <i>put</i> method for writing an int 2704 * value <i>(optional operation)</i>. 2705 * 2706 * <p> Writes four bytes containing the given int value, in the 2707 * current byte order, into this buffer at the given index. </p> 2708 * 2709 * @param index 2710 * The index at which the bytes will be written 2711 * 2712 * @param value 2713 * The int value to be written 2714 * 2715 * @return This buffer 2716 * 2717 * @throws IndexOutOfBoundsException 2718 * If {@code index} is negative 2719 * or not smaller than the buffer's limit, 2720 * minus three 2721 * 2722 * @throws ReadOnlyBufferException 2723 * If this buffer is read-only 2724 */ putInt(int index, int value)2725 public abstract ByteBuffer putInt(int index, int value); 2726 2727 // BEGIN Android-added: {get,put}*Unchecked() accessors. putIntUnchecked(int index, int value)2728 abstract void putIntUnchecked(int index, int value); putUnchecked(int pos, int[] dst, int srcOffset, int length)2729 abstract void putUnchecked(int pos, int[] dst, int srcOffset, int length); 2730 // END Android-added: {get,put}*Unchecked() accessors. 2731 2732 /** 2733 * Creates a view of this byte buffer as an int buffer. 2734 * 2735 * <p> The content of the new buffer will start at this buffer's current 2736 * position. Changes to this buffer's content will be visible in the new 2737 * buffer, and vice versa; the two buffers' position, limit, and mark 2738 * values will be independent. 2739 * 2740 * <p> The new buffer's position will be zero, its capacity and its limit 2741 * will be the number of bytes remaining in this buffer divided by 2742 * four, its mark will be undefined, and its byte order will be that 2743 * of the byte buffer at the moment the view is created. The new buffer 2744 * will be direct if, and only if, this buffer is direct, and it will be 2745 * read-only if, and only if, this buffer is read-only. </p> 2746 * 2747 * @return A new int buffer 2748 */ asIntBuffer()2749 public abstract IntBuffer asIntBuffer(); 2750 2751 2752 /** 2753 * Relative <i>get</i> method for reading a long value. 2754 * 2755 * <p> Reads the next eight bytes at this buffer's current position, 2756 * composing them into a long value according to the current byte order, 2757 * and then increments the position by eight. </p> 2758 * 2759 * @return The long value at the buffer's current position 2760 * 2761 * @throws BufferUnderflowException 2762 * If there are fewer than eight bytes 2763 * remaining in this buffer 2764 */ getLong()2765 public abstract long getLong(); 2766 2767 /** 2768 * Relative <i>put</i> method for writing a long 2769 * value <i>(optional operation)</i>. 2770 * 2771 * <p> Writes eight bytes containing the given long value, in the 2772 * current byte order, into this buffer at the current position, and then 2773 * increments the position by eight. </p> 2774 * 2775 * @param value 2776 * The long value to be written 2777 * 2778 * @return This buffer 2779 * 2780 * @throws BufferOverflowException 2781 * If there are fewer than eight bytes 2782 * remaining in this buffer 2783 * 2784 * @throws ReadOnlyBufferException 2785 * If this buffer is read-only 2786 */ putLong(long value)2787 public abstract ByteBuffer putLong(long value); 2788 2789 /** 2790 * Absolute <i>get</i> method for reading a long value. 2791 * 2792 * <p> Reads eight bytes at the given index, composing them into a 2793 * long value according to the current byte order. </p> 2794 * 2795 * @param index 2796 * The index from which the bytes will be read 2797 * 2798 * @return The long value at the given index 2799 * 2800 * @throws IndexOutOfBoundsException 2801 * If {@code index} is negative 2802 * or not smaller than the buffer's limit, 2803 * minus seven 2804 */ getLong(int index)2805 public abstract long getLong(int index); 2806 2807 // BEGIN Android-added: {get,put}*Unchecked() accessors. getLongUnchecked(int index)2808 abstract long getLongUnchecked(int index); getUnchecked(int pos, long[] dst, int dstOffset, int length)2809 abstract void getUnchecked(int pos, long[] dst, int dstOffset, int length); 2810 // END Android-added: {get,put}*Unchecked() accessors. 2811 2812 /** 2813 * Absolute <i>put</i> method for writing a long 2814 * value <i>(optional operation)</i>. 2815 * 2816 * <p> Writes eight bytes containing the given long value, in the 2817 * current byte order, into this buffer at the given index. </p> 2818 * 2819 * @param index 2820 * The index at which the bytes will be written 2821 * 2822 * @param value 2823 * The long value to be written 2824 * 2825 * @return This buffer 2826 * 2827 * @throws IndexOutOfBoundsException 2828 * If {@code index} is negative 2829 * or not smaller than the buffer's limit, 2830 * minus seven 2831 * 2832 * @throws ReadOnlyBufferException 2833 * If this buffer is read-only 2834 */ putLong(int index, long value)2835 public abstract ByteBuffer putLong(int index, long value); 2836 2837 // BEGIN Android-added: {get,put}*Unchecked() accessors. putLongUnchecked(int index, long value)2838 abstract void putLongUnchecked(int index, long value); putUnchecked(int pos, long[] dst, int srcOffset, int length)2839 abstract void putUnchecked(int pos, long[] dst, int srcOffset, int length); 2840 // END Android-added: {get,put}*Unchecked() accessors. 2841 2842 /** 2843 * Creates a view of this byte buffer as a long buffer. 2844 * 2845 * <p> The content of the new buffer will start at this buffer's current 2846 * position. Changes to this buffer's content will be visible in the new 2847 * buffer, and vice versa; the two buffers' position, limit, and mark 2848 * values will be independent. 2849 * 2850 * <p> The new buffer's position will be zero, its capacity and its limit 2851 * will be the number of bytes remaining in this buffer divided by 2852 * eight, its mark will be undefined, and its byte order will be that 2853 * of the byte buffer at the moment the view is created. The new buffer 2854 * will be direct if, and only if, this buffer is direct, and it will be 2855 * read-only if, and only if, this buffer is read-only. </p> 2856 * 2857 * @return A new long buffer 2858 */ asLongBuffer()2859 public abstract LongBuffer asLongBuffer(); 2860 2861 2862 /** 2863 * Relative <i>get</i> method for reading a float value. 2864 * 2865 * <p> Reads the next four bytes at this buffer's current position, 2866 * composing them into a float value according to the current byte order, 2867 * and then increments the position by four. </p> 2868 * 2869 * @return The float value at the buffer's current position 2870 * 2871 * @throws BufferUnderflowException 2872 * If there are fewer than four bytes 2873 * remaining in this buffer 2874 */ getFloat()2875 public abstract float getFloat(); 2876 2877 /** 2878 * Relative <i>put</i> method for writing a float 2879 * value <i>(optional operation)</i>. 2880 * 2881 * <p> Writes four bytes containing the given float value, in the 2882 * current byte order, into this buffer at the current position, and then 2883 * increments the position by four. </p> 2884 * 2885 * @param value 2886 * The float value to be written 2887 * 2888 * @return This buffer 2889 * 2890 * @throws BufferOverflowException 2891 * If there are fewer than four bytes 2892 * remaining in this buffer 2893 * 2894 * @throws ReadOnlyBufferException 2895 * If this buffer is read-only 2896 */ putFloat(float value)2897 public abstract ByteBuffer putFloat(float value); 2898 2899 /** 2900 * Absolute <i>get</i> method for reading a float value. 2901 * 2902 * <p> Reads four bytes at the given index, composing them into a 2903 * float value according to the current byte order. </p> 2904 * 2905 * @param index 2906 * The index from which the bytes will be read 2907 * 2908 * @return The float value at the given index 2909 * 2910 * @throws IndexOutOfBoundsException 2911 * If {@code index} is negative 2912 * or not smaller than the buffer's limit, 2913 * minus three 2914 */ getFloat(int index)2915 public abstract float getFloat(int index); 2916 2917 // BEGIN Android-added: {get,put}*Unchecked() accessors. getFloatUnchecked(int index)2918 abstract float getFloatUnchecked(int index); getUnchecked(int pos, float[] dst, int dstOffset, int length)2919 abstract void getUnchecked(int pos, float[] dst, int dstOffset, int length); 2920 // END Android-added: {get,put}*Unchecked() accessors. 2921 2922 /** 2923 * Absolute <i>put</i> method for writing a float 2924 * value <i>(optional operation)</i>. 2925 * 2926 * <p> Writes four bytes containing the given float value, in the 2927 * current byte order, into this buffer at the given index. </p> 2928 * 2929 * @param index 2930 * The index at which the bytes will be written 2931 * 2932 * @param value 2933 * The float value to be written 2934 * 2935 * @return This buffer 2936 * 2937 * @throws IndexOutOfBoundsException 2938 * If {@code index} is negative 2939 * or not smaller than the buffer's limit, 2940 * minus three 2941 * 2942 * @throws ReadOnlyBufferException 2943 * If this buffer is read-only 2944 */ putFloat(int index, float value)2945 public abstract ByteBuffer putFloat(int index, float value); 2946 2947 // BEGIN Android-added: {get,put}*Unchecked() accessors. putFloatUnchecked(int index, float value)2948 abstract void putFloatUnchecked(int index, float value); putUnchecked(int pos, float[] dst, int srcOffset, int length)2949 abstract void putUnchecked(int pos, float[] dst, int srcOffset, int length); 2950 // END Android-added: {get,put}*Unchecked() accessors. 2951 2952 /** 2953 * Creates a view of this byte buffer as a float buffer. 2954 * 2955 * <p> The content of the new buffer will start at this buffer's current 2956 * position. Changes to this buffer's content will be visible in the new 2957 * buffer, and vice versa; the two buffers' position, limit, and mark 2958 * values will be independent. 2959 * 2960 * <p> The new buffer's position will be zero, its capacity and its limit 2961 * will be the number of bytes remaining in this buffer divided by 2962 * four, its mark will be undefined, and its byte order will be that 2963 * of the byte buffer at the moment the view is created. The new buffer 2964 * will be direct if, and only if, this buffer is direct, and it will be 2965 * read-only if, and only if, this buffer is read-only. </p> 2966 * 2967 * @return A new float buffer 2968 */ asFloatBuffer()2969 public abstract FloatBuffer asFloatBuffer(); 2970 2971 2972 /** 2973 * Relative <i>get</i> method for reading a double value. 2974 * 2975 * <p> Reads the next eight bytes at this buffer's current position, 2976 * composing them into a double value according to the current byte order, 2977 * and then increments the position by eight. </p> 2978 * 2979 * @return The double value at the buffer's current position 2980 * 2981 * @throws BufferUnderflowException 2982 * If there are fewer than eight bytes 2983 * remaining in this buffer 2984 */ getDouble()2985 public abstract double getDouble(); 2986 2987 /** 2988 * Relative <i>put</i> method for writing a double 2989 * value <i>(optional operation)</i>. 2990 * 2991 * <p> Writes eight bytes containing the given double value, in the 2992 * current byte order, into this buffer at the current position, and then 2993 * increments the position by eight. </p> 2994 * 2995 * @param value 2996 * The double value to be written 2997 * 2998 * @return This buffer 2999 * 3000 * @throws BufferOverflowException 3001 * If there are fewer than eight bytes 3002 * remaining in this buffer 3003 * 3004 * @throws ReadOnlyBufferException 3005 * If this buffer is read-only 3006 */ putDouble(double value)3007 public abstract ByteBuffer putDouble(double value); 3008 3009 /** 3010 * Absolute <i>get</i> method for reading a double value. 3011 * 3012 * <p> Reads eight bytes at the given index, composing them into a 3013 * double value according to the current byte order. </p> 3014 * 3015 * @param index 3016 * The index from which the bytes will be read 3017 * 3018 * @return The double value at the given index 3019 * 3020 * @throws IndexOutOfBoundsException 3021 * If {@code index} is negative 3022 * or not smaller than the buffer's limit, 3023 * minus seven 3024 */ getDouble(int index)3025 public abstract double getDouble(int index); 3026 3027 // BEGIN Android-added: {get,put}*Unchecked() accessors. getDoubleUnchecked(int index)3028 abstract double getDoubleUnchecked(int index); getUnchecked(int pos, double[] dst, int dstOffset, int length)3029 abstract void getUnchecked(int pos, double[] dst, int dstOffset, int length); 3030 // END Android-added: {get,put}*Unchecked() accessors. 3031 3032 /** 3033 * Absolute <i>put</i> method for writing a double 3034 * value <i>(optional operation)</i>. 3035 * 3036 * <p> Writes eight bytes containing the given double value, in the 3037 * current byte order, into this buffer at the given index. </p> 3038 * 3039 * @param index 3040 * The index at which the bytes will be written 3041 * 3042 * @param value 3043 * The double value to be written 3044 * 3045 * @return This buffer 3046 * 3047 * @throws IndexOutOfBoundsException 3048 * If {@code index} is negative 3049 * or not smaller than the buffer's limit, 3050 * minus seven 3051 * 3052 * @throws ReadOnlyBufferException 3053 * If this buffer is read-only 3054 */ putDouble(int index, double value)3055 public abstract ByteBuffer putDouble(int index, double value); 3056 3057 // BEGIN Android-added: {get,put}*Unchecked() accessors. putDoubleUnchecked(int index, double value)3058 abstract void putDoubleUnchecked(int index, double value); putUnchecked(int pos, double[] dst, int srcOffset, int length)3059 abstract void putUnchecked(int pos, double[] dst, int srcOffset, int length); 3060 // END Android-added: {get,put}*Unchecked() accessors. 3061 3062 /** 3063 * Creates a view of this byte buffer as a double buffer. 3064 * 3065 * <p> The content of the new buffer will start at this buffer's current 3066 * position. Changes to this buffer's content will be visible in the new 3067 * buffer, and vice versa; the two buffers' position, limit, and mark 3068 * values will be independent. 3069 * 3070 * <p> The new buffer's position will be zero, its capacity and its limit 3071 * will be the number of bytes remaining in this buffer divided by 3072 * eight, its mark will be undefined, and its byte order will be that 3073 * of the byte buffer at the moment the view is created. The new buffer 3074 * will be direct if, and only if, this buffer is direct, and it will be 3075 * read-only if, and only if, this buffer is read-only. </p> 3076 * 3077 * @return A new double buffer 3078 */ asDoubleBuffer()3079 public abstract DoubleBuffer asDoubleBuffer(); 3080 3081 } 3082