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 29 package java.nio; 30 31 import jdk.internal.misc.Unsafe; 32 33 import libcore.io.Memory; 34 35 import java.util.Objects; 36 import dalvik.annotation.codegen.CovariantReturnType; 37 38 /** 39 * A byte buffer. 40 * 41 * <p> This class defines six categories of operations upon 42 * byte buffers: 43 * 44 * <ul> 45 * 46 * <li><p> Absolute and relative {@link #get() <i>get</i>} and 47 * {@link #put(byte) <i>put</i>} methods that read and write 48 * single bytes; </p></li> 49 * 50 * <li><p> Relative {@link #get(byte[]) <i>bulk get</i>} 51 * methods that transfer contiguous sequences of bytes from this buffer 52 * into an array; </p></li> 53 * 54 * <li><p> Relative {@link #put(byte[]) <i>bulk put</i>} 55 * methods that transfer contiguous sequences of bytes from a 56 * byte array or some other byte 57 * buffer into this buffer; </p></li> 58 * 59 * 60 * <li><p> Absolute and relative {@link #getChar() <i>get</i>} 61 * and {@link #putChar(char) <i>put</i>} methods that read and 62 * write values of other primitive types, translating them to and from 63 * sequences of bytes in a particular byte order; </p></li> 64 * 65 * <li><p> Methods for creating <i><a href="#views">view buffers</a></i>, 66 * which allow a byte buffer to be viewed as a buffer containing values of 67 * some other primitive type; and </p></li> 68 * 69 * 70 * <li><p> Methods for {@link #compact compacting}, {@link 71 * #duplicate duplicating}, and {@link #slice slicing} 72 * a byte buffer. </p></li> 73 * 74 * </ul> 75 * 76 * <p> Byte buffers can be created either by {@link #allocate 77 * <i>allocation</i>}, which allocates space for the buffer's 78 * 79 * 80 * content, or by {@link #wrap(byte[]) <i>wrapping</i>} an 81 * existing byte array into a buffer. 82 * 83 * 84 * 85 * <a name="direct"></a> 86 * <h2> Direct <i>vs.</i> non-direct buffers </h2> 87 * 88 * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a 89 * direct byte buffer, the Java virtual machine will make a best effort to 90 * perform native I/O operations directly upon it. That is, it will attempt to 91 * avoid copying the buffer's content to (or from) an intermediate buffer 92 * before (or after) each invocation of one of the underlying operating 93 * system's native I/O operations. 94 * 95 * <p> A direct byte buffer may be created by invoking the {@link 96 * #allocateDirect(int) allocateDirect} factory method of this class. The 97 * buffers returned by this method typically have somewhat higher allocation 98 * and deallocation costs than non-direct buffers. The contents of direct 99 * buffers may reside outside of the normal garbage-collected heap, and so 100 * their impact upon the memory footprint of an application might not be 101 * obvious. It is therefore recommended that direct buffers be allocated 102 * primarily for large, long-lived buffers that are subject to the underlying 103 * system's native I/O operations. In general it is best to allocate direct 104 * buffers only when they yield a measureable gain in program performance. 105 * 106 * <p> A direct byte buffer may also be created by {@link 107 * java.nio.channels.FileChannel#map mapping} a region of a file 108 * directly into memory. An implementation of the Java platform may optionally 109 * support the creation of direct byte buffers from native code via JNI. If an 110 * instance of one of these kinds of buffers refers to an inaccessible region 111 * of memory then an attempt to access that region will not change the buffer's 112 * content and will cause an unspecified exception to be thrown either at the 113 * time of the access or at some later time. 114 * 115 * <p> Whether a byte buffer is direct or non-direct may be determined by 116 * invoking its {@link #isDirect isDirect} method. This method is provided so 117 * that explicit buffer management can be done in performance-critical code. 118 * 119 * 120 * <a name="bin"></a> 121 * <h2> Access to binary data </h2> 122 * 123 * <p> This class defines methods for reading and writing values of all other 124 * primitive types, except <tt>boolean</tt>. Primitive values are translated 125 * to (or from) sequences of bytes according to the buffer's current byte 126 * order, which may be retrieved and modified via the {@link #order order} 127 * methods. Specific byte orders are represented by instances of the {@link 128 * ByteOrder} class. The initial order of a byte buffer is always {@link 129 * ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 130 * 131 * <p> For access to heterogeneous binary data, that is, sequences of values of 132 * different types, this class defines a family of absolute and relative 133 * <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point 134 * values, for example, this class defines: 135 * 136 * <blockquote><pre> 137 * float {@link #getFloat()} 138 * float {@link #getFloat(int) getFloat(int index)} 139 * void {@link #putFloat(float) putFloat(float f)} 140 * void {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote> 141 * 142 * <p> Corresponding methods are defined for the types <tt>char</tt>, 143 * <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and <tt>double</tt>. The index 144 * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of 145 * bytes rather than of the type being read or written. 146 * 147 * <a name="views"></a> 148 * 149 * <p> For access to homogeneous binary data, that is, sequences of values of 150 * the same type, this class defines methods that can create <i>views</i> of a 151 * given byte buffer. A <i>view buffer</i> is simply another buffer whose 152 * content is backed by the byte buffer. Changes to the byte buffer's content 153 * will be visible in the view buffer, and vice versa; the two buffers' 154 * position, limit, and mark values are independent. The {@link 155 * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of 156 * the {@link FloatBuffer} class that is backed by the byte buffer upon which 157 * the method is invoked. Corresponding view-creation methods are defined for 158 * the types <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and 159 * <tt>double</tt>. 160 * 161 * <p> View buffers have three important advantages over the families of 162 * type-specific <i>get</i> and <i>put</i> methods described above: 163 * 164 * <ul> 165 * 166 * <li><p> A view buffer is indexed not in terms of bytes but rather in terms 167 * of the type-specific size of its values; </p></li> 168 * 169 * <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i> 170 * methods that can transfer contiguous sequences of values between a buffer 171 * and an array or some other buffer of the same type; and </p></li> 172 * 173 * <li><p> A view buffer is potentially much more efficient because it will 174 * be direct if, and only if, its backing byte buffer is direct. </p></li> 175 * 176 * </ul> 177 * 178 * <p> The byte order of a view buffer is fixed to be that of its byte buffer 179 * at the time that the view is created. </p> 180 * 181 * 182 * 183 * 184 * <h2> Invocation chaining </h2> 185 * 186 * <p> Methods in this class that do not otherwise have a value to return are 187 * specified to return the buffer upon which they are invoked. This allows 188 * method invocations to be chained. 189 * 190 * 191 * The sequence of statements 192 * 193 * <blockquote><pre> 194 * bb.putInt(0xCAFEBABE); 195 * bb.putShort(3); 196 * bb.putShort(45);</pre></blockquote> 197 * 198 * can, for example, be replaced by the single statement 199 * 200 * <blockquote><pre> 201 * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote> 202 * 203 * 204 * 205 * @author Mark Reinhold 206 * @author JSR-51 Expert Group 207 * @since 1.4 208 */ 209 210 public abstract class ByteBuffer 211 extends Buffer 212 implements Comparable<ByteBuffer> 213 { 214 215 // These fields are declared here rather than in Heap-X-Buffer in order to 216 // reduce the number of virtual method invocations needed to access these 217 // values, which is especially costly when coding small buffers. 218 // 219 final byte[] hb; // Non-null only for heap buffers 220 final int offset; 221 boolean isReadOnly; // Valid only for heap buffers 222 223 // Creates a new buffer with the given mark, position, limit, capacity, 224 // backing array, and array offset 225 // ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset)226 ByteBuffer(int mark, int pos, int lim, int cap, // package-private 227 byte[] hb, int offset) 228 { 229 // Android-added: elementSizeShift parameter (log2 of element size). 230 super(mark, pos, lim, cap, 0 /* elementSizeShift */); 231 this.hb = hb; 232 this.offset = offset; 233 } 234 235 // Creates a new buffer with the given mark, position, limit, and capacity 236 // ByteBuffer(int mark, int pos, int lim, int cap)237 ByteBuffer(int mark, int pos, int lim, int cap) { // package-private 238 this(mark, pos, lim, cap, null, 0); 239 } 240 241 @Override base()242 Object base() { 243 return hb; 244 } 245 246 /** 247 * Allocates a new direct byte buffer. 248 * 249 * <p> The new buffer's position will be zero, its limit will be its 250 * capacity, its mark will be undefined, and each of its elements will be 251 * initialized to zero. Whether or not it has a 252 * {@link #hasArray backing array} is unspecified. 253 * 254 * @param capacity 255 * The new buffer's capacity, in bytes 256 * 257 * @return The new byte buffer 258 * 259 * @throws IllegalArgumentException 260 * If the <tt>capacity</tt> is a negative integer 261 */ allocateDirect(int capacity)262 public static ByteBuffer allocateDirect(int capacity) { 263 // Android-changed: Android's DirectByteBuffers carry a MemoryRef. 264 // return new DirectByteBuffer(capacity); 265 DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity); 266 return new DirectByteBuffer(capacity, memoryRef); 267 } 268 269 270 /** 271 * Allocates a new byte buffer. 272 * 273 * <p> The new buffer's position will be zero, its limit will be its 274 * capacity, its mark will be undefined, and each of its elements will be 275 * initialized to zero. It will have a {@link #array backing array}, 276 * and its {@link #arrayOffset array offset} will be zero. 277 * 278 * @param capacity 279 * The new buffer's capacity, in bytes 280 * 281 * @return The new byte buffer 282 * 283 * @throws IllegalArgumentException 284 * If the <tt>capacity</tt> is a negative integer 285 */ allocate(int capacity)286 public static ByteBuffer allocate(int capacity) { 287 if (capacity < 0) 288 throw createCapacityException(capacity); 289 return new HeapByteBuffer(capacity, capacity); 290 } 291 292 /** 293 * Wraps a byte array into a buffer. 294 * 295 * <p> The new buffer will be backed by the given byte array; 296 * that is, modifications to the buffer will cause the array to be modified 297 * and vice versa. The new buffer's capacity will be 298 * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit 299 * will be <tt>offset + length</tt>, and its mark will be undefined. Its 300 * {@link #array backing array} will be the given array, and 301 * its {@link #arrayOffset array offset} will be zero. </p> 302 * 303 * @param array 304 * The array that will back the new buffer 305 * 306 * @param offset 307 * The offset of the subarray to be used; must be non-negative and 308 * no larger than <tt>array.length</tt>. The new buffer's position 309 * will be set to this value. 310 * 311 * @param length 312 * The length of the subarray to be used; 313 * must be non-negative and no larger than 314 * <tt>array.length - offset</tt>. 315 * The new buffer's limit will be set to <tt>offset + length</tt>. 316 * 317 * @return The new byte buffer 318 * 319 * @throws IndexOutOfBoundsException 320 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 321 * parameters do not hold 322 */ wrap(byte[] array, int offset, int length)323 public static ByteBuffer wrap(byte[] array, 324 int offset, int length) 325 { 326 try { 327 return new HeapByteBuffer(array, offset, length); 328 } catch (IllegalArgumentException x) { 329 throw new IndexOutOfBoundsException(); 330 } 331 } 332 333 /** 334 * Wraps a byte array into a buffer. 335 * 336 * <p> The new buffer will be backed by the given byte array; 337 * that is, modifications to the buffer will cause the array to be modified 338 * and vice versa. The new buffer's capacity and limit will be 339 * <tt>array.length</tt>, its position will be zero, and its mark will be 340 * undefined. Its {@link #array backing array} will be the 341 * given array, and its {@link #arrayOffset array offset>} will 342 * be zero. </p> 343 * 344 * @param array 345 * The array that will back this buffer 346 * 347 * @return The new byte buffer 348 */ wrap(byte[] array)349 public static ByteBuffer wrap(byte[] array) { 350 return wrap(array, 0, array.length); 351 } 352 353 354 /** 355 * Creates a new byte buffer whose content is a shared subsequence of 356 * this buffer's content. 357 * 358 * <p> The content of the new buffer will start at this buffer's current 359 * position. Changes to this buffer's content will be visible in the new 360 * buffer, and vice versa; the two buffers' position, limit, and mark 361 * values will be independent. 362 * 363 * <p> The new buffer's position will be zero, its capacity and its limit 364 * will be the number of bytes remaining in this buffer, and its mark 365 * will be undefined. The new buffer will be direct if, and only if, this 366 * buffer is direct, and it will be read-only if, and only if, this buffer 367 * is read-only. </p> 368 * 369 * @return The new byte buffer 370 */ 371 @Override slice()372 public abstract ByteBuffer slice(); 373 374 /** 375 * Creates a new byte buffer whose content is a shared subsequence of 376 * this buffer's content. 377 * 378 * <p> The content of the new buffer will start at position {@code index} 379 * in this buffer, and will contain {@code length} elements. Changes to 380 * this buffer's content will be visible in the new buffer, and vice versa; 381 * the two buffers' position, limit, and mark values will be independent. 382 * 383 * <p> The new buffer's position will be zero, its capacity and its limit 384 * will be {@code length}, its mark will be undefined, and its byte order 385 * will be {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 386 * 387 * The new buffer will be direct if, and only if, this buffer is direct, 388 * and it will be read-only if, and only if, this buffer is read-only. </p> 389 * 390 * @param index 391 * The position in this buffer at which the content of the new 392 * buffer will start; must be non-negative and no larger than 393 * {@link #limit() limit()} 394 * 395 * @param length 396 * The number of elements the new buffer will contain; must be 397 * non-negative and no larger than {@code limit() - index} 398 * 399 * @return The new buffer 400 * 401 * @throws IndexOutOfBoundsException 402 * If {@code index} is negative or greater than {@code limit()}, 403 * {@code length} is negative, or {@code length > limit() - index} 404 * 405 * @since 13 406 */ 407 @Override slice(int index, int length)408 public abstract ByteBuffer slice(int index, int length); 409 410 /** 411 * Creates a new byte buffer that shares this buffer's content. 412 * 413 * <p> The content of the new buffer will be that of this buffer. Changes 414 * to this buffer's content will be visible in the new buffer, and vice 415 * versa; the two buffers' position, limit, and mark values will be 416 * independent. 417 * 418 * <p> The new buffer's capacity, limit, position, and mark values will be 419 * identical to those of this buffer. The new buffer will be direct if, 420 * and only if, this buffer is direct, and it will be read-only if, and 421 * only if, this buffer is read-only. </p> 422 * 423 * @return The new byte buffer 424 */ 425 @Override duplicate()426 public abstract ByteBuffer duplicate(); 427 428 /** 429 * Creates a new, read-only byte buffer that shares this buffer's 430 * content. 431 * 432 * <p> The content of the new buffer will be that of this buffer. Changes 433 * to this buffer's content will be visible in the new buffer; the new 434 * buffer itself, however, will be read-only and will not allow the shared 435 * content to be modified. The two buffers' position, limit, and mark 436 * values will be independent. 437 * 438 * <p> The new buffer's capacity, limit, position, and mark values will be 439 * identical to those of this buffer. 440 * 441 * <p> If this buffer is itself read-only then this method behaves in 442 * exactly the same way as the {@link #duplicate duplicate} method. </p> 443 * 444 * @return The new, read-only byte buffer 445 */ asReadOnlyBuffer()446 public abstract ByteBuffer asReadOnlyBuffer(); 447 448 449 // -- Singleton get/put methods -- 450 451 /** 452 * Relative <i>get</i> method. Reads the byte at this buffer's 453 * current position, and then increments the position. 454 * 455 * @return The byte at the buffer's current position 456 * 457 * @throws BufferUnderflowException 458 * If the buffer's current position is not smaller than its limit 459 */ get()460 public abstract byte get(); 461 462 /** 463 * Relative <i>put</i> method <i>(optional operation)</i>. 464 * 465 * <p> Writes the given byte into this buffer at the current 466 * position, and then increments the position. </p> 467 * 468 * @param b 469 * The byte to be written 470 * 471 * @return This buffer 472 * 473 * @throws BufferOverflowException 474 * If this buffer's current position is not smaller than its limit 475 * 476 * @throws ReadOnlyBufferException 477 * If this buffer is read-only 478 */ put(byte b)479 public abstract ByteBuffer put(byte b); 480 481 /** 482 * Absolute <i>get</i> method. Reads the byte at the given 483 * index. 484 * 485 * @param index 486 * The index from which the byte will be read 487 * 488 * @return The byte at the given index 489 * 490 * @throws IndexOutOfBoundsException 491 * If <tt>index</tt> is negative 492 * or not smaller than the buffer's limit 493 */ get(int index)494 public abstract byte get(int index); 495 496 /** 497 * Absolute <i>put</i> method <i>(optional operation)</i>. 498 * 499 * <p> Writes the given byte into this buffer at the given 500 * index. </p> 501 * 502 * @param index 503 * The index at which the byte will be written 504 * 505 * @param b 506 * The byte value to be written 507 * 508 * @return This buffer 509 * 510 * @throws IndexOutOfBoundsException 511 * If <tt>index</tt> is negative 512 * or not smaller than the buffer's limit 513 * 514 * @throws ReadOnlyBufferException 515 * If this buffer is read-only 516 */ put(int index, byte b)517 public abstract ByteBuffer put(int index, byte b); 518 519 520 // -- Bulk get operations -- 521 522 /** 523 * Relative bulk <i>get</i> method. 524 * 525 * <p> This method transfers bytes from this buffer into the given 526 * destination array. If there are fewer bytes remaining in the 527 * buffer than are required to satisfy the request, that is, if 528 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 529 * bytes are transferred and a {@link BufferUnderflowException} is 530 * thrown. 531 * 532 * <p> Otherwise, this method copies <tt>length</tt> bytes from this 533 * buffer into the given array, starting at the current position of this 534 * buffer and at the given offset in the array. The position of this 535 * buffer is then incremented by <tt>length</tt>. 536 * 537 * <p> In other words, an invocation of this method of the form 538 * <tt>src.get(dst, off, len)</tt> has exactly the same effect as 539 * the loop 540 * 541 * <pre>{@code 542 * for (int i = off; i < off + len; i++) 543 * dst[i] = src.get(); 544 * }</pre> 545 * 546 * except that it first checks that there are sufficient bytes in 547 * this buffer and it is potentially much more efficient. 548 * 549 * @param dst 550 * The array into which bytes are to be written 551 * 552 * @param offset 553 * The offset within the array of the first byte to be 554 * written; must be non-negative and no larger than 555 * <tt>dst.length</tt> 556 * 557 * @param length 558 * The maximum number of bytes to be written to the given 559 * array; must be non-negative and no larger than 560 * <tt>dst.length - offset</tt> 561 * 562 * @return This buffer 563 * 564 * @throws BufferUnderflowException 565 * If there are fewer than <tt>length</tt> bytes 566 * remaining in this buffer 567 * 568 * @throws IndexOutOfBoundsException 569 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 570 * parameters do not hold 571 */ get(byte[] dst, int offset, int length)572 public ByteBuffer get(byte[] dst, int offset, int length) { 573 checkBounds(offset, length, dst.length); 574 if (length > remaining()) 575 throw new BufferUnderflowException(); 576 int end = offset + length; 577 for (int i = offset; i < end; i++) 578 dst[i] = get(); 579 return this; 580 } 581 582 /** 583 * Relative bulk <i>get</i> method. 584 * 585 * <p> This method transfers bytes from this buffer into the given 586 * destination array. An invocation of this method of the form 587 * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation 588 * 589 * <pre> 590 * src.get(a, 0, a.length) </pre> 591 * 592 * @param dst 593 * The destination array 594 * 595 * @return This buffer 596 * 597 * @throws BufferUnderflowException 598 * If there are fewer than <tt>length</tt> bytes 599 * remaining in this buffer 600 */ get(byte[] dst)601 public ByteBuffer get(byte[] dst) { 602 return get(dst, 0, dst.length); 603 } 604 605 606 // -- Bulk put operations -- 607 608 /** 609 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 610 * 611 * <p> This method transfers the bytes remaining in the given source 612 * buffer into this buffer. If there are more bytes remaining in the 613 * source buffer than in this buffer, that is, if 614 * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>, 615 * then no bytes are transferred and a {@link 616 * BufferOverflowException} is thrown. 617 * 618 * <p> Otherwise, this method copies 619 * <i>n</i> = <tt>src.remaining()</tt> bytes from the given 620 * buffer into this buffer, starting at each buffer's current position. 621 * The positions of both buffers are then incremented by <i>n</i>. 622 * 623 * <p> In other words, an invocation of this method of the form 624 * <tt>dst.put(src)</tt> has exactly the same effect as the loop 625 * 626 * <pre> 627 * while (src.hasRemaining()) 628 * dst.put(src.get()); </pre> 629 * 630 * except that it first checks that there is sufficient space in this 631 * buffer and it is potentially much more efficient. 632 * 633 * @param src 634 * The source buffer from which bytes are to be read; 635 * must not be this buffer 636 * 637 * @return This buffer 638 * 639 * @throws BufferOverflowException 640 * If there is insufficient space in this buffer 641 * for the remaining bytes in the source buffer 642 * 643 * @throws IllegalArgumentException 644 * If the source buffer is this buffer 645 * 646 * @throws ReadOnlyBufferException 647 * If this buffer is read-only 648 */ put(ByteBuffer src)649 public ByteBuffer put(ByteBuffer src) { 650 if (src == this) 651 throw createSameBufferException(); 652 if (isReadOnly()) 653 throw new ReadOnlyBufferException(); 654 int n = src.remaining(); 655 if (n > remaining()) 656 throw new BufferOverflowException(); 657 658 // Android-changed: improve ByteBuffer.put(ByteBuffer) performance through bulk copy. 659 /* 660 for (int i = 0; i < n; i++) 661 put(src.get()); 662 */ 663 // Note that we use offset instead of arrayOffset because arrayOffset is specified to 664 // throw for read only buffers. Our use of arrayOffset here is provably safe, we only 665 // use it to read *from* readOnly buffers. 666 if (this.hb != null && src.hb != null) { 667 // System.arraycopy is intrinsified by ART and therefore tiny bit faster than memmove 668 System.arraycopy(src.hb, src.position() + src.offset, hb, position() + offset, n); 669 } else { 670 // Use the buffer object (and the raw memory address) if it's a direct buffer. Note that 671 // isDirect() doesn't imply !hasArray(), ByteBuffer.allocateDirect allocated buffer will 672 // have a backing, non-gc-movable byte array. JNI allocated direct byte buffers WILL NOT 673 // have a backing array. 674 final Object srcObject = src.isDirect() ? src : src.hb; 675 int srcOffset = src.position(); 676 if (!src.isDirect()) { 677 srcOffset += src.offset; 678 } 679 680 final ByteBuffer dst = this; 681 final Object dstObject = dst.isDirect() ? dst : dst.hb; 682 int dstOffset = dst.position(); 683 if (!dst.isDirect()) { 684 dstOffset += dst.offset; 685 } 686 Memory.memmove(dstObject, dstOffset, srcObject, srcOffset, n); 687 } 688 src.position(src.limit()); 689 this.position(this.position() + n); 690 return this; 691 } 692 693 /** 694 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 695 * 696 * <p> This method transfers bytes into this buffer from the given 697 * source array. If there are more bytes to be copied from the array 698 * than remain in this buffer, that is, if 699 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no 700 * bytes are transferred and a {@link BufferOverflowException} is 701 * thrown. 702 * 703 * <p> Otherwise, this method copies <tt>length</tt> bytes from the 704 * given array into this buffer, starting at the given offset in the array 705 * and at the current position of this buffer. The position of this buffer 706 * is then incremented by <tt>length</tt>. 707 * 708 * <p> In other words, an invocation of this method of the form 709 * <tt>dst.put(src, off, len)</tt> has exactly the same effect as 710 * the loop 711 * 712 * <pre>{@code 713 * for (int i = off; i < off + len; i++) 714 * dst.put(a[i]); 715 * }</pre> 716 * 717 * except that it first checks that there is sufficient space in this 718 * buffer and it is potentially much more efficient. 719 * 720 * @param src 721 * The array from which bytes are to be read 722 * 723 * @param offset 724 * The offset within the array of the first byte to be read; 725 * must be non-negative and no larger than <tt>array.length</tt> 726 * 727 * @param length 728 * The number of bytes to be read from the given array; 729 * must be non-negative and no larger than 730 * <tt>array.length - offset</tt> 731 * 732 * @return This buffer 733 * 734 * @throws BufferOverflowException 735 * If there is insufficient space in this buffer 736 * 737 * @throws IndexOutOfBoundsException 738 * If the preconditions on the <tt>offset</tt> and <tt>length</tt> 739 * parameters do not hold 740 * 741 * @throws ReadOnlyBufferException 742 * If this buffer is read-only 743 */ put(byte[] src, int offset, int length)744 public ByteBuffer put(byte[] src, int offset, int length) { 745 checkBounds(offset, length, src.length); 746 if (length > remaining()) 747 throw new BufferOverflowException(); 748 int end = offset + length; 749 for (int i = offset; i < end; i++) 750 this.put(src[i]); 751 return this; 752 } 753 754 /** 755 * Relative bulk <i>put</i> method <i>(optional operation)</i>. 756 * 757 * <p> This method transfers the entire content of the given source 758 * byte array into this buffer. An invocation of this method of the 759 * form <tt>dst.put(a)</tt> behaves in exactly the same way as the 760 * invocation 761 * 762 * <pre> 763 * dst.put(a, 0, a.length) </pre> 764 * 765 * @param src 766 * The source array 767 * 768 * @return This buffer 769 * 770 * @throws BufferOverflowException 771 * If there is insufficient space in this buffer 772 * 773 * @throws ReadOnlyBufferException 774 * If this buffer is read-only 775 */ put(byte[] src)776 public final ByteBuffer put(byte[] src) { 777 return put(src, 0, src.length); 778 } 779 780 781 // -- Other stuff -- 782 783 /** 784 * Tells whether or not this buffer is backed by an accessible byte 785 * array. 786 * 787 * <p> If this method returns <tt>true</tt> then the {@link #array() array} 788 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 789 * </p> 790 * 791 * @return <tt>true</tt> if, and only if, this buffer 792 * is backed by an array and is not read-only 793 */ hasArray()794 public final boolean hasArray() { 795 return (hb != null) && !isReadOnly; 796 } 797 798 /** 799 * Returns the byte array that backs this 800 * buffer <i>(optional operation)</i>. 801 * 802 * <p> Modifications to this buffer's content will cause the returned 803 * array's content to be modified, and vice versa. 804 * 805 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 806 * method in order to ensure that this buffer has an accessible backing 807 * array. </p> 808 * 809 * @return The array that backs this buffer 810 * 811 * @throws ReadOnlyBufferException 812 * If this buffer is backed by an array but is read-only 813 * 814 * @throws UnsupportedOperationException 815 * If this buffer is not backed by an accessible array 816 */ array()817 public final byte[] array() { 818 if (hb == null) 819 throw new UnsupportedOperationException(); 820 if (isReadOnly) 821 throw new ReadOnlyBufferException(); 822 return hb; 823 } 824 825 /** 826 * Returns the offset within this buffer's backing array of the first 827 * element of the buffer <i>(optional operation)</i>. 828 * 829 * <p> If this buffer is backed by an array then buffer position <i>p</i> 830 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>. 831 * 832 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 833 * method in order to ensure that this buffer has an accessible backing 834 * array. </p> 835 * 836 * @return The offset within this buffer's array 837 * of the first element of the buffer 838 * 839 * @throws ReadOnlyBufferException 840 * If this buffer is backed by an array but is read-only 841 * 842 * @throws UnsupportedOperationException 843 * If this buffer is not backed by an accessible array 844 */ arrayOffset()845 public final int arrayOffset() { 846 if (hb == null) 847 throw new UnsupportedOperationException(); 848 if (isReadOnly) 849 throw new ReadOnlyBufferException(); 850 return offset; 851 } 852 853 // BEGIN Android-added: covariant overloads of *Buffer methods that return this. 854 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 855 @Override position(int newPosition)856 public Buffer position(int newPosition) { 857 return super.position(newPosition); 858 } 859 860 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 861 @Override limit(int newLimit)862 public Buffer limit(int newLimit) { 863 return super.limit(newLimit); 864 } 865 866 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 867 @Override mark()868 public Buffer mark() { 869 return super.mark(); 870 } 871 872 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 873 @Override reset()874 public Buffer reset() { 875 return super.reset(); 876 } 877 878 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 879 @Override clear()880 public Buffer clear() { 881 return super.clear(); 882 } 883 884 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 885 @Override flip()886 public Buffer flip() { 887 return super.flip(); 888 } 889 890 @CovariantReturnType(returnType = ByteBuffer.class, presentAfter = 28) 891 @Override rewind()892 public Buffer rewind() { 893 return super.rewind(); 894 } 895 // END Android-added: covariant overloads of *Buffer methods that return this. 896 897 /** 898 * Compacts this buffer <i>(optional operation)</i>. 899 * 900 * <p> The bytes between the buffer's current position and its limit, 901 * if any, are copied to the beginning of the buffer. That is, the 902 * byte at index <i>p</i> = <tt>position()</tt> is copied 903 * to index zero, the byte at index <i>p</i> + 1 is copied 904 * to index one, and so forth until the byte at index 905 * <tt>limit()</tt> - 1 is copied to index 906 * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>. 907 * The buffer's position is then set to <i>n+1</i> and its limit is set to 908 * its capacity. The mark, if defined, is discarded. 909 * 910 * <p> The buffer's position is set to the number of bytes copied, 911 * rather than to zero, so that an invocation of this method can be 912 * followed immediately by an invocation of another relative <i>put</i> 913 * method. </p> 914 * 915 916 * 917 * <p> Invoke this method after writing data from a buffer in case the 918 * write was incomplete. The following loop, for example, copies bytes 919 * from one channel to another via the buffer <tt>buf</tt>: 920 * 921 * <blockquote><pre>{@code 922 * buf.clear(); // Prepare buffer for use 923 * while (in.read(buf) >= 0 || buf.position != 0) { 924 * buf.flip(); 925 * out.write(buf); 926 * buf.compact(); // In case of partial write 927 * } 928 * }</pre></blockquote> 929 * 930 931 * 932 * @return This buffer 933 * 934 * @throws ReadOnlyBufferException 935 * If this buffer is read-only 936 */ compact()937 public abstract ByteBuffer compact(); 938 939 /** 940 * Tells whether or not this byte buffer is direct. 941 * 942 * @return <tt>true</tt> if, and only if, this buffer is direct 943 */ isDirect()944 public abstract boolean isDirect(); 945 946 947 /** 948 * Returns a string summarizing the state of this buffer. 949 * 950 * @return A summary string 951 */ toString()952 public String toString() { 953 StringBuffer sb = new StringBuffer(); 954 sb.append(getClass().getName()); 955 sb.append("[pos="); 956 sb.append(position()); 957 sb.append(" lim="); 958 sb.append(limit()); 959 sb.append(" cap="); 960 sb.append(capacity()); 961 sb.append("]"); 962 return sb.toString(); 963 } 964 965 966 /** 967 * Returns the current hash code of this buffer. 968 * 969 * <p> The hash code of a byte buffer depends only upon its remaining 970 * elements; that is, upon the elements from <tt>position()</tt> up to, and 971 * including, the element at <tt>limit()</tt> - <tt>1</tt>. 972 * 973 * <p> Because buffer hash codes are content-dependent, it is inadvisable 974 * to use buffers as keys in hash maps or similar data structures unless it 975 * is known that their contents will not change. </p> 976 * 977 * @return The current hash code of this buffer 978 */ hashCode()979 public int hashCode() { 980 int h = 1; 981 int p = position(); 982 for (int i = limit() - 1; i >= p; i--) 983 h = 31 * h + (int)get(i); 984 return h; 985 } 986 987 /** 988 * Tells whether or not this buffer is equal to another object. 989 * 990 * <p> Two byte buffers are equal if, and only if, 991 * 992 * <ol> 993 * 994 * <li><p> They have the same element type, </p></li> 995 * 996 * <li><p> They have the same number of remaining elements, and 997 * </p></li> 998 * 999 * <li><p> The two sequences of remaining elements, considered 1000 * independently of their starting positions, are pointwise equal. 1001 1002 * </p></li> 1003 * 1004 * </ol> 1005 * 1006 * <p> A byte buffer is not equal to any other type of object. </p> 1007 * 1008 * @param ob The object to which this buffer is to be compared 1009 * 1010 * @return <tt>true</tt> if, and only if, this buffer is equal to the 1011 * given object 1012 */ equals(Object ob)1013 public boolean equals(Object ob) { 1014 if (this == ob) 1015 return true; 1016 if (!(ob instanceof ByteBuffer)) 1017 return false; 1018 ByteBuffer that = (ByteBuffer)ob; 1019 if (this.remaining() != that.remaining()) 1020 return false; 1021 int p = this.position(); 1022 for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) 1023 if (!equals(this.get(i), that.get(j))) 1024 return false; 1025 return true; 1026 } 1027 equals(byte x, byte y)1028 private static boolean equals(byte x, byte y) { 1029 1030 1031 return x == y; 1032 1033 } 1034 1035 /** 1036 * Compares this buffer to another. 1037 * 1038 * <p> Two byte buffers are compared by comparing their sequences of 1039 * remaining elements lexicographically, without regard to the starting 1040 * position of each sequence within its corresponding buffer. 1041 * 1042 * 1043 * 1044 * 1045 * 1046 * 1047 * 1048 * 1049 * Pairs of {@code byte} elements are compared as if by invoking 1050 * {@link Byte#compare(byte,byte)}. 1051 1052 * 1053 * <p> A byte buffer is not comparable to any other type of object. 1054 * 1055 * @return A negative integer, zero, or a positive integer as this buffer 1056 * is less than, equal to, or greater than the given buffer 1057 */ compareTo(ByteBuffer that)1058 public int compareTo(ByteBuffer that) { 1059 int n = this.position() + Math.min(this.remaining(), that.remaining()); 1060 for (int i = this.position(), j = that.position(); i < n; i++, j++) { 1061 int cmp = compare(this.get(i), that.get(j)); 1062 if (cmp != 0) 1063 return cmp; 1064 } 1065 return this.remaining() - that.remaining(); 1066 } 1067 compare(byte x, byte y)1068 private static int compare(byte x, byte y) { 1069 1070 1071 return Byte.compare(x, y); 1072 1073 } 1074 1075 /** 1076 * Finds and returns the relative index of the first mismatch between this 1077 * buffer and a given buffer. The index is relative to the 1078 * {@link #position() position} of each buffer and will be in the range of 1079 * 0 (inclusive) up to the smaller of the {@link #remaining() remaining} 1080 * elements in each buffer (exclusive). 1081 * 1082 * <p> If the two buffers share a common prefix then the returned index is 1083 * the length of the common prefix and it follows that there is a mismatch 1084 * between the two buffers at that index within the respective buffers. 1085 * If one buffer is a proper prefix of the other then the returned index is 1086 * the smaller of the remaining elements in each buffer, and it follows that 1087 * the index is only valid for the buffer with the larger number of 1088 * remaining elements. 1089 * Otherwise, there is no mismatch. 1090 * 1091 * @param that 1092 * The byte buffer to be tested for a mismatch with this buffer 1093 * 1094 * @return The relative index of the first mismatch between this and the 1095 * given buffer, otherwise -1 if no mismatch. 1096 * 1097 * @since 11 1098 */ mismatch(ByteBuffer that)1099 public int mismatch(ByteBuffer that) { 1100 int thisPos = this.position(); 1101 int thisRem = this.limit() - thisPos; 1102 int thatPos = that.position(); 1103 int thatRem = that.limit() - thatPos; 1104 int length = Math.min(thisRem, thatRem); 1105 if (length < 0) 1106 return -1; 1107 int r = BufferMismatch.mismatch(this, thisPos, 1108 that, thatPos, 1109 length); 1110 return (r == -1 && thisRem != thatRem) ? length : r; 1111 } 1112 1113 // -- Other char stuff -- 1114 1115 1116 // -- Other byte stuff: Access to binary data -- 1117 1118 1119 boolean bigEndian // package-private 1120 = true; 1121 boolean nativeByteOrder // package-private 1122 = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN); 1123 1124 /** 1125 * Retrieves this buffer's byte order. 1126 * 1127 * <p> The byte order is used when reading or writing multibyte values, and 1128 * when creating buffers that are views of this byte buffer. The order of 1129 * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN 1130 * BIG_ENDIAN}. </p> 1131 * 1132 * @return This buffer's byte order 1133 */ order()1134 public final ByteOrder order() { 1135 return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; 1136 } 1137 1138 /** 1139 * Modifies this buffer's byte order. 1140 * 1141 * @param bo 1142 * The new byte order, 1143 * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN} 1144 * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN} 1145 * 1146 * @return This buffer 1147 */ order(ByteOrder bo)1148 public final ByteBuffer order(ByteOrder bo) { 1149 bigEndian = (bo == ByteOrder.BIG_ENDIAN); 1150 nativeByteOrder = 1151 (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN)); 1152 return this; 1153 } 1154 1155 /** 1156 * Returns the memory address, pointing to the byte at the given index, 1157 * modulus the given unit size. 1158 * 1159 * <p> A return value greater than zero indicates the address of the byte at 1160 * the index is misaligned for the unit size, and the value's quantity 1161 * indicates how much the index should be rounded up or down to locate a 1162 * byte at an aligned address. Otherwise, a value of {@code 0} indicates 1163 * that the address of the byte at the index is aligned for the unit size. 1164 * 1165 * @apiNote 1166 * This method may be utilized to determine if unit size bytes from an 1167 * index can be accessed atomically, if supported by the native platform. 1168 * 1169 * @implNote 1170 * This implementation throws {@code UnsupportedOperationException} for 1171 * non-direct buffers when the given unit size is greater than {@code 8}. 1172 * 1173 * @param index 1174 * The index to query for alignment offset, must be non-negative, no 1175 * upper bounds check is performed 1176 * 1177 * @param unitSize 1178 * The unit size in bytes, must be a power of {@code 2} 1179 * 1180 * @return The indexed byte's memory address modulus the unit size 1181 * 1182 * @throws IllegalArgumentException 1183 * If the index is negative or the unit size is not a power of 1184 * {@code 2} 1185 * 1186 * @throws UnsupportedOperationException 1187 * If the native platform does not guarantee stable alignment offset 1188 * values for the given unit size when managing the memory regions 1189 * of buffers of the same kind as this buffer (direct or 1190 * non-direct). For example, if garbage collection would result 1191 * in the moving of a memory region covered by a non-direct buffer 1192 * from one location to another and both locations have different 1193 * alignment characteristics. 1194 * 1195 * @see #alignedSlice(int) 1196 * @since 9 1197 */ alignmentOffset(int index, int unitSize)1198 public final int alignmentOffset(int index, int unitSize) { 1199 if (index < 0) 1200 throw new IllegalArgumentException("Index less than zero: " + index); 1201 if (unitSize < 1 || (unitSize & (unitSize - 1)) != 0) 1202 throw new IllegalArgumentException("Unit size not a power of two: " + unitSize); 1203 if (unitSize > 8 && !isDirect()) 1204 throw new UnsupportedOperationException("Unit size unsupported for non-direct buffers: " + unitSize); 1205 1206 // BEGIN Android-changed: Android specific alignment calculation. 1207 // return (int) ((address + index) % unitSize); 1208 final long baseAddress = 1209 isDirect() ? address : (Unsafe.getUnsafe().arrayBaseOffset(byte[].class) + offset); 1210 1211 final long elementAddress = baseAddress + index; 1212 return (int) (elementAddress & (unitSize - 1)); 1213 // END Android-changed: Android specific alignment calculation. 1214 } 1215 1216 /** 1217 * Creates a new byte buffer whose content is a shared and aligned 1218 * subsequence of this buffer's content. 1219 * 1220 * <p> The content of the new buffer will start at this buffer's current 1221 * position rounded up to the index of the nearest aligned byte for the 1222 * given unit size, and end at this buffer's limit rounded down to the index 1223 * of the nearest aligned byte for the given unit size. 1224 * If rounding results in out-of-bound values then the new buffer's capacity 1225 * and limit will be zero. If rounding is within bounds the following 1226 * expressions will be true for a new buffer {@code nb} and unit size 1227 * {@code unitSize}: 1228 * <pre>{@code 1229 * nb.alignmentOffset(0, unitSize) == 0 1230 * nb.alignmentOffset(nb.limit(), unitSize) == 0 1231 * }</pre> 1232 * 1233 * <p> Changes to this buffer's content will be visible in the new 1234 * buffer, and vice versa; the two buffers' position, limit, and mark 1235 * values will be independent. 1236 * 1237 * <p> The new buffer's position will be zero, its capacity and its limit 1238 * will be the number of bytes remaining in this buffer or fewer subject to 1239 * alignment, its mark will be undefined, and its byte order will be 1240 * {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. 1241 * 1242 * The new buffer will be direct if, and only if, this buffer is direct, and 1243 * it will be read-only if, and only if, this buffer is read-only. </p> 1244 * 1245 * @apiNote 1246 * This method may be utilized to create a new buffer where unit size bytes 1247 * from index, that is a multiple of the unit size, may be accessed 1248 * atomically, if supported by the native platform. 1249 * 1250 * @implNote 1251 * This implementation throws {@code UnsupportedOperationException} for 1252 * non-direct buffers when the given unit size is greater than {@code 8}. 1253 * 1254 * @param unitSize 1255 * The unit size in bytes, must be a power of {@code 2} 1256 * 1257 * @return The new byte buffer 1258 * 1259 * @throws IllegalArgumentException 1260 * If the unit size not a power of {@code 2} 1261 * 1262 * @throws UnsupportedOperationException 1263 * If the native platform does not guarantee stable aligned slices 1264 * for the given unit size when managing the memory regions 1265 * of buffers of the same kind as this buffer (direct or 1266 * non-direct). For example, if garbage collection would result 1267 * in the moving of a memory region covered by a non-direct buffer 1268 * from one location to another and both locations have different 1269 * alignment characteristics. 1270 * 1271 * @see #alignmentOffset(int, int) 1272 * @see #slice() 1273 * @since 9 1274 */ alignedSlice(int unitSize)1275 public final ByteBuffer alignedSlice(int unitSize) { 1276 int pos = position(); 1277 int lim = limit(); 1278 1279 int pos_mod = alignmentOffset(pos, unitSize); 1280 int lim_mod = alignmentOffset(lim, unitSize); 1281 1282 // Round up the position to align with unit size 1283 int aligned_pos = (pos_mod > 0) 1284 ? pos + (unitSize - pos_mod) 1285 : pos; 1286 1287 // Round down the limit to align with unit size 1288 int aligned_lim = lim - lim_mod; 1289 1290 if (aligned_pos > lim || aligned_lim < pos) { 1291 aligned_pos = aligned_lim = pos; 1292 } 1293 1294 return slice(aligned_pos, aligned_lim - aligned_pos); 1295 } 1296 1297 // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes 1298 // _get(int i)1299 abstract byte _get(int i); // package-private _put(int i, byte b)1300 abstract void _put(int i, byte b); // package-private 1301 1302 1303 /** 1304 * Relative <i>get</i> method for reading a char value. 1305 * 1306 * <p> Reads the next two bytes at this buffer's current position, 1307 * composing them into a char value according to the current byte order, 1308 * and then increments the position by two. </p> 1309 * 1310 * @return The char value at the buffer's current position 1311 * 1312 * @throws BufferUnderflowException 1313 * If there are fewer than two bytes 1314 * remaining in this buffer 1315 */ getChar()1316 public abstract char getChar(); 1317 1318 /** 1319 * Relative <i>put</i> method for writing a char 1320 * value <i>(optional operation)</i>. 1321 * 1322 * <p> Writes two bytes containing the given char value, in the 1323 * current byte order, into this buffer at the current position, and then 1324 * increments the position by two. </p> 1325 * 1326 * @param value 1327 * The char value to be written 1328 * 1329 * @return This buffer 1330 * 1331 * @throws BufferOverflowException 1332 * If there are fewer than two bytes 1333 * remaining in this buffer 1334 * 1335 * @throws ReadOnlyBufferException 1336 * If this buffer is read-only 1337 */ putChar(char value)1338 public abstract ByteBuffer putChar(char value); 1339 1340 /** 1341 * Absolute <i>get</i> method for reading a char value. 1342 * 1343 * <p> Reads two bytes at the given index, composing them into a 1344 * char value according to the current byte order. </p> 1345 * 1346 * @param index 1347 * The index from which the bytes will be read 1348 * 1349 * @return The char value at the given index 1350 * 1351 * @throws IndexOutOfBoundsException 1352 * If <tt>index</tt> is negative 1353 * or not smaller than the buffer's limit, 1354 * minus one 1355 */ getChar(int index)1356 public abstract char getChar(int index); 1357 1358 // BEGIN Android-added: {get,put}*Unchecked() accessors. getCharUnchecked(int index)1359 abstract char getCharUnchecked(int index); getUnchecked(int pos, char[] dst, int dstOffset, int length)1360 abstract void getUnchecked(int pos, char[] dst, int dstOffset, int length); 1361 // END Android-added: {get,put}*Unchecked() accessors. 1362 1363 /** 1364 * Absolute <i>put</i> method for writing a char 1365 * value <i>(optional operation)</i>. 1366 * 1367 * <p> Writes two bytes containing the given char value, in the 1368 * current byte order, into this buffer at the given index. </p> 1369 * 1370 * @param index 1371 * The index at which the bytes will be written 1372 * 1373 * @param value 1374 * The char value to be written 1375 * 1376 * @return This buffer 1377 * 1378 * @throws IndexOutOfBoundsException 1379 * If <tt>index</tt> is negative 1380 * or not smaller than the buffer's limit, 1381 * minus one 1382 * 1383 * @throws ReadOnlyBufferException 1384 * If this buffer is read-only 1385 */ putChar(int index, char value)1386 public abstract ByteBuffer putChar(int index, char value); 1387 1388 // BEGIN Android-added: {get,put}*Unchecked() accessors. putCharUnchecked(int index, char value)1389 abstract void putCharUnchecked(int index, char value); putUnchecked(int pos, char[] dst, int srcOffset, int length)1390 abstract void putUnchecked(int pos, char[] dst, int srcOffset, int length); 1391 // END Android-added: {get,put}*Unchecked() accessors. 1392 1393 /** 1394 * Creates a view of this byte buffer as a char buffer. 1395 * 1396 * <p> The content of the new buffer will start at this buffer's current 1397 * position. Changes to this buffer's content will be visible in the new 1398 * buffer, and vice versa; the two buffers' position, limit, and mark 1399 * values will be independent. 1400 * 1401 * <p> The new buffer's position will be zero, its capacity and its limit 1402 * will be the number of bytes remaining in this buffer divided by 1403 * two, and its mark will be undefined. The new buffer will be direct 1404 * if, and only if, this buffer is direct, and it will be read-only if, and 1405 * only if, this buffer is read-only. </p> 1406 * 1407 * @return A new char buffer 1408 */ asCharBuffer()1409 public abstract CharBuffer asCharBuffer(); 1410 1411 1412 /** 1413 * Relative <i>get</i> method for reading a short value. 1414 * 1415 * <p> Reads the next two bytes at this buffer's current position, 1416 * composing them into a short value according to the current byte order, 1417 * and then increments the position by two. </p> 1418 * 1419 * @return The short value at the buffer's current position 1420 * 1421 * @throws BufferUnderflowException 1422 * If there are fewer than two bytes 1423 * remaining in this buffer 1424 */ getShort()1425 public abstract short getShort(); 1426 1427 /** 1428 * Relative <i>put</i> method for writing a short 1429 * value <i>(optional operation)</i>. 1430 * 1431 * <p> Writes two bytes containing the given short value, in the 1432 * current byte order, into this buffer at the current position, and then 1433 * increments the position by two. </p> 1434 * 1435 * @param value 1436 * The short value to be written 1437 * 1438 * @return This buffer 1439 * 1440 * @throws BufferOverflowException 1441 * If there are fewer than two bytes 1442 * remaining in this buffer 1443 * 1444 * @throws ReadOnlyBufferException 1445 * If this buffer is read-only 1446 */ putShort(short value)1447 public abstract ByteBuffer putShort(short value); 1448 1449 /** 1450 * Absolute <i>get</i> method for reading a short value. 1451 * 1452 * <p> Reads two bytes at the given index, composing them into a 1453 * short value according to the current byte order. </p> 1454 * 1455 * @param index 1456 * The index from which the bytes will be read 1457 * 1458 * @return The short value at the given index 1459 * 1460 * @throws IndexOutOfBoundsException 1461 * If <tt>index</tt> is negative 1462 * or not smaller than the buffer's limit, 1463 * minus one 1464 */ getShort(int index)1465 public abstract short getShort(int index); 1466 1467 // BEGIN Android-added: {get,put}*Unchecked() accessors. getShortUnchecked(int index)1468 abstract short getShortUnchecked(int index); getUnchecked(int pos, short[] dst, int dstOffset, int length)1469 abstract void getUnchecked(int pos, short[] dst, int dstOffset, int length); 1470 // END Android-added: {get,put}*Unchecked() accessors. 1471 1472 /** 1473 * Absolute <i>put</i> method for writing a short 1474 * value <i>(optional operation)</i>. 1475 * 1476 * <p> Writes two bytes containing the given short value, in the 1477 * current byte order, into this buffer at the given index. </p> 1478 * 1479 * @param index 1480 * The index at which the bytes will be written 1481 * 1482 * @param value 1483 * The short value to be written 1484 * 1485 * @return This buffer 1486 * 1487 * @throws IndexOutOfBoundsException 1488 * If <tt>index</tt> is negative 1489 * or not smaller than the buffer's limit, 1490 * minus one 1491 * 1492 * @throws ReadOnlyBufferException 1493 * If this buffer is read-only 1494 */ putShort(int index, short value)1495 public abstract ByteBuffer putShort(int index, short value); 1496 1497 // BEGIN Android-added: {get,put}*Unchecked() accessors. putShortUnchecked(int index, short value)1498 abstract void putShortUnchecked(int index, short value); putUnchecked(int pos, short[] dst, int srcOffset, int length)1499 abstract void putUnchecked(int pos, short[] dst, int srcOffset, int length); 1500 // END Android-added: {get,put}*Unchecked() accessors. 1501 1502 /** 1503 * Creates a view of this byte buffer as a short buffer. 1504 * 1505 * <p> The content of the new buffer will start at this buffer's current 1506 * position. Changes to this buffer's content will be visible in the new 1507 * buffer, and vice versa; the two buffers' position, limit, and mark 1508 * values will be independent. 1509 * 1510 * <p> The new buffer's position will be zero, its capacity and its limit 1511 * will be the number of bytes remaining in this buffer divided by 1512 * two, and its mark will be undefined. The new buffer will be direct 1513 * if, and only if, this buffer is direct, and it will be read-only if, and 1514 * only if, this buffer is read-only. </p> 1515 * 1516 * @return A new short buffer 1517 */ asShortBuffer()1518 public abstract ShortBuffer asShortBuffer(); 1519 1520 1521 /** 1522 * Relative <i>get</i> method for reading an int value. 1523 * 1524 * <p> Reads the next four bytes at this buffer's current position, 1525 * composing them into an int value according to the current byte order, 1526 * and then increments the position by four. </p> 1527 * 1528 * @return The int value at the buffer's current position 1529 * 1530 * @throws BufferUnderflowException 1531 * If there are fewer than four bytes 1532 * remaining in this buffer 1533 */ getInt()1534 public abstract int getInt(); 1535 1536 /** 1537 * Relative <i>put</i> method for writing an int 1538 * value <i>(optional operation)</i>. 1539 * 1540 * <p> Writes four bytes containing the given int value, in the 1541 * current byte order, into this buffer at the current position, and then 1542 * increments the position by four. </p> 1543 * 1544 * @param value 1545 * The int value to be written 1546 * 1547 * @return This buffer 1548 * 1549 * @throws BufferOverflowException 1550 * If there are fewer than four bytes 1551 * remaining in this buffer 1552 * 1553 * @throws ReadOnlyBufferException 1554 * If this buffer is read-only 1555 */ putInt(int value)1556 public abstract ByteBuffer putInt(int value); 1557 1558 /** 1559 * Absolute <i>get</i> method for reading an int value. 1560 * 1561 * <p> Reads four bytes at the given index, composing them into a 1562 * int value according to the current byte order. </p> 1563 * 1564 * @param index 1565 * The index from which the bytes will be read 1566 * 1567 * @return The int value at the given index 1568 * 1569 * @throws IndexOutOfBoundsException 1570 * If <tt>index</tt> is negative 1571 * or not smaller than the buffer's limit, 1572 * minus three 1573 */ getInt(int index)1574 public abstract int getInt(int index); 1575 1576 // BEGIN Android-added: {get,put}*Unchecked() accessors. getIntUnchecked(int index)1577 abstract int getIntUnchecked(int index); getUnchecked(int pos, int[] dst, int dstOffset, int length)1578 abstract void getUnchecked(int pos, int[] dst, int dstOffset, int length); 1579 // END Android-added: {get,put}*Unchecked() accessors. 1580 1581 /** 1582 * Absolute <i>put</i> method for writing an int 1583 * value <i>(optional operation)</i>. 1584 * 1585 * <p> Writes four bytes containing the given int value, in the 1586 * current byte order, into this buffer at the given index. </p> 1587 * 1588 * @param index 1589 * The index at which the bytes will be written 1590 * 1591 * @param value 1592 * The int value to be written 1593 * 1594 * @return This buffer 1595 * 1596 * @throws IndexOutOfBoundsException 1597 * If <tt>index</tt> is negative 1598 * or not smaller than the buffer's limit, 1599 * minus three 1600 * 1601 * @throws ReadOnlyBufferException 1602 * If this buffer is read-only 1603 */ putInt(int index, int value)1604 public abstract ByteBuffer putInt(int index, int value); 1605 1606 // BEGIN Android-added: {get,put}*Unchecked() accessors. putIntUnchecked(int index, int value)1607 abstract void putIntUnchecked(int index, int value); putUnchecked(int pos, int[] dst, int srcOffset, int length)1608 abstract void putUnchecked(int pos, int[] dst, int srcOffset, int length); 1609 // END Android-added: {get,put}*Unchecked() accessors. 1610 1611 /** 1612 * Creates a view of this byte buffer as an int buffer. 1613 * 1614 * <p> The content of the new buffer will start at this buffer's current 1615 * position. Changes to this buffer's content will be visible in the new 1616 * buffer, and vice versa; the two buffers' position, limit, and mark 1617 * values will be independent. 1618 * 1619 * <p> The new buffer's position will be zero, its capacity and its limit 1620 * will be the number of bytes remaining in this buffer divided by 1621 * four, and its mark will be undefined. The new buffer will be direct 1622 * if, and only if, this buffer is direct, and it will be read-only if, and 1623 * only if, this buffer is read-only. </p> 1624 * 1625 * @return A new int buffer 1626 */ asIntBuffer()1627 public abstract IntBuffer asIntBuffer(); 1628 1629 1630 /** 1631 * Relative <i>get</i> method for reading a long value. 1632 * 1633 * <p> Reads the next eight bytes at this buffer's current position, 1634 * composing them into a long value according to the current byte order, 1635 * and then increments the position by eight. </p> 1636 * 1637 * @return The long value at the buffer's current position 1638 * 1639 * @throws BufferUnderflowException 1640 * If there are fewer than eight bytes 1641 * remaining in this buffer 1642 */ getLong()1643 public abstract long getLong(); 1644 1645 /** 1646 * Relative <i>put</i> method for writing a long 1647 * value <i>(optional operation)</i>. 1648 * 1649 * <p> Writes eight bytes containing the given long value, in the 1650 * current byte order, into this buffer at the current position, and then 1651 * increments the position by eight. </p> 1652 * 1653 * @param value 1654 * The long value to be written 1655 * 1656 * @return This buffer 1657 * 1658 * @throws BufferOverflowException 1659 * If there are fewer than eight bytes 1660 * remaining in this buffer 1661 * 1662 * @throws ReadOnlyBufferException 1663 * If this buffer is read-only 1664 */ putLong(long value)1665 public abstract ByteBuffer putLong(long value); 1666 1667 /** 1668 * Absolute <i>get</i> method for reading a long value. 1669 * 1670 * <p> Reads eight bytes at the given index, composing them into a 1671 * long value according to the current byte order. </p> 1672 * 1673 * @param index 1674 * The index from which the bytes will be read 1675 * 1676 * @return The long value at the given index 1677 * 1678 * @throws IndexOutOfBoundsException 1679 * If <tt>index</tt> is negative 1680 * or not smaller than the buffer's limit, 1681 * minus seven 1682 */ getLong(int index)1683 public abstract long getLong(int index); 1684 1685 // BEGIN Android-added: {get,put}*Unchecked() accessors. getLongUnchecked(int index)1686 abstract long getLongUnchecked(int index); getUnchecked(int pos, long[] dst, int dstOffset, int length)1687 abstract void getUnchecked(int pos, long[] dst, int dstOffset, int length); 1688 // END Android-added: {get,put}*Unchecked() accessors. 1689 1690 /** 1691 * Absolute <i>put</i> method for writing a long 1692 * value <i>(optional operation)</i>. 1693 * 1694 * <p> Writes eight bytes containing the given long value, in the 1695 * current byte order, into this buffer at the given index. </p> 1696 * 1697 * @param index 1698 * The index at which the bytes will be written 1699 * 1700 * @param value 1701 * The long value to be written 1702 * 1703 * @return This buffer 1704 * 1705 * @throws IndexOutOfBoundsException 1706 * If <tt>index</tt> is negative 1707 * or not smaller than the buffer's limit, 1708 * minus seven 1709 * 1710 * @throws ReadOnlyBufferException 1711 * If this buffer is read-only 1712 */ putLong(int index, long value)1713 public abstract ByteBuffer putLong(int index, long value); 1714 1715 // BEGIN Android-added: {get,put}*Unchecked() accessors. putLongUnchecked(int index, long value)1716 abstract void putLongUnchecked(int index, long value); putUnchecked(int pos, long[] dst, int srcOffset, int length)1717 abstract void putUnchecked(int pos, long[] dst, int srcOffset, int length); 1718 // END Android-added: {get,put}*Unchecked() accessors. 1719 1720 /** 1721 * Creates a view of this byte buffer as a long buffer. 1722 * 1723 * <p> The content of the new buffer will start at this buffer's current 1724 * position. Changes to this buffer's content will be visible in the new 1725 * buffer, and vice versa; the two buffers' position, limit, and mark 1726 * values will be independent. 1727 * 1728 * <p> The new buffer's position will be zero, its capacity and its limit 1729 * will be the number of bytes remaining in this buffer divided by 1730 * eight, and its mark will be undefined. The new buffer will be direct 1731 * if, and only if, this buffer is direct, and it will be read-only if, and 1732 * only if, this buffer is read-only. </p> 1733 * 1734 * @return A new long buffer 1735 */ asLongBuffer()1736 public abstract LongBuffer asLongBuffer(); 1737 1738 1739 /** 1740 * Relative <i>get</i> method for reading a float value. 1741 * 1742 * <p> Reads the next four bytes at this buffer's current position, 1743 * composing them into a float value according to the current byte order, 1744 * and then increments the position by four. </p> 1745 * 1746 * @return The float value at the buffer's current position 1747 * 1748 * @throws BufferUnderflowException 1749 * If there are fewer than four bytes 1750 * remaining in this buffer 1751 */ getFloat()1752 public abstract float getFloat(); 1753 1754 /** 1755 * Relative <i>put</i> method for writing a float 1756 * value <i>(optional operation)</i>. 1757 * 1758 * <p> Writes four bytes containing the given float value, in the 1759 * current byte order, into this buffer at the current position, and then 1760 * increments the position by four. </p> 1761 * 1762 * @param value 1763 * The float value to be written 1764 * 1765 * @return This buffer 1766 * 1767 * @throws BufferOverflowException 1768 * If there are fewer than four bytes 1769 * remaining in this buffer 1770 * 1771 * @throws ReadOnlyBufferException 1772 * If this buffer is read-only 1773 */ putFloat(float value)1774 public abstract ByteBuffer putFloat(float value); 1775 1776 /** 1777 * Absolute <i>get</i> method for reading a float value. 1778 * 1779 * <p> Reads four bytes at the given index, composing them into a 1780 * float value according to the current byte order. </p> 1781 * 1782 * @param index 1783 * The index from which the bytes will be read 1784 * 1785 * @return The float value at the given index 1786 * 1787 * @throws IndexOutOfBoundsException 1788 * If <tt>index</tt> is negative 1789 * or not smaller than the buffer's limit, 1790 * minus three 1791 */ getFloat(int index)1792 public abstract float getFloat(int index); 1793 1794 // BEGIN Android-added: {get,put}*Unchecked() accessors. getFloatUnchecked(int index)1795 abstract float getFloatUnchecked(int index); getUnchecked(int pos, float[] dst, int dstOffset, int length)1796 abstract void getUnchecked(int pos, float[] dst, int dstOffset, int length); 1797 // END Android-added: {get,put}*Unchecked() accessors. 1798 1799 /** 1800 * Absolute <i>put</i> method for writing a float 1801 * value <i>(optional operation)</i>. 1802 * 1803 * <p> Writes four bytes containing the given float value, in the 1804 * current byte order, into this buffer at the given index. </p> 1805 * 1806 * @param index 1807 * The index at which the bytes will be written 1808 * 1809 * @param value 1810 * The float value to be written 1811 * 1812 * @return This buffer 1813 * 1814 * @throws IndexOutOfBoundsException 1815 * If <tt>index</tt> is negative 1816 * or not smaller than the buffer's limit, 1817 * minus three 1818 * 1819 * @throws ReadOnlyBufferException 1820 * If this buffer is read-only 1821 */ putFloat(int index, float value)1822 public abstract ByteBuffer putFloat(int index, float value); 1823 1824 // BEGIN Android-added: {get,put}*Unchecked() accessors. putFloatUnchecked(int index, float value)1825 abstract void putFloatUnchecked(int index, float value); putUnchecked(int pos, float[] dst, int srcOffset, int length)1826 abstract void putUnchecked(int pos, float[] dst, int srcOffset, int length); 1827 // END Android-added: {get,put}*Unchecked() accessors. 1828 1829 /** 1830 * Creates a view of this byte buffer as a float buffer. 1831 * 1832 * <p> The content of the new buffer will start at this buffer's current 1833 * position. Changes to this buffer's content will be visible in the new 1834 * buffer, and vice versa; the two buffers' position, limit, and mark 1835 * values will be independent. 1836 * 1837 * <p> The new buffer's position will be zero, its capacity and its limit 1838 * will be the number of bytes remaining in this buffer divided by 1839 * four, and its mark will be undefined. The new buffer will be direct 1840 * if, and only if, this buffer is direct, and it will be read-only if, and 1841 * only if, this buffer is read-only. </p> 1842 * 1843 * @return A new float buffer 1844 */ asFloatBuffer()1845 public abstract FloatBuffer asFloatBuffer(); 1846 1847 1848 /** 1849 * Relative <i>get</i> method for reading a double value. 1850 * 1851 * <p> Reads the next eight bytes at this buffer's current position, 1852 * composing them into a double value according to the current byte order, 1853 * and then increments the position by eight. </p> 1854 * 1855 * @return The double value at the buffer's current position 1856 * 1857 * @throws BufferUnderflowException 1858 * If there are fewer than eight bytes 1859 * remaining in this buffer 1860 */ getDouble()1861 public abstract double getDouble(); 1862 1863 /** 1864 * Relative <i>put</i> method for writing a double 1865 * value <i>(optional operation)</i>. 1866 * 1867 * <p> Writes eight bytes containing the given double value, in the 1868 * current byte order, into this buffer at the current position, and then 1869 * increments the position by eight. </p> 1870 * 1871 * @param value 1872 * The double value to be written 1873 * 1874 * @return This buffer 1875 * 1876 * @throws BufferOverflowException 1877 * If there are fewer than eight bytes 1878 * remaining in this buffer 1879 * 1880 * @throws ReadOnlyBufferException 1881 * If this buffer is read-only 1882 */ putDouble(double value)1883 public abstract ByteBuffer putDouble(double value); 1884 1885 /** 1886 * Absolute <i>get</i> method for reading a double value. 1887 * 1888 * <p> Reads eight bytes at the given index, composing them into a 1889 * double value according to the current byte order. </p> 1890 * 1891 * @param index 1892 * The index from which the bytes will be read 1893 * 1894 * @return The double value at the given index 1895 * 1896 * @throws IndexOutOfBoundsException 1897 * If <tt>index</tt> is negative 1898 * or not smaller than the buffer's limit, 1899 * minus seven 1900 */ getDouble(int index)1901 public abstract double getDouble(int index); 1902 1903 // BEGIN Android-added: {get,put}*Unchecked() accessors. getDoubleUnchecked(int index)1904 abstract double getDoubleUnchecked(int index); getUnchecked(int pos, double[] dst, int dstOffset, int length)1905 abstract void getUnchecked(int pos, double[] dst, int dstOffset, int length); 1906 // END Android-added: {get,put}*Unchecked() accessors. 1907 1908 /** 1909 * Absolute <i>put</i> method for writing a double 1910 * value <i>(optional operation)</i>. 1911 * 1912 * <p> Writes eight bytes containing the given double value, in the 1913 * current byte order, into this buffer at the given index. </p> 1914 * 1915 * @param index 1916 * The index at which the bytes will be written 1917 * 1918 * @param value 1919 * The double value to be written 1920 * 1921 * @return This buffer 1922 * 1923 * @throws IndexOutOfBoundsException 1924 * If <tt>index</tt> is negative 1925 * or not smaller than the buffer's limit, 1926 * minus seven 1927 * 1928 * @throws ReadOnlyBufferException 1929 * If this buffer is read-only 1930 */ putDouble(int index, double value)1931 public abstract ByteBuffer putDouble(int index, double value); 1932 1933 // BEGIN Android-added: {get,put}*Unchecked() accessors. putDoubleUnchecked(int index, double value)1934 abstract void putDoubleUnchecked(int index, double value); putUnchecked(int pos, double[] dst, int srcOffset, int length)1935 abstract void putUnchecked(int pos, double[] dst, int srcOffset, int length); 1936 // END Android-added: {get,put}*Unchecked() accessors. 1937 1938 /** 1939 * Creates a view of this byte buffer as a double buffer. 1940 * 1941 * <p> The content of the new buffer will start at this buffer's current 1942 * position. Changes to this buffer's content will be visible in the new 1943 * buffer, and vice versa; the two buffers' position, limit, and mark 1944 * values will be independent. 1945 * 1946 * <p> The new buffer's position will be zero, its capacity and its limit 1947 * will be the number of bytes remaining in this buffer divided by 1948 * eight, and its mark will be undefined. The new buffer will be direct 1949 * if, and only if, this buffer is direct, and it will be read-only if, and 1950 * only if, this buffer is read-only. </p> 1951 * 1952 * @return A new double buffer 1953 */ asDoubleBuffer()1954 public abstract DoubleBuffer asDoubleBuffer(); 1955 1956 // BEGIN Android-added: isAccessible(), setAccessible(), for use by frameworks (MediaCodec). 1957 /** 1958 * @hide 1959 */ isAccessible()1960 public boolean isAccessible() { 1961 return true; 1962 } 1963 1964 /** 1965 * @hide 1966 */ setAccessible(boolean value)1967 public void setAccessible(boolean value) { 1968 throw new UnsupportedOperationException(); 1969 } 1970 // END Android-added: isAccessible(), setAccessible(), for use by frameworks (MediaCodec). 1971 } 1972