001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package java.nio;
018:
019: import org.apache.harmony.nio.internal.DirectBuffer;
020: import org.apache.harmony.luni.platform.PlatformAddress;
021:
022: /**
023: * This class wraps a byte buffer to be a float buffer.
024: * <p>
025: * Implementation notice:
026: * <ul>
027: * <li>After a byte buffer instance is wrapped, it becomes privately owned by
028: * the adapter. It must NOT be accessed outside the adapter any more.</li>
029: * <li>The byte buffer's position and limit are NOT linked with the adapter.
030: * The adapter extends Buffer, thus has its own position and limit.</li>
031: * </ul>
032: * </p>
033: *
034: */
035: final class FloatToByteBufferAdapter extends FloatBuffer implements
036: DirectBuffer {
037:
038: static FloatBuffer wrap(ByteBuffer byteBuffer) {
039: return new FloatToByteBufferAdapter(byteBuffer.slice());
040: }
041:
042: private final ByteBuffer byteBuffer;
043:
044: FloatToByteBufferAdapter(ByteBuffer byteBuffer) {
045: super ((byteBuffer.capacity() >> 2));
046: this .byteBuffer = byteBuffer;
047: this .byteBuffer.clear();
048: }
049:
050: public int getByteCapacity() {
051: if (byteBuffer instanceof DirectBuffer) {
052: return ((DirectBuffer) byteBuffer).getByteCapacity();
053: }
054: assert false : byteBuffer;
055: return -1;
056: }
057:
058: public PlatformAddress getEffectiveAddress() {
059: if (byteBuffer instanceof DirectBuffer) {
060: return ((DirectBuffer) byteBuffer).getEffectiveAddress();
061: }
062: assert false : byteBuffer;
063: return null;
064: }
065:
066: public PlatformAddress getBaseAddress() {
067: if (byteBuffer instanceof DirectBuffer) {
068: return ((DirectBuffer) byteBuffer).getBaseAddress();
069: }
070: assert false : byteBuffer;
071: return null;
072: }
073:
074: public boolean isAddressValid() {
075: if (byteBuffer instanceof DirectBuffer) {
076: return ((DirectBuffer) byteBuffer).isAddressValid();
077: }
078: assert false : byteBuffer;
079: return false;
080: }
081:
082: public void addressValidityCheck() {
083: if (byteBuffer instanceof DirectBuffer) {
084: ((DirectBuffer) byteBuffer).addressValidityCheck();
085: } else {
086: assert false : byteBuffer;
087: }
088: }
089:
090: public void free() {
091: if (byteBuffer instanceof DirectBuffer) {
092: ((DirectBuffer) byteBuffer).free();
093: } else {
094: assert false : byteBuffer;
095: }
096: }
097:
098: @Override
099: public FloatBuffer asReadOnlyBuffer() {
100: FloatToByteBufferAdapter buf = new FloatToByteBufferAdapter(
101: byteBuffer.asReadOnlyBuffer());
102: buf.limit = limit;
103: buf.position = position;
104: buf.mark = mark;
105: return buf;
106: }
107:
108: @Override
109: public FloatBuffer compact() {
110: if (byteBuffer.isReadOnly()) {
111: throw new ReadOnlyBufferException();
112: }
113: byteBuffer.limit(limit << 2);
114: byteBuffer.position(position << 2);
115: byteBuffer.compact();
116: byteBuffer.clear();
117: position = limit - position;
118: limit = capacity;
119: mark = UNSET_MARK;
120: return this ;
121: }
122:
123: @Override
124: public FloatBuffer duplicate() {
125: FloatToByteBufferAdapter buf = new FloatToByteBufferAdapter(
126: byteBuffer.duplicate());
127: buf.limit = limit;
128: buf.position = position;
129: buf.mark = mark;
130: return buf;
131: }
132:
133: @Override
134: public float get() {
135: if (position == limit) {
136: throw new BufferUnderflowException();
137: }
138: return byteBuffer.getFloat(position++ << 2);
139: }
140:
141: @Override
142: public float get(int index) {
143: if (index < 0 || index >= limit) {
144: throw new IndexOutOfBoundsException();
145: }
146: return byteBuffer.getFloat(index << 2);
147: }
148:
149: @Override
150: public boolean isDirect() {
151: return byteBuffer.isDirect();
152: }
153:
154: @Override
155: public boolean isReadOnly() {
156: return byteBuffer.isReadOnly();
157: }
158:
159: @Override
160: public ByteOrder order() {
161: return byteBuffer.order();
162: }
163:
164: @Override
165: protected float[] protectedArray() {
166: throw new UnsupportedOperationException();
167: }
168:
169: @Override
170: protected int protectedArrayOffset() {
171: throw new UnsupportedOperationException();
172: }
173:
174: @Override
175: protected boolean protectedHasArray() {
176: return false;
177: }
178:
179: @Override
180: public FloatBuffer put(float c) {
181: if (position == limit) {
182: throw new BufferOverflowException();
183: }
184: byteBuffer.putFloat(position++ << 2, c);
185: return this ;
186: }
187:
188: @Override
189: public FloatBuffer put(int index, float c) {
190: if (index < 0 || index >= limit) {
191: throw new IndexOutOfBoundsException();
192: }
193: byteBuffer.putFloat(index << 2, c);
194: return this ;
195: }
196:
197: @Override
198: public FloatBuffer slice() {
199: byteBuffer.limit(limit << 2);
200: byteBuffer.position(position << 2);
201: FloatBuffer result = new FloatToByteBufferAdapter(byteBuffer
202: .slice());
203: byteBuffer.clear();
204: return result;
205: }
206:
207: }
|