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 // added some missing updates on position and limit
24 import java.nio.channels.FileChannel.MapMode;
25 import org.apache.harmony.luni.platform.PlatformAddress;
26 import org.apache.harmony.nio.internal.DirectBuffer;
31 public final class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
33 private static final int CHAR_SIZE = 2;
35 private static final int SHORT_SIZE = 2;
37 private static final int INTEGER_SIZE = 4;
39 private static final int LONG_SIZE = 8;
41 private static final int FLOAT_SIZE = 4;
43 private static final int DOUBLE_SIZE = 8;
45 public MappedByteBufferAdapter(ByteBuffer buffer) {
49 public MappedByteBufferAdapter(PlatformAddress addr, int capa, int offset, MapMode mode) {
50 super(addr, capa, offset, mode);
54 public CharBuffer asCharBuffer() {
55 return this.wrapped.asCharBuffer();
59 public DoubleBuffer asDoubleBuffer() {
60 return this.wrapped.asDoubleBuffer();
64 public FloatBuffer asFloatBuffer() {
65 return this.wrapped.asFloatBuffer();
69 public IntBuffer asIntBuffer() {
70 return this.wrapped.asIntBuffer();
74 public LongBuffer asLongBuffer() {
75 return this.wrapped.asLongBuffer();
79 public ByteBuffer asReadOnlyBuffer() {
80 MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
82 buf.limit = this.limit;
83 buf.position = this.position;
89 public ShortBuffer asShortBuffer() {
90 return this.wrapped.asShortBuffer();
94 public ByteBuffer compact() {
95 if (this.wrapped.isReadOnly()) {
96 throw new ReadOnlyBufferException();
98 this.wrapped.limit(this.limit);
99 this.wrapped.position(this.position);
100 this.wrapped.compact();
101 this.wrapped.clear();
102 this.position = this.limit - this.position;
103 this.limit = this.capacity;
104 this.mark = UNSET_MARK;
109 public ByteBuffer duplicate() {
110 MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
112 buf.limit = this.limit;
113 buf.position = this.position;
114 buf.mark = this.mark;
120 this.wrapped.limit(this.limit);
121 this.wrapped.position(this.position);
122 byte result = this.wrapped.get();
128 public byte get(int index) {
129 this.wrapped.limit(this.limit);
130 this.wrapped.position(this.position);
131 return this.wrapped.get(index);
135 public char getChar() {
136 this.wrapped.limit(this.limit);
137 this.wrapped.position(this.position);
138 char result = this.wrapped.getChar();
139 this.position += CHAR_SIZE;
144 public char getChar(int index) {
145 this.wrapped.limit(this.limit);
146 this.wrapped.position(this.position);
147 return this.wrapped.getChar(index);
151 public double getDouble() {
152 this.wrapped.limit(this.limit);
153 this.wrapped.position(this.position);
154 double result = this.wrapped.getDouble();
155 this.position += DOUBLE_SIZE;
160 public double getDouble(int index) {
161 this.wrapped.limit(this.limit);
162 this.wrapped.position(this.position);
163 return this.wrapped.getDouble(index);
166 public PlatformAddress getEffectiveAddress() {
167 // BEGIN android-changed
168 PlatformAddress addr = ((DirectBuffer) this.wrapped).getEffectiveAddress();
169 effectiveDirectAddress = addr.toInt();
171 // END android-changed
175 public float getFloat() {
176 this.wrapped.limit(this.limit);
177 this.wrapped.position(this.position);
178 float result = this.wrapped.getFloat();
179 this.position += FLOAT_SIZE;
184 public float getFloat(int index) {
185 this.wrapped.limit(this.limit);
186 this.wrapped.position(this.position);
187 return this.wrapped.getFloat(index);
191 public int getInt() {
192 this.wrapped.limit(this.limit);
193 this.wrapped.position(this.position);
194 int result = this.wrapped.getInt();
195 this.position += INTEGER_SIZE;
200 public int getInt(int index) {
201 this.wrapped.limit(this.limit);
202 this.wrapped.position(this.position);
203 return this.wrapped.getInt(index);
207 public long getLong() {
208 this.wrapped.limit(this.limit);
209 this.wrapped.position(this.position);
210 long result = this.wrapped.getLong();
211 this.position += LONG_SIZE;
216 public long getLong(int index) {
217 this.wrapped.limit(this.limit);
218 this.wrapped.position(this.position);
219 return this.wrapped.getLong(index);
223 public short getShort() {
224 this.wrapped.limit(this.limit);
225 this.wrapped.position(this.position);
226 short result = this.wrapped.getShort();
227 this.position += SHORT_SIZE;
232 public short getShort(int index) {
233 this.wrapped.limit(this.limit);
234 this.wrapped.position(this.position);
235 return this.wrapped.getShort(index);
239 public boolean isDirect() {
244 public boolean isReadOnly() {
245 return this.wrapped.isReadOnly();
249 ByteBuffer orderImpl(ByteOrder byteOrder) {
250 super.orderImpl(byteOrder);
251 return this.wrapped.order(byteOrder);
255 public ByteBuffer put(byte b) {
256 this.wrapped.limit(this.limit);
257 this.wrapped.position(this.position);
264 public ByteBuffer put(byte[] src, int off, int len) {
265 this.wrapped.limit(this.limit);
266 this.wrapped.position(this.position);
267 this.wrapped.put(src, off, len);
268 this.position += len;
273 public ByteBuffer put(int index, byte b) {
274 this.wrapped.limit(this.limit);
275 this.wrapped.position(this.position);
276 this.wrapped.put(index, b);
281 public ByteBuffer putChar(char value) {
282 this.wrapped.limit(this.limit);
283 this.wrapped.position(this.position);
284 this.wrapped.putChar(value);
285 this.position += CHAR_SIZE;
290 public ByteBuffer putChar(int index, char value) {
291 this.wrapped.limit(this.limit);
292 this.wrapped.position(this.position);
293 this.wrapped.putChar(index, value);
298 public ByteBuffer putDouble(double value) {
299 this.wrapped.limit(this.limit);
300 this.wrapped.position(this.position);
301 this.wrapped.putDouble(value);
302 this.position += DOUBLE_SIZE;
307 public ByteBuffer putDouble(int index, double value) {
308 this.wrapped.limit(this.limit);
309 this.wrapped.position(this.position);
310 this.wrapped.putDouble(index, value);
315 public ByteBuffer putFloat(float value) {
316 this.wrapped.limit(this.limit);
317 this.wrapped.position(this.position);
318 this.wrapped.putFloat(value);
319 this.position += FLOAT_SIZE;
324 public ByteBuffer putFloat(int index, float value) {
325 this.wrapped.limit(this.limit);
326 this.wrapped.position(this.position);
327 this.wrapped.putFloat(index, value);
332 public ByteBuffer putInt(int index, int value) {
333 this.wrapped.limit(this.limit);
334 this.wrapped.position(this.position);
335 this.wrapped.putInt(index, value);
340 public ByteBuffer putInt(int value) {
341 this.wrapped.limit(this.limit);
342 this.wrapped.position(this.position);
343 this.wrapped.putInt(value);
344 this.position += INTEGER_SIZE;
349 public ByteBuffer putLong(int index, long value) {
350 this.wrapped.limit(this.limit);
351 this.wrapped.position(this.position);
352 this.wrapped.putLong(index, value);
357 public ByteBuffer putLong(long value) {
358 this.wrapped.limit(this.limit);
359 this.wrapped.position(this.position);
360 this.wrapped.putLong(value);
361 this.position += LONG_SIZE;
366 public ByteBuffer putShort(int index, short value) {
367 this.wrapped.limit(this.limit);
368 this.wrapped.position(this.position);
369 this.wrapped.putShort(index, value);
374 public ByteBuffer putShort(short value) {
375 this.wrapped.limit(this.limit);
376 this.wrapped.position(this.position);
377 this.wrapped.putShort(value);
378 this.position += SHORT_SIZE;
383 public ByteBuffer slice() {
384 this.wrapped.limit(this.limit);
385 this.wrapped.position(this.position);
386 MappedByteBufferAdapter result = new MappedByteBufferAdapter(
387 this.wrapped.slice());
388 this.wrapped.clear();
393 byte[] protectedArray() {
394 return this.wrapped.protectedArray();
398 int protectedArrayOffset() {
399 return this.wrapped.protectedArrayOffset();
403 boolean protectedHasArray() {
404 return this.wrapped.protectedHasArray();
407 public PlatformAddress getBaseAddress() {
408 return this.wrapped.getBaseAddress();
411 public boolean isAddressValid() {
412 return this.wrapped.isAddressValid();
415 public void addressValidityCheck() {
416 this.wrapped.addressValidityCheck();
423 public int getByteCapacity() {
424 return wrapped.getByteCapacity();