1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26
27#warn This file is preprocessed before being compiled
28// Android-note: This file is generated by ojluni/src/tools/gensrc_android.sh.
29
30package java.nio;
31
32import java.util.Objects;
33import libcore.io.Memory;
34
35/**
36#if[rw]
37 * A read/write Heap$Type$Buffer.
38#else[rw]
39 * A read-only Heap$Type$Buffer.  This class extends the corresponding
40 * read/write class, overriding the mutation methods to throw a {@link
41 * ReadOnlyBufferException} and overriding the view-buffer methods to return an
42 * instance of this class rather than of the superclass.
43#end[rw]
44 */
45// Android-changed: Make it final as no subclasses exist.
46final class Heap$Type$Buffer$RW$
47    extends {#if[ro]?Heap}$Type$Buffer
48{
49    // Android-removed: Removed unused constants.
50    /*
51    // Cached array base offset
52    private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
53
54    // Cached array index scale
55    private static final long ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale($type$[].class);
56    */
57
58    // For speed these fields are actually declared in X-Buffer;
59    // these declarations are here as documentation
60    /*
61#if[rw]
62    protected final $type$[] hb;
63    protected final int offset;
64#end[rw]
65    */
66    // Android-removed: Removed MemorySegmentProxy to be supported yet.
67    Heap$Type$Buffer$RW$(int cap, int lim) {            // package-private
68#if[rw]
69        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
70        // super(-1, 0, lim, cap, new $type$[cap], 0);
71        this(cap, lim, false);
72        /*
73        hb = new $type$[cap];
74        offset = 0;
75        */
76        // Android-removed: buffer.address is only used by Direct*Buffer.
77        // this.address = ARRAY_BASE_OFFSET;
78#else[rw]
79        super(cap, lim);
80        this.isReadOnly = true;
81#end[rw]
82    }
83
84#if[rw]
85   // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
86    private Heap$Type$Buffer$RW$(int cap, int lim, boolean isReadOnly) {
87        super(-1, 0, lim, cap, new $type$[cap], 0);
88        this.isReadOnly = isReadOnly;
89    }
90#end[rw]
91
92    // Android-removed: Removed MemorySegmentProxy to be supported yet.
93    Heap$Type$Buffer$RW$($type$[] buf, int off, int len) { // package-private
94#if[rw]
95        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
96        // super(-1, off, off + len, buf.length, buf, 0);
97        this(buf, off, len, false);
98        /*
99        hb = buf;
100        offset = 0;
101        */
102        // Android-removed: buffer.address is only used by Direct*Buffer.
103        // this.address = ARRAY_BASE_OFFSET;
104#else[rw]
105        super(buf, off, len);
106        this.isReadOnly = true;
107#end[rw]
108    }
109
110#if[rw]
111   // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
112    private Heap$Type$Buffer$RW$($type$[] buf, int off, int len, boolean isReadOnly) {
113        super(-1, off, off + len, buf.length, buf, 0);
114        this.isReadOnly = isReadOnly;
115    }
116#end[rw]
117
118    // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
119    // Android-changed: Make the method private.
120    // Android-removed: Removed MemorySegmentProxy to be supported yet.
121    private Heap$Type$Buffer$RW$($type$[] buf,
122                                   int mark, int pos, int lim, int cap,
123                                   int off, boolean isReadOnly)
124    {
125#if[rw]
126        super(mark, pos, lim, cap, buf, off);
127        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
128        this.isReadOnly = isReadOnly;
129        /*
130        hb = buf;
131        offset = off;
132        */
133        // Android-removed: buffer.address is only used by Direct*Buffer.
134        // this.address = ARRAY_BASE_OFFSET + off * ARRAY_INDEX_SCALE;
135#else[rw]
136        super(buf, mark, pos, lim, cap, off);
137        this.isReadOnly = true;
138#end[rw]
139    }
140
141    public $Type$Buffer slice() {
142        int pos = this.position();
143        int lim = this.limit();
144        int rem = (pos <= lim ? lim - pos : 0);
145        return new Heap$Type$Buffer$RW$(hb,
146                -1,
147                0,
148                rem,
149                rem,
150        // Android-removed: Removed MemorySegmentProxy not supported yet.
151                pos + offset,
152        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
153                isReadOnly);
154    }
155
156    @Override
157    public $Type$Buffer slice(int index, int length) {
158        Objects.checkFromIndexSize(index, length, limit());
159        return new Heap$Type$Buffer$RW$(hb,
160                -1,
161                0,
162                length,
163                length,
164        // Android-removed: Removed MemorySegmentProxy not supported yet.
165                index + offset,
166        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
167                isReadOnly);
168    }
169
170    public $Type$Buffer duplicate() {
171        return new Heap$Type$Buffer$RW$(hb,
172                this.markValue(),
173                this.position(),
174                this.limit(),
175                this.capacity(),
176        // Android-removed: Removed MemorySegmentProxy not supported yet.
177                offset,
178        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
179                isReadOnly);
180    }
181
182    public $Type$Buffer asReadOnlyBuffer() {
183#if[rw]
184        // Android-removed: Removed MemorySegmentProxy not supported yet.
185        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
186        /*
187        return new Heap$Type$BufferR(hb,
188                                     this.markValue(),
189                                     this.position(),
190                                     this.limit(),
191                                     this.capacity(),
192                                     offset, segment);
193        */
194        return new Heap$Type$Buffer(hb,
195                this.markValue(),
196                this.position(),
197                this.limit(),
198                this.capacity(),
199                offset,
200                true /* isReadOnly */);
201#else[rw]
202        return duplicate();
203#end[rw]
204    }
205
206#if[rw]
207
208    // Android-changed:  Make it private as no subclasses exist.
209    private int ix(int i) {
210        return i + offset;
211    }
212
213#if[byte]
214    private long byteOffset(long i) {
215        return address + i;
216    }
217#end[byte]
218
219    @Override
220    public $type$ get() {
221        return hb[ix(nextGetIndex())];
222    }
223
224    @Override
225    public $type$ get(int i) {
226        return hb[ix(checkIndex(i))];
227    }
228
229#if[streamableType]
230    @Override
231    $type$ getUnchecked(int i) {
232    return hb[ix(i)];
233    }
234#end[streamableType]
235
236    @Override
237    public $Type$Buffer get($type$[] dst, int offset, int length) {
238        checkScope();
239        Objects.checkFromIndexSize(offset, length, dst.length);
240        int pos = position();
241        if (length > limit() - pos)
242            throw new BufferUnderflowException();
243        System.arraycopy(hb, ix(pos), dst, offset, length);
244        position(pos + length);
245        return this;
246    }
247
248    @Override
249    public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
250        checkScope();
251        Objects.checkFromIndexSize(index, length, limit());
252        Objects.checkFromIndexSize(offset, length, dst.length);
253        System.arraycopy(hb, ix(index), dst, offset, length);
254        return this;
255    }
256
257    public boolean isDirect() {
258        return false;
259    }
260
261#end[rw]
262
263    @Override
264    public boolean isReadOnly() {
265        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
266        return {#if[rw]?isReadOnly:true};
267    }
268
269    @Override
270    public $Type$Buffer put($type$ x) {
271#if[rw]
272        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
273        throwIfReadOnly();
274        hb[ix(nextPutIndex())] = x;
275        return this;
276#else[rw]
277        throw new ReadOnlyBufferException();
278#end[rw]
279    }
280
281    @Override
282    public $Type$Buffer put(int i, $type$ x) {
283#if[rw]
284        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
285        throwIfReadOnly();
286        hb[ix(checkIndex(i))] = x;
287        return this;
288#else[rw]
289        throw new ReadOnlyBufferException();
290#end[rw]
291    }
292
293    @Override
294    public $Type$Buffer put($type$[] src, int offset, int length) {
295#if[rw]
296        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
297        throwIfReadOnly();
298        checkScope();
299        Objects.checkFromIndexSize(offset, length, src.length);
300        int pos = position();
301        if (length > limit() - pos)
302            throw new BufferOverflowException();
303        System.arraycopy(src, offset, hb, ix(pos), length);
304        position(pos + length);
305        return this;
306#else[rw]
307        throw new ReadOnlyBufferException();
308#end[rw]
309    }
310
311    @Override
312    public $Type$Buffer put($Type$Buffer src) {
313#if[rw]
314        checkScope();
315#if[byte]
316        // Android-note: The super class speed-up this operation with Memory.memmove, and arraycopy.
317        super.put(src);
318#else[byte]
319        // Android-changed: Speed-up this operation if the src is a heap or direct buffer.
320        // super.put(src);
321        if (src == this) {
322            throw createSameBufferException();
323        }
324        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
325        throwIfReadOnly();
326        if (src instanceof Heap$Type$Buffer sb) {
327            int n = sb.remaining();
328            if (n > remaining())
329                throw new BufferOverflowException();
330            System.arraycopy(sb.hb, sb.ix(sb.position()),
331                    hb, ix(position()), n);
332            sb.position(sb.position() + n);
333            position(position() + n);
334        } else if (src.isDirect()) {
335            int n = src.remaining();
336            if (n > remaining())
337                throw new BufferOverflowException();
338            src.get(hb, ix(position()), n);
339            position(position() + n);
340        } else {
341            super.put(src);
342        }
343#end[byte]
344        return this;
345#else[rw]
346        throw new ReadOnlyBufferException();
347#end[rw]
348    }
349
350    @Override
351    public $Type$Buffer put(int index, $Type$Buffer src, int offset, int length) {
352#if[rw]
353        checkScope();
354        super.put(index, src, offset, length);
355        return this;
356#else[rw]
357        throw new ReadOnlyBufferException();
358#end[rw]
359    }
360
361    @Override
362    public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
363#if[rw]
364        checkScope();
365        Objects.checkFromIndexSize(index, length, limit());
366        Objects.checkFromIndexSize(offset, length, src.length);
367        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
368        throwIfReadOnly();
369        System.arraycopy(src, offset, hb, ix(index), length);
370        return this;
371#else[rw]
372        throw new ReadOnlyBufferException();
373#end[rw]
374    }
375
376#if[char]
377
378    @Override
379    public $Type$Buffer put(String src, int start, int end) {
380#if[rw]
381        checkScope();
382        int length = end - start;
383        Objects.checkFromIndexSize(start, length, src.length());
384        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
385        throwIfReadOnly();
386        int pos = position();
387        int lim = limit();
388        int rem = (pos <= lim) ? lim - pos : 0;
389        if (length > rem)
390            throw new BufferOverflowException();
391        src.getChars(start, end, hb, ix(pos));
392        position(pos + length);
393        return this;
394#else[rw]
395        throw new ReadOnlyBufferException();
396#end[rw]
397    }
398
399#end[char]
400
401    @Override
402    public $Type$Buffer compact() {
403#if[rw]
404        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
405        throwIfReadOnly();
406        int pos = position();
407        int lim = limit();
408        assert (pos <= lim);
409        int rem = (pos <= lim ? lim - pos : 0);
410        System.arraycopy(hb, ix(pos), hb, ix(0), rem);
411        position(rem);
412        limit(capacity());
413        discardMark();
414        return this;
415#else[rw]
416        throw new ReadOnlyBufferException();
417#end[rw]
418    }
419
420
421
422#if[byte]
423
424    @Override
425    byte _get(int i) {                          // package-private
426        return hb[i];
427    }
428
429    @Override
430    void _put(int i, byte b) {                  // package-private
431#if[rw]
432        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
433        throwIfReadOnly();
434        hb[i] = b;
435#else[rw]
436        throw new ReadOnlyBufferException();
437#end[rw]
438    }
439
440    // char
441
442#if[rw]
443
444    @Override
445    public char getChar() {
446        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
447        // return SCOPED_MEMORY_ACCESS.getCharUnaligned(scope(), hb, byteOffset(nextGetIndex(2)), bigEndian);
448        return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian);
449    }
450
451    @Override
452    public char getChar(int i) {
453        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
454        // return SCOPED_MEMORY_ACCESS.getCharUnaligned(scope(), hb, byteOffset(checkIndex(i, 2)), bigEndian);
455        return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian);
456    }
457
458#end[rw]
459
460    @Override
461    public $Type$Buffer putChar(char x) {
462#if[rw]
463        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
464        throwIfReadOnly();
465        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
466        // SCOPED_MEMORY_ACCESS.putCharUnaligned(scope(), hb, byteOffset(nextPutIndex(2)), x, bigEndian);
467        Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian);
468        return this;
469#else[rw]
470        throw new ReadOnlyBufferException();
471#end[rw]
472    }
473
474    // BEGIN Android-added: {get,put}*Unchecked() accessors.
475    @Override
476    char getCharUnchecked(int i) {
477        return Bits.getChar(this, ix(i), bigEndian);
478    }
479
480    @Override
481    void getUnchecked(int pos, char[] dst, int dstOffset, int length) {
482        Memory.unsafeBulkGet(dst, dstOffset, length * 2, hb, ix(pos), 2, !nativeByteOrder);
483    }
484    // END Android-added: {get,put}*Unchecked() accessors.
485
486    @Override
487    public $Type$Buffer putChar(int i, char x) {
488#if[rw]
489        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
490        throwIfReadOnly();
491        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
492        // SSCOPED_MEMORY_ACCESS.putCharUnaligned(scope(), hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
493        Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian);
494        return this;
495#else[rw]
496        throw new ReadOnlyBufferException();
497#end[rw]
498    }
499
500    // BEGIN Android-added: {get,put}*Unchecked() accessors.
501    @Override
502    void putCharUnchecked(int i, char x) {
503        Bits.putChar(this, ix(i), x, bigEndian);
504    }
505
506    @Override
507    void putUnchecked(int pos, char[] src, int srcOffset, int length) {
508        Memory.unsafeBulkPut(hb, ix(pos), length * 2, src, srcOffset, 2, !nativeByteOrder);
509    }
510    // END Android-added: {get,put}*Unchecked() accessors.
511
512    @Override
513    public CharBuffer asCharBuffer() {
514        int pos = position();
515        int size = (limit() - pos) >> 1;
516        // Android-removed: buffer.address is only used by Direct*Buffer.
517        // long addr = address + pos;
518        // Android-changed: Merge the big and little endian buffer class.
519        /*
520        return (bigEndian
521                ? (CharBuffer)(new ByteBufferAsCharBuffer$RW$B(this,
522                                                               -1,
523                                                               0,
524                                                               size,
525                                                               size,
526                                                               addr, segment))
527                : (CharBuffer)(new ByteBufferAsCharBuffer$RW$L(this,
528                                                               -1,
529                                                               0,
530                                                               size,
531                                                               size,
532                                                               addr, segment)));
533        */
534        return new ByteBufferAsCharBuffer(this,
535                -1,
536                0,
537                size,
538                size,
539                pos,
540                order());
541    }
542
543
544    // short
545
546#if[rw]
547
548    @Override
549    public short getShort() {
550        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
551        // return SCOPED_MEMORY_ACCESS.getShortUnaligned(scope(), hb, byteOffset(nextGetIndex(2)), bigEndian);
552        return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian);
553    }
554
555    @Override
556    public short getShort(int i) {
557        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
558        // return SCOPED_MEMORY_ACCESS.getShortUnaligned(scope(), hb, byteOffset(checkIndex(i, 2)), bigEndian);
559        return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian);
560    }
561
562#end[rw]
563
564    // BEGIN Android-added: {get,put}*Unchecked() accessors.
565    @Override
566    short getShortUnchecked(int i) {
567        return Bits.getShort(this, ix(i), bigEndian);
568    }
569
570    @Override
571    void getUnchecked(int pos, short[] dst, int dstOffset, int length) {
572        Memory.unsafeBulkGet(dst, dstOffset, length * 2, hb, ix(pos), 2, !nativeByteOrder);
573    }
574    // END Android-added: {get,put}*Unchecked() accessors.
575
576    @Override
577    public $Type$Buffer putShort(short x) {
578#if[rw]
579        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
580        throwIfReadOnly();
581        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
582        // SCOPED_MEMORY_ACCESS.putShortUnaligned(scope(), hb, byteOffset(nextPutIndex(2)), x, bigEndian);
583        Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian);
584        return this;
585#else[rw]
586        throw new ReadOnlyBufferException();
587#end[rw]
588    }
589
590    @Override
591    public $Type$Buffer putShort(int i, short x) {
592#if[rw]
593        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
594        // SCOPED_MEMORY_ACCESS.putShortUnaligned(scope(), hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
595        Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian);
596        return this;
597#else[rw]
598        throw new ReadOnlyBufferException();
599#end[rw]
600    }
601
602    // BEGIN Android-added: {get,put}*Unchecked() accessors.
603    @Override
604    void putShortUnchecked(int i, short x) {
605        Bits.putShort(this, ix(i), x, bigEndian);
606    }
607
608    @Override
609    void putUnchecked(int pos, short[] src, int srcOffset, int length) {
610        Memory.unsafeBulkPut(hb, ix(pos), length * 2, src, srcOffset, 2, !nativeByteOrder);
611    }
612    // END Android-added: {get,put}*Unchecked() accessors.
613
614    @Override
615    public ShortBuffer asShortBuffer() {
616        int pos = position();
617        int size = (limit() - pos) >> 1;
618        // Android-removed: buffer.address is only used by Direct*Buffer.
619        // long addr = address + pos;
620        // Android-changed: Merge the big and little endian buffer class.
621        /*
622        return (bigEndian
623                ? (ShortBuffer)(new ByteBufferAsShortBuffer$RW$B(this,
624                                                                 -1,
625                                                                 0,
626                                                                 size,
627                                                                 size,
628                                                                 addr, segment))
629                : (ShortBuffer)(new ByteBufferAsShortBuffer$RW$L(this,
630                                                                 -1,
631                                                                 0,
632                                                                 size,
633                                                                 size,
634                                                                 addr, segment)));
635        */
636        return new ByteBufferAsShortBuffer(this,
637                -1,
638                0,
639                size,
640                size,
641                pos,
642                order());
643    }
644
645
646    // int
647
648#if[rw]
649
650    @Override
651    public int getInt() {
652        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
653        // return SCOPED_MEMORY_ACCESS.getIntUnaligned(scope(), hb, byteOffset(nextGetIndex(4)), bigEndian);
654        return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
655    }
656
657    @Override
658    public int getInt(int i) {
659        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
660        // return SCOPED_MEMORY_ACCESS.getIntUnaligned(scope(), hb, byteOffset(checkIndex(i, 4)), bigEndian);
661        return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian);
662    }
663
664#end[rw]
665
666    // BEGIN Android-added: {get,put}*Unchecked() accessors.
667    @Override
668    int getIntUnchecked(int i) {
669        return Bits.getInt(this, ix(i), bigEndian);
670    }
671
672    @Override
673    void getUnchecked(int pos, int[] dst, int dstOffset, int length) {
674        Memory.unsafeBulkGet(dst, dstOffset, length * 4, hb, ix(pos), 4, !nativeByteOrder);
675    }
676    // END Android-added: {get,put}*Unchecked() accessors.
677
678    @Override
679    public $Type$Buffer putInt(int x) {
680#if[rw]
681        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
682        throwIfReadOnly();
683        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
684        // SCOPED_MEMORY_ACCESS.putIntUnaligned(scope(), hb, byteOffset(nextPutIndex(4)), x, bigEndian);
685        Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian);
686        return this;
687#else[rw]
688        throw new ReadOnlyBufferException();
689#end[rw]
690    }
691
692    @Override
693    public $Type$Buffer putInt(int i, int x) {
694#if[rw]
695        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
696        throwIfReadOnly();
697        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
698        // SCOPED_MEMORY_ACCESS.putIntUnaligned(scope(), hb, byteOffset(checkIndex(i, 4)), x, bigEndian);
699        Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian);
700        return this;
701#else[rw]
702        throw new ReadOnlyBufferException();
703#end[rw]
704    }
705
706    // BEGIN Android-added: {get,put}*Unchecked() accessors.
707    @Override
708    void putIntUnchecked(int i, int x) {
709        Bits.putInt(this, ix(i), x, bigEndian);
710    }
711
712    @Override
713    void putUnchecked(int pos, int[] src, int srcOffset, int length) {
714        Memory.unsafeBulkPut(hb, ix(pos), length * 4, src, srcOffset, 4, !nativeByteOrder);
715    }
716    // END Android-added: {get,put}*Unchecked() accessors.
717
718    @Override
719    public IntBuffer asIntBuffer() {
720        int pos = position();
721        int size = (limit() - pos) >> 2;
722        // Android-removed: buffer.address is only used by Direct*Buffer.
723        // long addr = address + pos;
724        // Android-changed: Merge the big and little endian buffer class.
725        /*
726        return (bigEndian
727                ? (IntBuffer)(new ByteBufferAsIntBuffer$RW$B(this,
728                                                             -1,
729                                                             0,
730                                                             size,
731                                                             size,
732                                                             addr, segment))
733                : (IntBuffer)(new ByteBufferAsIntBuffer$RW$L(this,
734                                                             -1,
735                                                             0,
736                                                             size,
737                                                             addr, segment)));
738        */
739        return new ByteBufferAsIntBuffer(this,
740                -1,
741                0,
742                size,
743                size,
744                pos,
745                order());
746    }
747
748
749    // long
750
751#if[rw]
752
753    @Override
754    public long getLong() {
755        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
756        // return SCOPED_MEMORY_ACCESS.getLongUnaligned(scope(), hb, byteOffset(nextGetIndex(8)), bigEndian);
757        return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian);
758    }
759
760    @Override
761    public long getLong(int i) {
762        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
763        // return SCOPED_MEMORY_ACCESS.getLongUnaligned(scope(), hb, byteOffset(checkIndex(i, 8)), bigEndian);
764        return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian);
765    }
766
767#end[rw]
768
769    // BEGIN Android-added: {get,put}*Unchecked() accessors.
770    @Override
771    long getLongUnchecked(int i) {
772        return Bits.getLong(this, ix(i), bigEndian);
773    }
774
775    @Override
776    void getUnchecked(int pos, long[] dst, int dstOffset, int length) {
777        Memory.unsafeBulkGet(dst, dstOffset, length * 8, hb, ix(pos), 8, !nativeByteOrder);
778    }
779    // END Android-added: {get,put}*Unchecked() accessors.
780
781    @Override
782    public $Type$Buffer putLong(long x) {
783#if[rw]
784        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
785        throwIfReadOnly();
786        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
787        // SCOPED_MEMORY_ACCESS.putLongUnaligned(scope(), hb, byteOffset(nextPutIndex(8)), x, bigEndian);
788        Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian);
789        return this;
790#else[rw]
791        throw new ReadOnlyBufferException();
792#end[rw]
793    }
794
795    @Override
796    public $Type$Buffer putLong(int i, long x) {
797#if[rw]
798        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
799        throwIfReadOnly();
800        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
801        // SCOPED_MEMORY_ACCESS.putLongUnaligned(scope(), hb, byteOffset(checkIndex(i, 8)), x, bigEndian);
802        Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian);
803        return this;
804#else[rw]
805        throw new ReadOnlyBufferException();
806#end[rw]
807    }
808
809    // BEGIN Android-added: {get,put}*Unchecked() accessors.
810    @Override
811    void putLongUnchecked(int i, long x) {
812        Bits.putLong(this, ix(i), x, bigEndian);
813    }
814
815    @Override
816    void putUnchecked(int pos, long[] src, int srcOffset, int length) {
817        Memory.unsafeBulkPut(hb, ix(pos), length * 8, src, srcOffset, 8, !nativeByteOrder);
818    }
819    // END Android-added: {get,put}*Unchecked() accessors.
820
821    @Override
822    public LongBuffer asLongBuffer() {
823        int pos = position();
824        int size = (limit() - pos) >> 3;
825        // Android-removed: buffer.address is only used by Direct*Buffer.
826        // long addr = address + pos;
827        // Android-changed: Merge the big and little endian buffer class.
828        /*
829        return (bigEndian
830                ? (LongBuffer)(new ByteBufferAsLongBuffer$RW$B(this,
831                                                               -1,
832                                                               0,
833                                                               size,
834                                                               size,
835                                                               addr, segment))
836                : (LongBuffer)(new ByteBufferAsLongBuffer$RW$L(this,
837                                                               -1,
838                                                               0,
839                                                               size,
840                                                               size,
841                                                               addr, segment)));
842        */
843        return new ByteBufferAsLongBuffer(this,
844                -1,
845                0,
846                size,
847                size,
848                pos,
849                order());
850    }
851
852
853    // float
854
855#if[rw]
856
857    @Override
858    public float getFloat() {
859        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
860        // int x = SCOPED_MEMORY_ACCESS.getIntUnaligned(scope(), hb, byteOffset(nextGetIndex(4)), bigEndian);
861        // return Float.intBitsToFloat(x);
862        return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian);
863    }
864
865    @Override
866    public float getFloat(int i) {
867        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
868        // int x = SCOPED_MEMORY_ACCESS.getIntUnaligned(scope(), hb, byteOffset(checkIndex(i, 4)), bigEndian);
869        // return Float.intBitsToFloat(x);
870        return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian);
871    }
872
873#end[rw]
874
875    // BEGIN Android-added: {get,put}*Unchecked() accessors.
876    @Override
877    float getFloatUnchecked(int i) {
878        return Bits.getFloat(this, ix(i), bigEndian);
879    }
880
881    @Override
882    void getUnchecked(int pos, float[] dst, int dstOffset, int length) {
883        Memory.unsafeBulkGet(dst, dstOffset, length * 4, hb, ix(pos), 4, !nativeByteOrder);
884    }
885    // END Android-added: {get,put}*Unchecked() accessors.
886
887    @Override
888    public $Type$Buffer putFloat(float x) {
889#if[rw]
890        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
891        throwIfReadOnly();
892        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
893        // int y = Float.floatToRawIntBits(x);
894        // SCOPED_MEMORY_ACCESS.putIntUnaligned(scope(), hb, byteOffset(nextPutIndex(4)), y, bigEndian);
895        Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian);
896        return this;
897#else[rw]
898        throw new ReadOnlyBufferException();
899#end[rw]
900    }
901
902    @Override
903    public $Type$Buffer putFloat(int i, float x) {
904#if[rw]
905        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
906        throwIfReadOnly();
907        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
908        // int y = Float.floatToRawIntBits(x);
909        // SCOPED_MEMORY_ACCESS.putIntUnaligned(scope(), hb, byteOffset(checkIndex(i, 4)), y, bigEndian);
910        Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian);
911        return this;
912#else[rw]
913        throw new ReadOnlyBufferException();
914#end[rw]
915    }
916
917    // BEGIN Android-added: {get,put}*Unchecked() accessors.
918    @Override
919    void putFloatUnchecked(int i, float x) {
920        Bits.putFloat(this, ix(i), x, bigEndian);
921    }
922
923    @Override
924    void putUnchecked(int pos, float[] src, int srcOffset, int length) {
925        Memory.unsafeBulkPut(hb, ix(pos), length * 4, src, srcOffset, 4, !nativeByteOrder);
926    }
927    // END Android-added: {get,put}*Unchecked() accessors.
928
929    @Override
930    public FloatBuffer asFloatBuffer() {
931        int pos = position();
932        int size = (limit() - pos) >> 2;
933        // Android-removed: buffer.address is only used by Direct*Buffer.
934        // long addr = address + pos;
935        // Android-changed: Merge the big and little endian buffer class.
936        /*
937        return (bigEndian
938                ? (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$B(this,
939                                                                 -1,
940                                                                 0,
941                                                                 size,
942                                                                 size,
943                                                                 addr, segment))
944                : (FloatBuffer)(new ByteBufferAsFloatBuffer$RW$L(this,
945                                                                 -1,
946                                                                 0,
947                                                                 size,
948                                                                 size,
949                                                                 addr, segment)));
950        */
951        return new ByteBufferAsFloatBuffer(this,
952                -1,
953                0,
954                size,
955                size,
956                pos,
957                order());
958    }
959
960
961    // double
962
963#if[rw]
964
965    @Override
966    public double getDouble() {
967        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
968        // long x = SCOPED_MEMORY_ACCESS.getLongUnaligned(scope(), hb, byteOffset(nextGetIndex(8)), bigEndian);
969        // return Double.longBitsToDouble(x);
970        return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian);
971    }
972
973    @Override
974    public double getDouble(int i) {
975        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
976        // long x = SCOPED_MEMORY_ACCESS.getLongUnaligned(scope(), hb, byteOffset(checkIndex(i, 8)), bigEndian);
977        // return Double.longBitsToDouble(x);
978        return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian);
979    }
980
981#end[rw]
982
983    // BEGIN Android-added: {get,put}*Unchecked() accessors.
984    @Override
985    double getDoubleUnchecked(int i) {
986        return Bits.getDouble(this, ix(i), bigEndian);
987    }
988
989    @Override
990    void getUnchecked(int pos, double[] dst, int dstOffset, int length) {
991        Memory.unsafeBulkGet(dst, dstOffset, length * 8, hb, ix(pos), 8, !nativeByteOrder);
992    }
993    // END Android-added: {get,put}*Unchecked() accessors.
994
995    @Override
996    public $Type$Buffer putDouble(double x) {
997#if[rw]
998        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
999        throwIfReadOnly();
1000        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
1001        // long y = Double.doubleToRawLongBits(x);
1002        // SCOPED_MEMORY_ACCESS.putLongUnaligned(scope(), hb, byteOffset(nextPutIndex(8)), y, bigEndian);
1003        Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian);
1004        return this;
1005#else[rw]
1006        throw new ReadOnlyBufferException();
1007#end[rw]
1008    }
1009
1010    @Override
1011    public $Type$Buffer putDouble(int i, double x) {
1012#if[rw]
1013        // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
1014        throwIfReadOnly();
1015        // Android-changed: Use Bits instead of ScopedMemoryAccess to be supported yet.
1016        // long y = Double.doubleToRawLongBits(x);
1017        // SCOPED_MEMORY_ACCESS.putLongUnaligned(scope(), hb, byteOffset(checkIndex(i, 8)), y, bigEndian);
1018        Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian);
1019        return this;
1020#else[rw]
1021        throw new ReadOnlyBufferException();
1022#end[rw]
1023    }
1024
1025    // BEGIN Android-added: {get,put}*Unchecked() accessors.
1026    @Override
1027    void putDoubleUnchecked(int i, double x) {
1028        Bits.putDouble(this, ix(i), x, bigEndian);
1029    }
1030
1031    @Override
1032    void putUnchecked(int pos, double[] src, int srcOffset, int length) {
1033        Memory.unsafeBulkPut(hb, ix(pos), length * 8, src, srcOffset, 8, !nativeByteOrder);
1034    }
1035    // END Android-added: {get,put}*Unchecked() accessors.
1036
1037    @Override
1038    public DoubleBuffer asDoubleBuffer() {
1039        int pos = position();
1040        int size = (limit() - pos) >> 3;
1041        // Android-removed: buffer.address is only used by Direct*Buffer.
1042        // long addr = address + pos;
1043        // Android-changed: Merge the big and little endian buffer class.
1044        /*
1045        return (bigEndian
1046                ? (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$B(this,
1047                                                                   -1,
1048                                                                   0,
1049                                                                   size,
1050                                                                   size,
1051                                                                   addr, segment))
1052                : (DoubleBuffer)(new ByteBufferAsDoubleBuffer$RW$L(this,
1053                                                                   -1,
1054                                                                   0,
1055                                                                   size,
1056                                                                   size,
1057                                                                   addr, segment)));
1058        */
1059        return new ByteBufferAsDoubleBuffer(this,
1060                -1,
1061                0,
1062                size,
1063                size,
1064                pos,
1065                order());
1066    }
1067
1068
1069#end[byte]
1070
1071
1072#if[char]
1073
1074    String toString(int start, int end) {               // package-private
1075        try {
1076            return new String(hb, start + offset, end - start);
1077        } catch (StringIndexOutOfBoundsException x) {
1078            throw new IndexOutOfBoundsException();
1079        }
1080    }
1081
1082
1083    // --- Methods to support CharSequence ---
1084
1085    public CharBuffer subSequence(int start, int end) {
1086        int pos = position();
1087        Objects.checkFromToIndex(start, end, limit() - pos);
1088        return new HeapCharBuffer$RW$(hb,
1089                -1,
1090                pos + start,
1091                pos + end,
1092                capacity(),
1093        // Android-removed: Removed MemorySegmentProxy not supported yet.
1094                offset,
1095        // Android-changed: Merge the Read-only buffer class with this Read-Write buffer class.
1096                isReadOnly);
1097    }
1098
1099#end[char]
1100
1101
1102#if[!byte]
1103
1104    public ByteOrder order() {
1105        return ByteOrder.nativeOrder();
1106    }
1107#end[!byte]
1108#if[char]
1109
1110    ByteOrder charRegionOrder() {
1111        return order();
1112    }
1113#end[char]
1114
1115    // Android-added: Merge the Read-only buffer class with this Read-Write buffer class.
1116    private void throwIfReadOnly() {
1117        if (isReadOnly) {
1118            throw new ReadOnlyBufferException();
1119        }
1120    }
1121}
1122