Home | History | Annotate | Download | only in nio
      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 
     32 import java.io.IOException;
     33 import java.util.Spliterator;
     34 import java.util.stream.StreamSupport;
     35 import java.util.stream.IntStream;
     36 
     37 
     38 /**
     39  * A char buffer.
     40  *
     41  * <p> This class defines four categories of operations upon
     42  * char buffers:
     43  *
     44  * <ul>
     45  *
     46  * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
     47  * {@link #put(char) </code><i>put</i><code>} methods that read and write
     48  * single chars; </p></li>
     49  *
     50  * <li><p> Relative {@link #get(char[]) </code><i>bulk get</i><code>}
     51  * methods that transfer contiguous sequences of chars from this buffer
     52  * into an array; and</p></li>
     53  *
     54  * <li><p> Relative {@link #put(char[]) </code><i>bulk put</i><code>}
     55  * methods that transfer contiguous sequences of chars from a
     56  * char array,&#32;a&#32;string, or some other char
     57  * buffer into this buffer;&#32;and </p></li>
     58  *
     59  * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
     60  * #duplicate </code>duplicating<code>}, and {@link #slice
     61  * </code>slicing<code>} a char buffer.  </p></li>
     62  *
     63  * </ul>
     64  *
     65  * <p> Char buffers can be created either by {@link #allocate
     66  * </code><i>allocation</i><code>}, which allocates space for the buffer's
     67  * content, by {@link #wrap(char[]) </code><i>wrapping</i><code>} an existing
     68  * char array or&#32;string into a buffer, or by creating a
     69  * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer.
     70  *
     71  * <p> Like a byte buffer, a char buffer is either <a
     72  * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>.  A
     73  * char buffer created via the <tt>wrap</tt> methods of this class will
     74  * be non-direct.  A char buffer created as a view of a byte buffer will
     75  * be direct if, and only if, the byte buffer itself is direct.  Whether or not
     76  * a char buffer is direct may be determined by invoking the {@link
     77  * #isDirect isDirect} method.  </p>
     78  *
     79  * <p> This class implements the {@link CharSequence} interface so that
     80  * character buffers may be used wherever character sequences are accepted, for
     81  * example in the regular-expression package <tt>{@link java.util.regex}</tt>.
     82  * </p>
     83  *
     84  * <p> Methods in this class that do not otherwise have a value to return are
     85  * specified to return the buffer upon which they are invoked.  This allows
     86  * method invocations to be chained.
     87  *
     88  *
     89  * The sequence of statements
     90  *
     91  * <blockquote><pre>
     92  * cb.put("text/");
     93  * cb.put(subtype);
     94  * cb.put("; charset=");
     95  * cb.put(enc);</pre></blockquote>
     96  *
     97  * can, for example, be replaced by the single statement
     98  *
     99  * <blockquote><pre>
    100  * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote>
    101  *
    102  * @author Mark Reinhold
    103  * @author JSR-51 Expert Group
    104  * @since 1.4
    105  */
    106 
    107 public abstract class CharBuffer
    108         extends Buffer
    109         implements Comparable<CharBuffer>, Appendable, CharSequence, Readable {
    110 
    111     // These fields are declared here rather than in Heap-X-Buffer in order to
    112     // reduce the number of virtual method invocations needed to access these
    113     // values, which is especially costly when coding small buffers.
    114     //
    115     final char[] hb;                  // Non-null only for heap buffers
    116     final int offset;
    117     boolean isReadOnly;                 // Valid only for heap buffers
    118 
    119     // Creates a new buffer with the given mark, position, limit, capacity,
    120     // backing array, and array offset
    121     //
    122     CharBuffer(int mark, int pos, int lim, int cap,   // package-private
    123                char[] hb, int offset) {
    124         super(mark, pos, lim, cap, 1);
    125         this.hb = hb;
    126         this.offset = offset;
    127     }
    128 
    129     // Creates a new buffer with the given mark, position, limit, and capacity
    130     //
    131     CharBuffer(int mark, int pos, int lim, int cap) { // package-private
    132         this(mark, pos, lim, cap, null, 0);
    133     }
    134 
    135 
    136     /**
    137      * Allocates a new char buffer.
    138      *
    139      * <p> The new buffer's position will be zero, its limit will be its
    140      * capacity, its mark will be undefined, and each of its elements will be
    141      * initialized to zero.  It will have a {@link #array
    142      * </code>backing array<code>}, and its {@link #arrayOffset </code>array
    143      * offset<code>} will be zero.
    144      *
    145      * @param capacity The new buffer's capacity, in chars
    146      * @return The new char buffer
    147      * @throws IllegalArgumentException If the <tt>capacity</tt> is a negative integer
    148      */
    149     public static CharBuffer allocate(int capacity) {
    150         if (capacity < 0)
    151             throw new IllegalArgumentException();
    152         return new HeapCharBuffer(capacity, capacity);
    153     }
    154 
    155     /**
    156      * Wraps a char array into a buffer.
    157      *
    158      * <p> The new buffer will be backed by the given char array;
    159      * that is, modifications to the buffer will cause the array to be modified
    160      * and vice versa.  The new buffer's capacity will be
    161      * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
    162      * will be <tt>offset + length</tt>, and its mark will be undefined.  Its
    163      * {@link #array </code>backing array<code>} will be the given array, and
    164      * its {@link #arrayOffset </code>array offset<code>} will be zero.  </p>
    165      *
    166      * @param array  The array that will back the new buffer
    167      * @param offset The offset of the subarray to be used; must be non-negative and
    168      *               no larger than <tt>array.length</tt>.  The new buffer's position
    169      *               will be set to this value.
    170      * @param length The length of the subarray to be used;
    171      *               must be non-negative and no larger than
    172      *               <tt>array.length - offset</tt>.
    173      *               The new buffer's limit will be set to <tt>offset + length</tt>.
    174      * @return The new char buffer
    175      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
    176      *                                   <tt>length</tt>
    177      *                                   parameters do not hold
    178      */
    179     public static CharBuffer wrap(char[] array,
    180                                   int offset, int length) {
    181         try {
    182             return new HeapCharBuffer(array, offset, length);
    183         } catch (IllegalArgumentException x) {
    184             throw new IndexOutOfBoundsException();
    185         }
    186     }
    187 
    188     /**
    189      * Wraps a char array into a buffer.
    190      *
    191      * <p> The new buffer will be backed by the given char array;
    192      * that is, modifications to the buffer will cause the array to be modified
    193      * and vice versa.  The new buffer's capacity and limit will be
    194      * <tt>array.length</tt>, its position will be zero, and its mark will be
    195      * undefined.  Its {@link #array </code>backing array<code>} will be the
    196      * given array, and its {@link #arrayOffset </code>array offset<code>} will
    197      * be zero.  </p>
    198      *
    199      * @param array The array that will back this buffer
    200      * @return The new char buffer
    201      */
    202     public static CharBuffer wrap(char[] array) {
    203         return wrap(array, 0, array.length);
    204     }
    205 
    206 
    207     /**
    208      * Attempts to read characters into the specified character buffer.
    209      * The buffer is used as a repository of characters as-is: the only
    210      * changes made are the results of a put operation. No flipping or
    211      * rewinding of the buffer is performed.
    212      *
    213      * @param target the buffer to read characters into
    214      * @return The number of characters added to the buffer, or
    215      * -1 if this source of characters is at its end
    216      * @throws IOException             if an I/O error occurs
    217      * @throws NullPointerException    if target is null
    218      * @throws ReadOnlyBufferException if target is a read only buffer
    219      * @since 1.5
    220      */
    221     public int read(CharBuffer target) throws IOException {
    222         // Determine the number of bytes n that can be transferred
    223         int targetRemaining = target.remaining();
    224         int remaining = remaining();
    225         if (remaining == 0)
    226             return -1;
    227         int n = Math.min(remaining, targetRemaining);
    228         int limit = limit();
    229         // Set source limit to prevent target overflow
    230         if (targetRemaining < remaining)
    231             limit(position() + n);
    232         try {
    233             if (n > 0)
    234                 target.put(this);
    235         } finally {
    236             limit(limit); // restore real limit
    237         }
    238         return n;
    239     }
    240 
    241     /**
    242      * Wraps a character sequence into a buffer.
    243      *
    244      * <p> The content of the new, read-only buffer will be the content of the
    245      * given character sequence.  The buffer's capacity will be
    246      * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit
    247      * will be <tt>end</tt>, and its mark will be undefined.  </p>
    248      *
    249      * @param csq   The character sequence from which the new character buffer is to
    250      *              be created
    251      * @param start The index of the first character to be used;
    252      *              must be non-negative and no larger than <tt>csq.length()</tt>.
    253      *              The new buffer's position will be set to this value.
    254      * @param end   The index of the character following the last character to be
    255      *              used; must be no smaller than <tt>start</tt> and no larger
    256      *              than <tt>csq.length()</tt>.
    257      *              The new buffer's limit will be set to this value.
    258      * @return The new character buffer
    259      * @throws IndexOutOfBoundsException If the preconditions on the <tt>start</tt> and
    260      *                                   <tt>end</tt>
    261      *                                   parameters do not hold
    262      */
    263     public static CharBuffer wrap(CharSequence csq, int start, int end) {
    264         try {
    265             return new StringCharBuffer(csq, start, end);
    266         } catch (IllegalArgumentException x) {
    267             throw new IndexOutOfBoundsException();
    268         }
    269     }
    270 
    271     /**
    272      * Wraps a character sequence into a buffer.
    273      *
    274      * <p> The content of the new, read-only buffer will be the content of the
    275      * given character sequence.  The new buffer's capacity and limit will be
    276      * <tt>csq.length()</tt>, its position will be zero, and its mark will be
    277      * undefined.  </p>
    278      *
    279      * @param csq The character sequence from which the new character buffer is to
    280      *            be created
    281      * @return The new character buffer
    282      */
    283     public static CharBuffer wrap(CharSequence csq) {
    284         return wrap(csq, 0, csq.length());
    285     }
    286 
    287 
    288     /**
    289      * Creates a new char buffer whose content is a shared subsequence of
    290      * this buffer's content.
    291      *
    292      * <p> The content of the new buffer will start at this buffer's current
    293      * position.  Changes to this buffer's content will be visible in the new
    294      * buffer, and vice versa; the two buffers' position, limit, and mark
    295      * values will be independent.
    296      *
    297      * <p> The new buffer's position will be zero, its capacity and its limit
    298      * will be the number of chars remaining in this buffer, and its mark
    299      * will be undefined.  The new buffer will be direct if, and only if, this
    300      * buffer is direct, and it will be read-only if, and only if, this buffer
    301      * is read-only.  </p>
    302      *
    303      * @return The new char buffer
    304      */
    305     public abstract CharBuffer slice();
    306 
    307     /**
    308      * Creates a new char buffer that shares this buffer's content.
    309      *
    310      * <p> The content of the new buffer will be that of this buffer.  Changes
    311      * to this buffer's content will be visible in the new buffer, and vice
    312      * versa; the two buffers' position, limit, and mark values will be
    313      * independent.
    314      *
    315      * <p> The new buffer's capacity, limit, position, and mark values will be
    316      * identical to those of this buffer.  The new buffer will be direct if,
    317      * and only if, this buffer is direct, and it will be read-only if, and
    318      * only if, this buffer is read-only.  </p>
    319      *
    320      * @return The new char buffer
    321      */
    322     public abstract CharBuffer duplicate();
    323 
    324     /**
    325      * Creates a new, read-only char buffer that shares this buffer's
    326      * content.
    327      *
    328      * <p> The content of the new buffer will be that of this buffer.  Changes
    329      * to this buffer's content will be visible in the new buffer; the new
    330      * buffer itself, however, will be read-only and will not allow the shared
    331      * content to be modified.  The two buffers' position, limit, and mark
    332      * values will be independent.
    333      *
    334      * <p> The new buffer's capacity, limit, position, and mark values will be
    335      * identical to those of this buffer.
    336      *
    337      * <p> If this buffer is itself read-only then this method behaves in
    338      * exactly the same way as the {@link #duplicate duplicate} method.  </p>
    339      *
    340      * @return The new, read-only char buffer
    341      */
    342     public abstract CharBuffer asReadOnlyBuffer();
    343 
    344 
    345     // -- Singleton get/put methods --
    346 
    347     /**
    348      * Relative <i>get</i> method.  Reads the char at this buffer's
    349      * current position, and then increments the position. </p>
    350      *
    351      * @return The char at the buffer's current position
    352      * @throws BufferUnderflowException If the buffer's current position is not smaller than its
    353      *                                  limit
    354      */
    355     public abstract char get();
    356 
    357     /**
    358      * Relative <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    359      *
    360      * <p> Writes the given char into this buffer at the current
    361      * position, and then increments the position. </p>
    362      *
    363      * @param c The char to be written
    364      * @return This buffer
    365      * @throws BufferOverflowException If this buffer's current position is not smaller than its
    366      *                                 limit
    367      * @throws ReadOnlyBufferException If this buffer is read-only
    368      */
    369     public abstract CharBuffer put(char c);
    370 
    371     /**
    372      * Absolute <i>get</i> method.  Reads the char at the given
    373      * index. </p>
    374      *
    375      * @param index The index from which the char will be read
    376      * @return The char at the given index
    377      * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
    378      *                                   or not smaller than the buffer's limit
    379      */
    380     public abstract char get(int index);
    381 
    382     /**
    383      * Absolute <i>get</i> method.  Reads the char at the given
    384      * index without any validation of the index.
    385      *
    386      * @param  index
    387      *         The index from which the char will be read
    388      *
    389      * @return  The char at the given index
    390      */
    391     abstract char getUnchecked(int index);   // package-private
    392 
    393     /**
    394      * Absolute <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    395      *
    396      * <p> Writes the given char into this buffer at the given
    397      * index. </p>
    398      *
    399      * @param index The index at which the char will be written
    400      * @param c     The char value to be written
    401      * @return This buffer
    402      * @throws IndexOutOfBoundsException If <tt>index</tt> is negative
    403      *                                   or not smaller than the buffer's limit
    404      * @throws ReadOnlyBufferException   If this buffer is read-only
    405      */
    406     public abstract CharBuffer put(int index, char c);
    407 
    408 
    409     // -- Bulk get operations --
    410 
    411     /**
    412      * Relative bulk <i>get</i> method.
    413      *
    414      * <p> This method transfers chars from this buffer into the given
    415      * destination array.  If there are fewer chars remaining in the
    416      * buffer than are required to satisfy the request, that is, if
    417      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
    418      * chars are transferred and a {@link BufferUnderflowException} is
    419      * thrown.
    420      *
    421      * <p> Otherwise, this method copies <tt>length</tt> chars from this
    422      * buffer into the given array, starting at the current position of this
    423      * buffer and at the given offset in the array.  The position of this
    424      * buffer is then incremented by <tt>length</tt>.
    425      *
    426      * <p> In other words, an invocation of this method of the form
    427      * <tt>src.get(dst,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
    428      * the loop
    429      *
    430      * <pre>
    431      *     for (int i = off; i < off + len; i++)
    432      *         dst[i] = src.get(); </pre>
    433      *
    434      * except that it first checks that there are sufficient chars in
    435      * this buffer and it is potentially much more efficient. </p>
    436      *
    437      * @param dst    The array into which chars are to be written
    438      * @param offset The offset within the array of the first char to be
    439      *               written; must be non-negative and no larger than
    440      *               <tt>dst.length</tt>
    441      * @param length The maximum number of chars to be written to the given
    442      *               array; must be non-negative and no larger than
    443      *               <tt>dst.length - offset</tt>
    444      * @return This buffer
    445      * @throws BufferUnderflowException  If there are fewer than <tt>length</tt> chars
    446      *                                   remaining in this buffer
    447      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
    448      *                                   <tt>length</tt>
    449      *                                   parameters do not hold
    450      */
    451     public CharBuffer get(char[] dst, int offset, int length) {
    452         checkBounds(offset, length, dst.length);
    453         if (length > remaining())
    454             throw new BufferUnderflowException();
    455         int end = offset + length;
    456         for (int i = offset; i < end; i++)
    457             dst[i] = get();
    458         return this;
    459     }
    460 
    461     /**
    462      * Relative bulk <i>get</i> method.
    463      *
    464      * <p> This method transfers chars from this buffer into the given
    465      * destination array.  An invocation of this method of the form
    466      * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
    467      *
    468      * <pre>
    469      *     src.get(a, 0, a.length) </pre>
    470      *
    471      * @return This buffer
    472      * @throws BufferUnderflowException If there are fewer than <tt>length</tt> chars
    473      *                                  remaining in this buffer
    474      */
    475     public CharBuffer get(char[] dst) {
    476         return get(dst, 0, dst.length);
    477     }
    478 
    479 
    480     // -- Bulk put operations --
    481 
    482     /**
    483      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    484      *
    485      * <p> This method transfers the chars remaining in the given source
    486      * buffer into this buffer.  If there are more chars remaining in the
    487      * source buffer than in this buffer, that is, if
    488      * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
    489      * then no chars are transferred and a {@link
    490      * BufferOverflowException} is thrown.
    491      *
    492      * <p> Otherwise, this method copies
    493      * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> chars from the given
    494      * buffer into this buffer, starting at each buffer's current position.
    495      * The positions of both buffers are then incremented by <i>n</i>.
    496      *
    497      * <p> In other words, an invocation of this method of the form
    498      * <tt>dst.put(src)</tt> has exactly the same effect as the loop
    499      *
    500      * <pre>
    501      *     while (src.hasRemaining())
    502      *         dst.put(src.get()); </pre>
    503      *
    504      * except that it first checks that there is sufficient space in this
    505      * buffer and it is potentially much more efficient. </p>
    506      *
    507      * @param src The source buffer from which chars are to be read;
    508      *            must not be this buffer
    509      * @return This buffer
    510      * @throws BufferOverflowException  If there is insufficient space in this buffer
    511      *                                  for the remaining chars in the source buffer
    512      * @throws IllegalArgumentException If the source buffer is this buffer
    513      * @throws ReadOnlyBufferException  If this buffer is read-only
    514      */
    515     public CharBuffer put(CharBuffer src) {
    516         if (src == this)
    517             throw new IllegalArgumentException();
    518         int n = src.remaining();
    519         if (n > remaining())
    520             throw new BufferOverflowException();
    521         for (int i = 0; i < n; i++)
    522             put(src.get());
    523         return this;
    524     }
    525 
    526     /**
    527      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    528      *
    529      * <p> This method transfers chars into this buffer from the given
    530      * source array.  If there are more chars to be copied from the array
    531      * than remain in this buffer, that is, if
    532      * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
    533      * chars are transferred and a {@link BufferOverflowException} is
    534      * thrown.
    535      *
    536      * <p> Otherwise, this method copies <tt>length</tt> chars from the
    537      * given array into this buffer, starting at the given offset in the array
    538      * and at the current position of this buffer.  The position of this buffer
    539      * is then incremented by <tt>length</tt>.
    540      *
    541      * <p> In other words, an invocation of this method of the form
    542      * <tt>dst.put(src,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
    543      * the loop
    544      *
    545      * <pre>
    546      *     for (int i = off; i < off + len; i++)
    547      *         dst.put(a[i]); </pre>
    548      *
    549      * except that it first checks that there is sufficient space in this
    550      * buffer and it is potentially much more efficient. </p>
    551      *
    552      * @param src    The array from which chars are to be read
    553      * @param offset The offset within the array of the first char to be read;
    554      *               must be non-negative and no larger than <tt>array.length</tt>
    555      * @param length The number of chars to be read from the given array;
    556      *               must be non-negative and no larger than
    557      *               <tt>array.length - offset</tt>
    558      * @return This buffer
    559      * @throws BufferOverflowException   If there is insufficient space in this buffer
    560      * @throws IndexOutOfBoundsException If the preconditions on the <tt>offset</tt> and
    561      *                                   <tt>length</tt>
    562      *                                   parameters do not hold
    563      * @throws ReadOnlyBufferException   If this buffer is read-only
    564      */
    565     public CharBuffer put(char[] src, int offset, int length) {
    566         checkBounds(offset, length, src.length);
    567         if (length > remaining())
    568             throw new BufferOverflowException();
    569         int end = offset + length;
    570         for (int i = offset; i < end; i++)
    571             this.put(src[i]);
    572         return this;
    573     }
    574 
    575     /**
    576      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    577      *
    578      * <p> This method transfers the entire content of the given source
    579      * char array into this buffer.  An invocation of this method of the
    580      * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
    581      * invocation
    582      *
    583      * <pre>
    584      *     dst.put(a, 0, a.length) </pre>
    585      *
    586      * @return This buffer
    587      * @throws BufferOverflowException If there is insufficient space in this buffer
    588      * @throws ReadOnlyBufferException If this buffer is read-only
    589      */
    590     public final CharBuffer put(char[] src) {
    591         return put(src, 0, src.length);
    592     }
    593 
    594 
    595     /**
    596      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    597      *
    598      * <p> This method transfers chars from the given string into this
    599      * buffer.  If there are more chars to be copied from the string than
    600      * remain in this buffer, that is, if
    601      * <tt>end&nbsp;-&nbsp;start</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
    602      * then no chars are transferred and a {@link
    603      * BufferOverflowException} is thrown.
    604      *
    605      * <p> Otherwise, this method copies
    606      * <i>n</i>&nbsp;=&nbsp;<tt>end</tt>&nbsp;-&nbsp;<tt>start</tt> chars
    607      * from the given string into this buffer, starting at the given
    608      * <tt>start</tt> index and at the current position of this buffer.  The
    609      * position of this buffer is then incremented by <i>n</i>.
    610      *
    611      * <p> In other words, an invocation of this method of the form
    612      * <tt>dst.put(src,&nbsp;start,&nbsp;end)</tt> has exactly the same effect
    613      * as the loop
    614      *
    615      * <pre>
    616      *     for (int i = start; i < end; i++)
    617      *         dst.put(src.charAt(i)); </pre>
    618      *
    619      * except that it first checks that there is sufficient space in this
    620      * buffer and it is potentially much more efficient. </p>
    621      *
    622      * @param src   The string from which chars are to be read
    623      * @param start The offset within the string of the first char to be read;
    624      *              must be non-negative and no larger than
    625      *              <tt>string.length()</tt>
    626      * @param end   The offset within the string of the last char to be read,
    627      *              plus one; must be non-negative and no larger than
    628      *              <tt>string.length()</tt>
    629      * @return This buffer
    630      * @throws BufferOverflowException   If there is insufficient space in this buffer
    631      * @throws IndexOutOfBoundsException If the preconditions on the <tt>start</tt> and
    632      *                                   <tt>end</tt>
    633      *                                   parameters do not hold
    634      * @throws ReadOnlyBufferException   If this buffer is read-only
    635      */
    636     public CharBuffer put(String src, int start, int end) {
    637         checkBounds(start, end - start, src.length());
    638 
    639         // Android-changed: Don't bother making changes to the buffer if there's nothing
    640         // to write. This is questionable behaviour but code expects it.
    641         if (start == end) {
    642             return this;
    643         }
    644 
    645         // Android-changed: Throw ReadOnlyBufferException as soon as possible.
    646         if (isReadOnly()) {
    647             throw new ReadOnlyBufferException();
    648         }
    649 
    650         // Android-changed: Throw as early as we can if there isn't enough space.
    651         if ((end - start) > remaining()) {
    652             throw new BufferOverflowException();
    653         }
    654 
    655         for (int i = start; i < end; i++)
    656             this.put(src.charAt(i));
    657         return this;
    658     }
    659 
    660     /**
    661      * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
    662      *
    663      * <p> This method transfers the entire content of the given source string
    664      * into this buffer.  An invocation of this method of the form
    665      * <tt>dst.put(s)</tt> behaves in exactly the same way as the invocation
    666      *
    667      * <pre>
    668      *     dst.put(s, 0, s.length()) </pre>
    669      *
    670      * @return This buffer
    671      * @throws BufferOverflowException If there is insufficient space in this buffer
    672      * @throws ReadOnlyBufferException If this buffer is read-only
    673      */
    674     public final CharBuffer put(String src) {
    675         return put(src, 0, src.length());
    676     }
    677 
    678 
    679     // -- Other stuff --
    680 
    681     /**
    682      * Tells whether or not this buffer is backed by an accessible char
    683      * array.
    684      *
    685      * <p> If this method returns <tt>true</tt> then the {@link #array() array}
    686      * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
    687      * </p>
    688      *
    689      * @return <tt>true</tt> if, and only if, this buffer
    690      * is backed by an array and is not read-only
    691      */
    692     public final boolean hasArray() {
    693         return (hb != null) && !isReadOnly;
    694     }
    695 
    696     /**
    697      * Returns the char array that backs this
    698      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
    699      *
    700      * <p> Modifications to this buffer's content will cause the returned
    701      * array's content to be modified, and vice versa.
    702      *
    703      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
    704      * method in order to ensure that this buffer has an accessible backing
    705      * array.  </p>
    706      *
    707      * @return The array that backs this buffer
    708      * @throws ReadOnlyBufferException       If this buffer is backed by an array but is read-only
    709      * @throws UnsupportedOperationException If this buffer is not backed by an accessible array
    710      */
    711     public final char[] array() {
    712         if (hb == null)
    713             throw new UnsupportedOperationException();
    714         if (isReadOnly)
    715             throw new ReadOnlyBufferException();
    716         return hb;
    717     }
    718 
    719     /**
    720      * Returns the offset within this buffer's backing array of the first
    721      * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
    722      *
    723      * <p> If this buffer is backed by an array then buffer position <i>p</i>
    724      * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
    725      *
    726      * <p> Invoke the {@link #hasArray hasArray} method before invoking this
    727      * method in order to ensure that this buffer has an accessible backing
    728      * array.  </p>
    729      *
    730      * @return The offset within this buffer's array
    731      * of the first element of the buffer
    732      * @throws ReadOnlyBufferException       If this buffer is backed by an array but is read-only
    733      * @throws UnsupportedOperationException If this buffer is not backed by an accessible array
    734      */
    735     public final int arrayOffset() {
    736         if (hb == null)
    737             throw new UnsupportedOperationException();
    738         if (isReadOnly)
    739             throw new ReadOnlyBufferException();
    740         return offset;
    741     }
    742 
    743     /**
    744      * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
    745      *
    746      * <p> The chars between the buffer's current position and its limit,
    747      * if any, are copied to the beginning of the buffer.  That is, the
    748      * char at index <i>p</i>&nbsp;=&nbsp;<tt>position()</tt> is copied
    749      * to index zero, the char at index <i>p</i>&nbsp;+&nbsp;1 is copied
    750      * to index one, and so forth until the char at index
    751      * <tt>limit()</tt>&nbsp;-&nbsp;1 is copied to index
    752      * <i>n</i>&nbsp;=&nbsp;<tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>&nbsp;-&nbsp;<i>p</i>.
    753      * The buffer's position is then set to <i>n+1</i> and its limit is set to
    754      * its capacity.  The mark, if defined, is discarded.
    755      *
    756      * <p> The buffer's position is set to the number of chars copied,
    757      * rather than to zero, so that an invocation of this method can be
    758      * followed immediately by an invocation of another relative <i>put</i>
    759      * method. </p>
    760      *
    761      * @return This buffer
    762      * @throws ReadOnlyBufferException If this buffer is read-only
    763      */
    764     public abstract CharBuffer compact();
    765 
    766     /**
    767      * Tells whether or not this char buffer is direct. </p>
    768      *
    769      * @return <tt>true</tt> if, and only if, this buffer is direct
    770      */
    771     public abstract boolean isDirect();
    772 
    773 
    774     /**
    775      * Returns the current hash code of this buffer.
    776      *
    777      * <p> The hash code of a char buffer depends only upon its remaining
    778      * elements; that is, upon the elements from <tt>position()</tt> up to, and
    779      * including, the element at <tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>.
    780      *
    781      * <p> Because buffer hash codes are content-dependent, it is inadvisable
    782      * to use buffers as keys in hash maps or similar data structures unless it
    783      * is known that their contents will not change.  </p>
    784      *
    785      * @return The current hash code of this buffer
    786      */
    787     public int hashCode() {
    788         int h = 1;
    789         int p = position();
    790         for (int i = limit() - 1; i >= p; i--)
    791             h = 31 * h + (int) get(i);
    792         return h;
    793     }
    794 
    795     /**
    796      * Tells whether or not this buffer is equal to another object.
    797      *
    798      * <p> Two char buffers are equal if, and only if,
    799      *
    800      * <p><ol>
    801      *
    802      * <li><p> They have the same element type,  </p></li>
    803      *
    804      * <li><p> They have the same number of remaining elements, and
    805      * </p></li>
    806      *
    807      * <li><p> The two sequences of remaining elements, considered
    808      * independently of their starting positions, are pointwise equal.
    809      *
    810      *
    811      *
    812      *
    813      *
    814      *
    815      *
    816      * </p></li>
    817      *
    818      * </ol>
    819      *
    820      * <p> A char buffer is not equal to any other type of object.  </p>
    821      *
    822      * @param ob The object to which this buffer is to be compared
    823      * @return <tt>true</tt> if, and only if, this buffer is equal to the
    824      * given object
    825      */
    826     public boolean equals(Object ob) {
    827         if (this == ob)
    828             return true;
    829         if (!(ob instanceof CharBuffer))
    830             return false;
    831         CharBuffer that = (CharBuffer) ob;
    832         if (this.remaining() != that.remaining())
    833             return false;
    834         int p = this.position();
    835         for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--)
    836             if (!equals(this.get(i), that.get(j)))
    837                 return false;
    838         return true;
    839     }
    840 
    841     private static boolean equals(char x, char y) {
    842 
    843 
    844         return x == y;
    845 
    846     }
    847 
    848     /**
    849      * Compares this buffer to another.
    850      *
    851      * <p> Two char buffers are compared by comparing their sequences of
    852      * remaining elements lexicographically, without regard to the starting
    853      * position of each sequence within its corresponding buffer.
    854      *
    855      *
    856      *
    857      *
    858      *
    859      *
    860      *
    861      *
    862      * Pairs of {@code char} elements are compared as if by invoking
    863      * {@link Character#compare(char, char)}.
    864      *
    865      *
    866      * <p> A char buffer is not comparable to any other type of object.
    867      *
    868      * @return A negative integer, zero, or a positive integer as this buffer
    869      * is less than, equal to, or greater than the given buffer
    870      */
    871     public int compareTo(CharBuffer that) {
    872         int n = this.position() + Math.min(this.remaining(), that.remaining());
    873         for (int i = this.position(), j = that.position(); i < n; i++, j++) {
    874             int cmp = compare(this.get(i), that.get(j));
    875             if (cmp != 0)
    876                 return cmp;
    877         }
    878         return this.remaining() - that.remaining();
    879     }
    880 
    881     private static int compare(char x, char y) {
    882 
    883 
    884         return Character.compare(x, y);
    885 
    886     }
    887 
    888     // -- Other char stuff --
    889 
    890 
    891     /**
    892      * Returns a string containing the characters in this buffer.
    893      *
    894      * <p> The first character of the resulting string will be the character at
    895      * this buffer's position, while the last character will be the character
    896      * at index <tt>limit()</tt>&nbsp;-&nbsp;1.  Invoking this method does not
    897      * change the buffer's position. </p>
    898      *
    899      * @return The specified string
    900      */
    901     public String toString() {
    902         return toString(position(), limit());
    903     }
    904 
    905     abstract String toString(int start, int end);       // package-private
    906 
    907 
    908     // --- Methods to support CharSequence ---
    909 
    910     /**
    911      * Returns the length of this character buffer.
    912      *
    913      * <p> When viewed as a character sequence, the length of a character
    914      * buffer is simply the number of characters between the position
    915      * (inclusive) and the limit (exclusive); that is, it is equivalent to
    916      * <tt>remaining()</tt>. </p>
    917      *
    918      * @return The length of this character buffer
    919      */
    920     public final int length() {
    921         return remaining();
    922     }
    923 
    924     /**
    925      * Reads the character at the given index relative to the current
    926      * position. </p>
    927      *
    928      * @param index The index of the character to be read, relative to the position;
    929      *              must be non-negative and smaller than <tt>remaining()</tt>
    930      * @return The character at index
    931      * <tt>position()&nbsp;+&nbsp;index</tt>
    932      * @throws IndexOutOfBoundsException If the preconditions on <tt>index</tt> do not hold
    933      */
    934     public final char charAt(int index) {
    935         return get(position() + checkIndex(index, 1));
    936     }
    937 
    938     /**
    939      * Creates a new character buffer that represents the specified subsequence
    940      * of this buffer, relative to the current position.
    941      *
    942      * <p> The new buffer will share this buffer's content; that is, if the
    943      * content of this buffer is mutable then modifications to one buffer will
    944      * cause the other to be modified.  The new buffer's capacity will be that
    945      * of this buffer, its position will be
    946      * <tt>position()</tt>&nbsp;+&nbsp;<tt>start</tt>, and its limit will be
    947      * <tt>position()</tt>&nbsp;+&nbsp;<tt>end</tt>.  The new buffer will be
    948      * direct if, and only if, this buffer is direct, and it will be read-only
    949      * if, and only if, this buffer is read-only.  </p>
    950      *
    951      * @param start The index, relative to the current position, of the first
    952      *              character in the subsequence; must be non-negative and no larger
    953      *              than <tt>remaining()</tt>
    954      * @param end   The index, relative to the current position, of the character
    955      *              following the last character in the subsequence; must be no
    956      *              smaller than <tt>start</tt> and no larger than
    957      *              <tt>remaining()</tt>
    958      * @return The new character buffer
    959      * @throws IndexOutOfBoundsException If the preconditions on <tt>start</tt> and <tt>end</tt>
    960      *                                   do not hold
    961      */
    962     public abstract CharBuffer subSequence(int start, int end);
    963 
    964 
    965     // --- Methods to support Appendable ---
    966 
    967     /**
    968      * Appends the specified character sequence  to this
    969      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
    970      *
    971      * <p> An invocation of this method of the form <tt>dst.append(csq)</tt>
    972      * behaves in exactly the same way as the invocation
    973      *
    974      * <pre>
    975      *     dst.put(csq.toString()) </pre>
    976      *
    977      * <p> Depending on the specification of <tt>toString</tt> for the
    978      * character sequence <tt>csq</tt>, the entire sequence may not be
    979      * appended.  For instance, invoking the {@link CharBuffer#toString()
    980      * toString} method of a character buffer will return a subsequence whose
    981      * content depends upon the buffer's position and limit.
    982      *
    983      * @param csq The character sequence to append.  If <tt>csq</tt> is
    984      *            <tt>null</tt>, then the four characters <tt>"null"</tt> are
    985      *            appended to this character buffer.
    986      * @return This buffer
    987      * @throws BufferOverflowException If there is insufficient space in this buffer
    988      * @throws ReadOnlyBufferException If this buffer is read-only
    989      * @since 1.5
    990      */
    991     public CharBuffer append(CharSequence csq) {
    992         if (csq == null)
    993             return put("null");
    994         else
    995             return put(csq.toString());
    996     }
    997 
    998     /**
    999      * Appends a subsequence of the  specified character sequence  to this
   1000      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
   1001      *
   1002      * <p> An invocation of this method of the form <tt>dst.append(csq, start,
   1003      * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in exactly the
   1004      * same way as the invocation
   1005      *
   1006      * <pre>
   1007      *     dst.put(csq.subSequence(start, end).toString()) </pre>
   1008      *
   1009      * @param csq The character sequence from which a subsequence will be
   1010      *            appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
   1011      *            will be appended as if <tt>csq</tt> contained the four
   1012      *            characters <tt>"null"</tt>.
   1013      * @return This buffer
   1014      * @throws BufferOverflowException   If there is insufficient space in this buffer
   1015      * @throws IndexOutOfBoundsException If <tt>start</tt> or <tt>end</tt> are negative,
   1016      *                                   <tt>start</tt>
   1017      *                                   is greater than <tt>end</tt>, or <tt>end</tt> is greater
   1018      *                                   than
   1019      *                                   <tt>csq.length()</tt>
   1020      * @throws ReadOnlyBufferException   If this buffer is read-only
   1021      * @since 1.5
   1022      */
   1023     public CharBuffer append(CharSequence csq, int start, int end) {
   1024         CharSequence cs = (csq == null ? "null" : csq);
   1025         return put(cs.subSequence(start, end).toString());
   1026     }
   1027 
   1028     /**
   1029      * Appends the specified char  to this
   1030      * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
   1031      *
   1032      * <p> An invocation of this method of the form <tt>dst.append(c)</tt>
   1033      * behaves in exactly the same way as the invocation
   1034      *
   1035      * <pre>
   1036      *     dst.put(c) </pre>
   1037      *
   1038      * @param c The 16-bit char to append
   1039      * @return This buffer
   1040      * @throws BufferOverflowException If there is insufficient space in this buffer
   1041      * @throws ReadOnlyBufferException If this buffer is read-only
   1042      * @since 1.5
   1043      */
   1044     public CharBuffer append(char c) {
   1045         return put(c);
   1046     }
   1047 
   1048 
   1049     // -- Other byte stuff: Access to binary data --
   1050 
   1051 
   1052     /**
   1053      * Retrieves this buffer's byte order.
   1054      *
   1055      * <p> The byte order of a char buffer created by allocation or by
   1056      * wrapping an existing <tt>char</tt> array is the {@link
   1057      * ByteOrder#nativeOrder </code>native order<code>} of the underlying
   1058      * hardware.  The byte order of a char buffer created as a <a
   1059      * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the
   1060      * byte buffer at the moment that the view is created.  </p>
   1061      *
   1062      * @return This buffer's byte order
   1063      */
   1064     public abstract ByteOrder order();
   1065 
   1066     @Override
   1067     public IntStream chars() {
   1068         CharBuffer self = this;
   1069         return StreamSupport.intStream(() -> new CharBufferSpliterator(self),
   1070             Buffer.SPLITERATOR_CHARACTERISTICS, false);
   1071     }
   1072 }
   1073