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