blob: 079f7f8ab7b2800fa918203c3fbd3e587c471ded [file] [log] [blame]
The Android Open Source Project0eec4642012-04-01 00:00:00 -07001/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java.nio;
18
19import org.apache.harmony.luni.platform.PlatformAddress;
20import org.apache.harmony.nio.internal.DirectBuffer;
21
22/**
23 * This class wraps a byte buffer to be a float buffer.
24 * <p>
25 * Implementation notice:
26 * <ul>
27 * <li>After a byte buffer instance is wrapped, it becomes privately owned by
28 * the adapter. It must NOT be accessed outside the adapter any more.</li>
29 * <li>The byte buffer's position and limit are NOT linked with the adapter.
30 * The adapter extends Buffer, thus has its own position and limit.</li>
31 * </ul>
32 * </p>
33 */
34final class FloatToByteBufferAdapter extends FloatBuffer implements
35 DirectBuffer {
36
37 static FloatBuffer wrap(ByteBuffer byteBuffer) {
38 return new FloatToByteBufferAdapter(byteBuffer.slice());
39 }
40
41 private final ByteBuffer byteBuffer;
42
43 FloatToByteBufferAdapter(ByteBuffer byteBuffer) {
44 super((byteBuffer.capacity() >> 2));
45 this.byteBuffer = byteBuffer;
46 this.byteBuffer.clear();
47 }
48
49 public int getByteCapacity() {
50 if (byteBuffer instanceof DirectBuffer) {
51 return ((DirectBuffer) byteBuffer).getByteCapacity();
52 }
53 assert false : byteBuffer;
54 return -1;
55 }
56
57 public PlatformAddress getEffectiveAddress() {
58 if (byteBuffer instanceof DirectBuffer) {
59 // BEGIN android-changed
60 PlatformAddress addr = ((DirectBuffer)byteBuffer).getEffectiveAddress();
61 effectiveDirectAddress = addr.toInt();
62 return addr;
63 // END android-changed
64 }
65 assert false : byteBuffer;
66 return null;
67 }
68
69 public PlatformAddress getBaseAddress() {
70 if (byteBuffer instanceof DirectBuffer) {
71 return ((DirectBuffer) byteBuffer).getBaseAddress();
72 }
73 assert false : byteBuffer;
74 return null;
75 }
76
77 public boolean isAddressValid() {
78 if (byteBuffer instanceof DirectBuffer) {
79 return ((DirectBuffer) byteBuffer).isAddressValid();
80 }
81 assert false : byteBuffer;
82 return false;
83 }
84
85 public void addressValidityCheck() {
86 if (byteBuffer instanceof DirectBuffer) {
87 ((DirectBuffer) byteBuffer).addressValidityCheck();
88 } else {
89 assert false : byteBuffer;
90 }
91 }
92
93 public void free() {
94 if (byteBuffer instanceof DirectBuffer) {
95 ((DirectBuffer) byteBuffer).free();
96 } else {
97 assert false : byteBuffer;
98 }
99 }
100
101 @Override
102 public FloatBuffer asReadOnlyBuffer() {
103 FloatToByteBufferAdapter buf = new FloatToByteBufferAdapter(byteBuffer
104 .asReadOnlyBuffer());
105 buf.limit = limit;
106 buf.position = position;
107 buf.mark = mark;
108 return buf;
109 }
110
111 @Override
112 public FloatBuffer compact() {
113 if (byteBuffer.isReadOnly()) {
114 throw new ReadOnlyBufferException();
115 }
116 byteBuffer.limit(limit << 2);
117 byteBuffer.position(position << 2);
118 byteBuffer.compact();
119 byteBuffer.clear();
120 position = limit - position;
121 limit = capacity;
122 mark = UNSET_MARK;
123 return this;
124 }
125
126 @Override
127 public FloatBuffer duplicate() {
128 FloatToByteBufferAdapter buf = new FloatToByteBufferAdapter(byteBuffer
129 .duplicate());
130 buf.limit = limit;
131 buf.position = position;
132 buf.mark = mark;
133 return buf;
134 }
135
136 @Override
137 public float get() {
138 if (position == limit) {
139 throw new BufferUnderflowException();
140 }
141 return byteBuffer.getFloat(position++ << 2);
142 }
143
144 @Override
145 public float get(int index) {
146 if (index < 0 || index >= limit) {
147 throw new IndexOutOfBoundsException();
148 }
149 return byteBuffer.getFloat(index << 2);
150 }
151
152 @Override
153 public boolean isDirect() {
154 return byteBuffer.isDirect();
155 }
156
157 @Override
158 public boolean isReadOnly() {
159 return byteBuffer.isReadOnly();
160 }
161
162 @Override
163 public ByteOrder order() {
164 return byteBuffer.order();
165 }
166
167 @Override
168 protected float[] protectedArray() {
169 throw new UnsupportedOperationException();
170 }
171
172 @Override
173 protected int protectedArrayOffset() {
174 throw new UnsupportedOperationException();
175 }
176
177 @Override
178 protected boolean protectedHasArray() {
179 return false;
180 }
181
182 @Override
183 public FloatBuffer put(float c) {
184 if (position == limit) {
185 throw new BufferOverflowException();
186 }
187 byteBuffer.putFloat(position++ << 2, c);
188 return this;
189 }
190
191 @Override
192 public FloatBuffer put(int index, float c) {
193 if (index < 0 || index >= limit) {
194 throw new IndexOutOfBoundsException();
195 }
196 byteBuffer.putFloat(index << 2, c);
197 return this;
198 }
199
200 // BEGIN android-added
201 @Override
202 public FloatBuffer put(float[] c, int off, int len) {
203 if (byteBuffer instanceof ReadWriteDirectByteBuffer) {
204 byteBuffer.limit(limit << 2);
205 byteBuffer.position(position << 2);
206 ((ReadWriteDirectByteBuffer) byteBuffer).put(c, off, len);
207 this.position += len;
208 return this;
209 } else {
210 return super.put(c, off, len);
211 }
212 }
213 // END android-added
214
215
216 @Override
217 public FloatBuffer slice() {
218 byteBuffer.limit(limit << 2);
219 byteBuffer.position(position << 2);
220 FloatBuffer result = new FloatToByteBufferAdapter(byteBuffer.slice());
221 byteBuffer.clear();
222 return result;
223 }
224
225}