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