1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 * Copyright (c) 2000, 2008, 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
27package java.nio;
28
29/**
30 * A read/write HeapFloatBuffer.
31 */
32
33class HeapFloatBuffer extends FloatBuffer {
34
35    // For speed these fields are actually declared in X-Buffer;
36    // these declarations are here as documentation
37    /*
38
39      protected final float[] hb;
40      protected final int offset;
41
42    */
43
44    HeapFloatBuffer(int cap, int lim) {            // package-private
45        this(cap, lim, false);
46    }
47
48    HeapFloatBuffer(int cap, int lim, boolean isReadOnly) {            // package-private
49        super(-1, 0, lim, cap, new float[cap], 0);
50        this.isReadOnly = isReadOnly;
51    }
52
53    HeapFloatBuffer(float[] buf, int off, int len) { // package-private
54        this(buf, off, len, false);
55    }
56
57    HeapFloatBuffer(float[] buf, int off, int len, boolean isReadOnly) { // package-private
58        super(-1, off, off + len, buf.length, buf, 0);
59        this.isReadOnly = isReadOnly;
60    }
61
62    protected HeapFloatBuffer(float[] buf,
63                              int mark, int pos, int lim, int cap,
64                              int off) {
65        this(buf, mark, pos, lim, cap, off, false);
66    }
67
68    protected HeapFloatBuffer(float[] buf,
69                              int mark, int pos, int lim, int cap,
70                              int off, boolean isReadOnly) {
71        super(mark, pos, lim, cap, buf, off);
72        this.isReadOnly = isReadOnly;
73    }
74
75    public FloatBuffer slice() {
76        return new HeapFloatBuffer(hb,
77                -1,
78                0,
79                this.remaining(),
80                this.remaining(),
81                this.position() + offset,
82                isReadOnly);
83    }
84
85    public FloatBuffer duplicate() {
86        return new HeapFloatBuffer(hb,
87                this.markValue(),
88                this.position(),
89                this.limit(),
90                this.capacity(),
91                offset,
92                isReadOnly);
93    }
94
95    public FloatBuffer asReadOnlyBuffer() {
96        return new HeapFloatBuffer(hb,
97                this.markValue(),
98                this.position(),
99                this.limit(),
100                this.capacity(),
101                offset, true);
102    }
103
104    protected int ix(int i) {
105        return i + offset;
106    }
107
108    public float get() {
109        return hb[ix(nextGetIndex())];
110    }
111
112    public float get(int i) {
113        return hb[ix(checkIndex(i))];
114    }
115
116    public FloatBuffer get(float[] dst, int offset, int length) {
117        checkBounds(offset, length, dst.length);
118        if (length > remaining())
119            throw new BufferUnderflowException();
120        System.arraycopy(hb, ix(position()), dst, offset, length);
121        position(position() + length);
122        return this;
123    }
124
125    public boolean isDirect() {
126        return false;
127    }
128
129    public boolean isReadOnly() {
130        return isReadOnly;
131    }
132
133    public FloatBuffer put(float x) {
134        if (isReadOnly) {
135            throw new ReadOnlyBufferException();
136        }
137        hb[ix(nextPutIndex())] = x;
138        return this;
139    }
140
141    public FloatBuffer put(int i, float x) {
142        if (isReadOnly) {
143            throw new ReadOnlyBufferException();
144        }
145        hb[ix(checkIndex(i))] = x;
146        return this;
147    }
148
149    public FloatBuffer put(float[] src, int offset, int length) {
150        if (isReadOnly) {
151            throw new ReadOnlyBufferException();
152        }
153        checkBounds(offset, length, src.length);
154        if (length > remaining())
155            throw new BufferOverflowException();
156        System.arraycopy(src, offset, hb, ix(position()), length);
157        position(position() + length);
158        return this;
159    }
160
161    public FloatBuffer put(FloatBuffer src) {
162        if (isReadOnly) {
163            throw new ReadOnlyBufferException();
164        }
165        if (src instanceof HeapFloatBuffer) {
166            if (src == this)
167                throw new IllegalArgumentException();
168            HeapFloatBuffer sb = (HeapFloatBuffer) src;
169            int n = sb.remaining();
170            if (n > remaining())
171                throw new BufferOverflowException();
172            System.arraycopy(sb.hb, sb.ix(sb.position()),
173                    hb, ix(position()), n);
174            sb.position(sb.position() + n);
175            position(position() + n);
176        } else if (src.isDirect()) {
177            int n = src.remaining();
178            if (n > remaining())
179                throw new BufferOverflowException();
180            src.get(hb, ix(position()), n);
181            position(position() + n);
182        } else {
183            super.put(src);
184        }
185        return this;
186    }
187
188    public FloatBuffer compact() {
189        if (isReadOnly) {
190            throw new ReadOnlyBufferException();
191        }
192        System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
193        position(remaining());
194        limit(capacity());
195        discardMark();
196        return this;
197    }
198
199    public ByteOrder order() {
200        return ByteOrder.nativeOrder();
201    }
202}
203