OSDN Git Service

83a51c1c650879a10d12c4e08f3ee6564400e941
[android-x86/dalvik.git] / libcore / nio / src / main / java / java / nio / MappedByteBufferAdapter.java
1 /*
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 // BEGIN android-note
19 // updated to a newer version of harmony
20 // added some missing updates on position and limit
21 // END android-note
22
23 package java.nio;
24
25 import org.apache.harmony.luni.platform.PlatformAddress;
26 import org.apache.harmony.nio.internal.DirectBuffer;
27
28
29 final class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
30
31     private static final int CHAR_SIZE = 2;
32
33     private static final int SHORT_SIZE = 2;
34
35     private static final int INTEGER_SIZE = 4;
36
37     private static final int LONG_SIZE = 8;
38
39     private static final int FLOAT_SIZE = 4;
40
41     private static final int DOUBLE_SIZE = 8;
42     
43     public MappedByteBufferAdapter(ByteBuffer buffer) {
44         super(buffer);
45     }
46
47     public MappedByteBufferAdapter(PlatformAddress addr, int capa, int offset,
48             int mode) {
49         super(addr, capa, offset, mode);
50     }
51
52     public CharBuffer asCharBuffer() {
53         return this.wrapped.asCharBuffer();
54     }
55
56     public DoubleBuffer asDoubleBuffer() {
57         return this.wrapped.asDoubleBuffer();
58     }
59
60     public FloatBuffer asFloatBuffer() {
61         return this.wrapped.asFloatBuffer();
62     }
63
64     public IntBuffer asIntBuffer() {
65         return this.wrapped.asIntBuffer();
66     }
67
68     public LongBuffer asLongBuffer() {
69         return this.wrapped.asLongBuffer();
70     }
71
72     public ByteBuffer asReadOnlyBuffer() {
73         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
74                 .asReadOnlyBuffer());
75         buf.limit = this.limit;
76         buf.position = this.position;
77         buf.mark = this.mark;
78         return buf;
79     }
80
81     public ShortBuffer asShortBuffer() {
82         return this.wrapped.asShortBuffer();
83     }
84
85     public ByteBuffer compact() {
86         if (this.wrapped.isReadOnly()) {
87             throw new ReadOnlyBufferException();
88         }
89         this.wrapped.limit(this.limit);
90         this.wrapped.position(this.position);
91         this.wrapped.compact();
92         this.wrapped.clear();
93         this.position = this.limit - this.position;
94         this.limit = this.capacity;
95         this.mark = UNSET_MARK;
96         return this;
97     }
98
99     public ByteBuffer duplicate() {
100         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
101                 .duplicate());
102         buf.limit = this.limit;
103         buf.position = this.position;
104         buf.mark = this.mark;
105         return buf;
106     }
107
108     public byte get() {
109         this.wrapped.limit(this.limit);
110         this.wrapped.position(this.position);
111         byte result = this.wrapped.get(); 
112         this.position++;
113         return result;
114     }
115
116     public byte get(int index) {
117         this.wrapped.limit(this.limit);
118         this.wrapped.position(this.position);
119         return this.wrapped.get(index);
120     }
121
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;
127         return result;
128     }
129
130     public char getChar(int index) {
131         this.wrapped.limit(this.limit);
132         this.wrapped.position(this.position);
133         return this.wrapped.getChar(index);
134     }
135
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;
141         return result;
142     }
143
144     public double getDouble(int index) {
145         this.wrapped.limit(this.limit);
146         this.wrapped.position(this.position);
147         return this.wrapped.getDouble(index);
148     }
149
150     public PlatformAddress getEffectiveAddress() {
151         return ((DirectBuffer) this.wrapped).getEffectiveAddress();
152     }
153
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;
159         return result;
160     }
161
162     public float getFloat(int index) {
163         this.wrapped.limit(this.limit);
164         this.wrapped.position(this.position);
165         return this.wrapped.getFloat(index);
166     }
167
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;
173         return result;
174     }
175
176     public int getInt(int index) {
177         this.wrapped.limit(this.limit);
178         this.wrapped.position(this.position);
179         return this.wrapped.getInt(index);
180     }
181
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;
187         return result;
188     }
189
190     public long getLong(int index) {
191         this.wrapped.limit(this.limit);
192         this.wrapped.position(this.position);
193         return this.wrapped.getLong(index);
194     }
195
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;
201         return result;
202     }
203
204     public short getShort(int index) {
205         this.wrapped.limit(this.limit);
206         this.wrapped.position(this.position);
207         return this.wrapped.getShort(index);
208     }
209
210     public boolean isDirect() {
211         return true;
212     }
213
214     public boolean isReadOnly() {
215         return this.wrapped.isReadOnly();
216     }
217
218     ByteBuffer orderImpl(ByteOrder byteOrder) {
219         super.orderImpl(byteOrder);
220         return this.wrapped.order(byteOrder);
221     }
222
223     public ByteBuffer put(byte b) {
224         this.wrapped.limit(this.limit);
225         this.wrapped.position(this.position);
226         this.wrapped.put(b);
227         this.position++;
228         return this;
229     }
230
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;
236         return this;
237     }
238
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);
243         return this;
244     }
245
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;
251         return this;
252     }
253
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);
258         return this;
259     }
260
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;
266         return this;
267     }
268
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);
273         return this;
274     }
275
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;
281         return this;
282     }
283
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);
288         return this;
289     }
290
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);
295         return this;
296     }
297
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;
303         return this;
304     }
305
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);
310         return this;
311     }
312
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;
318         return this;
319     }
320
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);
325         return this;
326     }
327
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;
333         return this;
334     }
335
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();
342         return result;
343     }
344
345     byte[] protectedArray() {
346         return this.wrapped.protectedArray();
347     }
348
349     int protectedArrayOffset() {
350         return this.wrapped.protectedArrayOffset();
351     }
352
353     boolean protectedHasArray() {
354         return this.wrapped.protectedHasArray();
355     }
356
357     public PlatformAddress getBaseAddress() {
358         return this.wrapped.getBaseAddress();
359     }
360
361     public boolean isAddressValid() {
362         return this.wrapped.isAddressValid();
363     }
364
365     public void addressValidityCheck() {
366         this.wrapped.addressValidityCheck();
367     }
368
369     public void free() {
370         this.wrapped.free();
371     }
372
373     public int getByteCapacity() {
374         return wrapped.getByteCapacity();
375     }
376 }