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