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