• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 package java.nio;
28 
29 /**
30  * A read/write HeapFloatBuffer.
31  */
32 
33 class 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 
HeapFloatBuffer(int cap, int lim)44     HeapFloatBuffer(int cap, int lim) {            // package-private
45         this(cap, lim, false);
46     }
47 
HeapFloatBuffer(int cap, int lim, boolean isReadOnly)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 
HeapFloatBuffer(float[] buf, int off, int len)53     HeapFloatBuffer(float[] buf, int off, int len) { // package-private
54         this(buf, off, len, false);
55     }
56 
HeapFloatBuffer(float[] buf, int off, int len, boolean isReadOnly)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 
HeapFloatBuffer(float[] buf, int mark, int pos, int lim, int cap, int off)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 
HeapFloatBuffer(float[] buf, int mark, int pos, int lim, int cap, int off, boolean isReadOnly)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 
slice()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 
duplicate()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 
asReadOnlyBuffer()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 
ix(int i)104     protected int ix(int i) {
105         return i + offset;
106     }
107 
get()108     public float get() {
109         return hb[ix(nextGetIndex())];
110     }
111 
get(int i)112     public float get(int i) {
113         return hb[ix(checkIndex(i))];
114     }
115 
get(float[] dst, int offset, int length)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 
isDirect()125     public boolean isDirect() {
126         return false;
127     }
128 
isReadOnly()129     public boolean isReadOnly() {
130         return isReadOnly;
131     }
132 
put(float x)133     public FloatBuffer put(float x) {
134         if (isReadOnly) {
135             throw new ReadOnlyBufferException();
136         }
137         hb[ix(nextPutIndex())] = x;
138         return this;
139     }
140 
put(int i, float x)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 
put(float[] src, int offset, int length)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 
put(FloatBuffer src)161     public FloatBuffer put(FloatBuffer src) {
162         if (src == this) {
163             throw new IllegalArgumentException();
164         }
165         if (isReadOnly) {
166             throw new ReadOnlyBufferException();
167         }
168         if (src instanceof HeapFloatBuffer) {
169             HeapFloatBuffer sb = (HeapFloatBuffer) 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 
compact()189     public FloatBuffer 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 
order()200     public ByteOrder order() {
201         return ByteOrder.nativeOrder();
202     }
203 }
204