2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 // updated to a newer version of harmony
20 // added some missing updates on position and limit
25 import org.apache.harmony.luni.platform.PlatformAddress;
26 import org.apache.harmony.nio.internal.DirectBuffer;
29 final class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
31 private static final int CHAR_SIZE = 2;
33 private static final int SHORT_SIZE = 2;
35 private static final int INTEGER_SIZE = 4;
37 private static final int LONG_SIZE = 8;
39 private static final int FLOAT_SIZE = 4;
41 private static final int DOUBLE_SIZE = 8;
43 public MappedByteBufferAdapter(ByteBuffer buffer) {
47 public MappedByteBufferAdapter(PlatformAddress addr, int capa, int offset,
49 super(addr, capa, offset, mode);
52 public CharBuffer asCharBuffer() {
53 return this.wrapped.asCharBuffer();
56 public DoubleBuffer asDoubleBuffer() {
57 return this.wrapped.asDoubleBuffer();
60 public FloatBuffer asFloatBuffer() {
61 return this.wrapped.asFloatBuffer();
64 public IntBuffer asIntBuffer() {
65 return this.wrapped.asIntBuffer();
68 public LongBuffer asLongBuffer() {
69 return this.wrapped.asLongBuffer();
72 public ByteBuffer asReadOnlyBuffer() {
73 MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
75 buf.limit = this.limit;
76 buf.position = this.position;
81 public ShortBuffer asShortBuffer() {
82 return this.wrapped.asShortBuffer();
85 public ByteBuffer compact() {
86 if (this.wrapped.isReadOnly()) {
87 throw new ReadOnlyBufferException();
89 this.wrapped.limit(this.limit);
90 this.wrapped.position(this.position);
91 this.wrapped.compact();
93 this.position = this.limit - this.position;
94 this.limit = this.capacity;
95 this.mark = UNSET_MARK;
99 public ByteBuffer duplicate() {
100 MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
102 buf.limit = this.limit;
103 buf.position = this.position;
104 buf.mark = this.mark;
109 this.wrapped.limit(this.limit);
110 this.wrapped.position(this.position);
111 byte result = this.wrapped.get();
116 public byte get(int index) {
117 this.wrapped.limit(this.limit);
118 this.wrapped.position(this.position);
119 return this.wrapped.get(index);
122 public char getChar() {
123 this.wrapped.limit(this.limit);
124 this.wrapped.position(this.position);
125 char result = this.wrapped.getChar();
126 this.position += CHAR_SIZE;
130 public char getChar(int index) {
131 this.wrapped.limit(this.limit);
132 this.wrapped.position(this.position);
133 return this.wrapped.getChar(index);
136 public double getDouble() {
137 this.wrapped.limit(this.limit);
138 this.wrapped.position(this.position);
139 double result = this.wrapped.getDouble();
140 this.position += DOUBLE_SIZE;
144 public double getDouble(int index) {
145 this.wrapped.limit(this.limit);
146 this.wrapped.position(this.position);
147 return this.wrapped.getDouble(index);
150 public PlatformAddress getEffectiveAddress() {
151 return ((DirectBuffer) this.wrapped).getEffectiveAddress();
154 public float getFloat() {
155 this.wrapped.limit(this.limit);
156 this.wrapped.position(this.position);
157 float result = this.wrapped.getFloat();
158 this.position += FLOAT_SIZE;
162 public float getFloat(int index) {
163 this.wrapped.limit(this.limit);
164 this.wrapped.position(this.position);
165 return this.wrapped.getFloat(index);
168 public int getInt() {
169 this.wrapped.limit(this.limit);
170 this.wrapped.position(this.position);
171 int result = this.wrapped.getInt();
172 this.position += INTEGER_SIZE;
176 public int getInt(int index) {
177 this.wrapped.limit(this.limit);
178 this.wrapped.position(this.position);
179 return this.wrapped.getInt(index);
182 public long getLong() {
183 this.wrapped.limit(this.limit);
184 this.wrapped.position(this.position);
185 long result = this.wrapped.getLong();
186 this.position += LONG_SIZE;
190 public long getLong(int index) {
191 this.wrapped.limit(this.limit);
192 this.wrapped.position(this.position);
193 return this.wrapped.getLong(index);
196 public short getShort() {
197 this.wrapped.limit(this.limit);
198 this.wrapped.position(this.position);
199 short result = this.wrapped.getShort();
200 this.position += SHORT_SIZE;
204 public short getShort(int index) {
205 this.wrapped.limit(this.limit);
206 this.wrapped.position(this.position);
207 return this.wrapped.getShort(index);
210 public boolean isDirect() {
214 public boolean isReadOnly() {
215 return this.wrapped.isReadOnly();
218 ByteBuffer orderImpl(ByteOrder byteOrder) {
219 super.orderImpl(byteOrder);
220 return this.wrapped.order(byteOrder);
223 public ByteBuffer put(byte b) {
224 this.wrapped.limit(this.limit);
225 this.wrapped.position(this.position);
231 public ByteBuffer put(byte[] src, int off, int len) {
232 this.wrapped.limit(this.limit);
233 this.wrapped.position(this.position);
234 this.wrapped.put(src, off, len);
235 this.position += len;
239 public ByteBuffer put(int index, byte b) {
240 this.wrapped.limit(this.limit);
241 this.wrapped.position(this.position);
242 this.wrapped.put(index, b);
246 public ByteBuffer putChar(char value) {
247 this.wrapped.limit(this.limit);
248 this.wrapped.position(this.position);
249 this.wrapped.putChar(value);
250 this.position += CHAR_SIZE;
254 public ByteBuffer putChar(int index, char value) {
255 this.wrapped.limit(this.limit);
256 this.wrapped.position(this.position);
257 this.wrapped.putChar(index, value);
261 public ByteBuffer putDouble(double value) {
262 this.wrapped.limit(this.limit);
263 this.wrapped.position(this.position);
264 this.wrapped.putDouble(value);
265 this.position += DOUBLE_SIZE;
269 public ByteBuffer putDouble(int index, double value) {
270 this.wrapped.limit(this.limit);
271 this.wrapped.position(this.position);
272 this.wrapped.putDouble(index, value);
276 public ByteBuffer putFloat(float value) {
277 this.wrapped.limit(this.limit);
278 this.wrapped.position(this.position);
279 this.wrapped.putFloat(value);
280 this.position += FLOAT_SIZE;
284 public ByteBuffer putFloat(int index, float value) {
285 this.wrapped.limit(this.limit);
286 this.wrapped.position(this.position);
287 this.wrapped.putFloat(index, value);
291 public ByteBuffer putInt(int index, int value) {
292 this.wrapped.limit(this.limit);
293 this.wrapped.position(this.position);
294 this.wrapped.putInt(index, value);
298 public ByteBuffer putInt(int value) {
299 this.wrapped.limit(this.limit);
300 this.wrapped.position(this.position);
301 this.wrapped.putInt(value);
302 this.position += INTEGER_SIZE;
306 public ByteBuffer putLong(int index, long value) {
307 this.wrapped.limit(this.limit);
308 this.wrapped.position(this.position);
309 this.wrapped.putLong(index, value);
313 public ByteBuffer putLong(long value) {
314 this.wrapped.limit(this.limit);
315 this.wrapped.position(this.position);
316 this.wrapped.putLong(value);
317 this.position += LONG_SIZE;
321 public ByteBuffer putShort(int index, short value) {
322 this.wrapped.limit(this.limit);
323 this.wrapped.position(this.position);
324 this.wrapped.putShort(index, value);
328 public ByteBuffer putShort(short value) {
329 this.wrapped.limit(this.limit);
330 this.wrapped.position(this.position);
331 this.wrapped.putShort(value);
332 this.position += SHORT_SIZE;
336 public ByteBuffer slice() {
337 this.wrapped.limit(this.limit);
338 this.wrapped.position(this.position);
339 MappedByteBufferAdapter result = new MappedByteBufferAdapter(
340 this.wrapped.slice());
341 this.wrapped.clear();
345 byte[] protectedArray() {
346 return this.wrapped.protectedArray();
349 int protectedArrayOffset() {
350 return this.wrapped.protectedArrayOffset();
353 boolean protectedHasArray() {
354 return this.wrapped.protectedHasArray();
357 public PlatformAddress getBaseAddress() {
358 return this.wrapped.getBaseAddress();
361 public boolean isAddressValid() {
362 return this.wrapped.isAddressValid();
365 public void addressValidityCheck() {
366 this.wrapped.addressValidityCheck();
373 public int getByteCapacity() {
374 return wrapped.getByteCapacity();