Home | History | Annotate | Download | only in nio
      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 // added some missing updates on position and limit
     20 // END android-note
     21 
     22 package java.nio;
     23 
     24 import java.nio.channels.FileChannel.MapMode;
     25 import org.apache.harmony.luni.platform.PlatformAddress;
     26 import org.apache.harmony.nio.internal.DirectBuffer;
     27 
     28 /**
     29  * @hide
     30  */
     31 public final class MappedByteBufferAdapter extends MappedByteBuffer implements DirectBuffer {
     32 
     33     private static final int CHAR_SIZE = 2;
     34 
     35     private static final int SHORT_SIZE = 2;
     36 
     37     private static final int INTEGER_SIZE = 4;
     38 
     39     private static final int LONG_SIZE = 8;
     40 
     41     private static final int FLOAT_SIZE = 4;
     42 
     43     private static final int DOUBLE_SIZE = 8;
     44 
     45     public MappedByteBufferAdapter(ByteBuffer buffer) {
     46         super(buffer);
     47     }
     48 
     49     public MappedByteBufferAdapter(PlatformAddress addr, int capa, int offset, MapMode mode) {
     50         super(addr, capa, offset, mode);
     51     }
     52 
     53     @Override
     54     public CharBuffer asCharBuffer() {
     55         return this.wrapped.asCharBuffer();
     56     }
     57 
     58     @Override
     59     public DoubleBuffer asDoubleBuffer() {
     60         return this.wrapped.asDoubleBuffer();
     61     }
     62 
     63     @Override
     64     public FloatBuffer asFloatBuffer() {
     65         return this.wrapped.asFloatBuffer();
     66     }
     67 
     68     @Override
     69     public IntBuffer asIntBuffer() {
     70         return this.wrapped.asIntBuffer();
     71     }
     72 
     73     @Override
     74     public LongBuffer asLongBuffer() {
     75         return this.wrapped.asLongBuffer();
     76     }
     77 
     78     @Override
     79     public ByteBuffer asReadOnlyBuffer() {
     80         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
     81                 .asReadOnlyBuffer());
     82         buf.limit = this.limit;
     83         buf.position = this.position;
     84         buf.mark = this.mark;
     85         return buf;
     86     }
     87 
     88     @Override
     89     public ShortBuffer asShortBuffer() {
     90         return this.wrapped.asShortBuffer();
     91     }
     92 
     93     @Override
     94     public ByteBuffer compact() {
     95         if (this.wrapped.isReadOnly()) {
     96             throw new ReadOnlyBufferException();
     97         }
     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;
    105         return this;
    106     }
    107 
    108     @Override
    109     public ByteBuffer duplicate() {
    110         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
    111                 .duplicate());
    112         buf.limit = this.limit;
    113         buf.position = this.position;
    114         buf.mark = this.mark;
    115         return buf;
    116     }
    117 
    118     @Override
    119     public byte get() {
    120         this.wrapped.limit(this.limit);
    121         this.wrapped.position(this.position);
    122         byte result = this.wrapped.get();
    123         this.position++;
    124         return result;
    125     }
    126 
    127     @Override
    128     public byte get(int index) {
    129         this.wrapped.limit(this.limit);
    130         this.wrapped.position(this.position);
    131         return this.wrapped.get(index);
    132     }
    133 
    134     @Override
    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;
    140         return result;
    141     }
    142 
    143     @Override
    144     public char getChar(int index) {
    145         this.wrapped.limit(this.limit);
    146         this.wrapped.position(this.position);
    147         return this.wrapped.getChar(index);
    148     }
    149 
    150     @Override
    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;
    156         return result;
    157     }
    158 
    159     @Override
    160     public double getDouble(int index) {
    161         this.wrapped.limit(this.limit);
    162         this.wrapped.position(this.position);
    163         return this.wrapped.getDouble(index);
    164     }
    165 
    166     public PlatformAddress getEffectiveAddress() {
    167         // BEGIN android-changed
    168         PlatformAddress addr = ((DirectBuffer) this.wrapped).getEffectiveAddress();
    169         effectiveDirectAddress = addr.toInt();
    170         return addr;
    171         // END android-changed
    172     }
    173 
    174     @Override
    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;
    180         return result;
    181     }
    182 
    183     @Override
    184     public float getFloat(int index) {
    185         this.wrapped.limit(this.limit);
    186         this.wrapped.position(this.position);
    187         return this.wrapped.getFloat(index);
    188     }
    189 
    190     @Override
    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;
    196         return result;
    197     }
    198 
    199     @Override
    200     public int getInt(int index) {
    201         this.wrapped.limit(this.limit);
    202         this.wrapped.position(this.position);
    203         return this.wrapped.getInt(index);
    204     }
    205 
    206     @Override
    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;
    212         return result;
    213     }
    214 
    215     @Override
    216     public long getLong(int index) {
    217         this.wrapped.limit(this.limit);
    218         this.wrapped.position(this.position);
    219         return this.wrapped.getLong(index);
    220     }
    221 
    222     @Override
    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;
    228         return result;
    229     }
    230 
    231     @Override
    232     public short getShort(int index) {
    233         this.wrapped.limit(this.limit);
    234         this.wrapped.position(this.position);
    235         return this.wrapped.getShort(index);
    236     }
    237 
    238     @Override
    239     public boolean isDirect() {
    240         return true;
    241     }
    242 
    243     @Override
    244     public boolean isReadOnly() {
    245         return this.wrapped.isReadOnly();
    246     }
    247 
    248     @Override
    249     ByteBuffer orderImpl(ByteOrder byteOrder) {
    250         super.orderImpl(byteOrder);
    251         return this.wrapped.order(byteOrder);
    252     }
    253 
    254     @Override
    255     public ByteBuffer put(byte b) {
    256         this.wrapped.limit(this.limit);
    257         this.wrapped.position(this.position);
    258         this.wrapped.put(b);
    259         this.position++;
    260         return this;
    261     }
    262 
    263     @Override
    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;
    269         return this;
    270     }
    271 
    272     @Override
    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);
    277         return this;
    278     }
    279 
    280     @Override
    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;
    286         return this;
    287     }
    288 
    289     @Override
    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);
    294         return this;
    295     }
    296 
    297     @Override
    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;
    303         return this;
    304     }
    305 
    306     @Override
    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);
    311         return this;
    312     }
    313 
    314     @Override
    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;
    320         return this;
    321     }
    322 
    323     @Override
    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);
    328         return this;
    329     }
    330 
    331     @Override
    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);
    336         return this;
    337     }
    338 
    339     @Override
    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;
    345         return this;
    346     }
    347 
    348     @Override
    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);
    353         return this;
    354     }
    355 
    356     @Override
    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;
    362         return this;
    363     }
    364 
    365     @Override
    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);
    370         return this;
    371     }
    372 
    373     @Override
    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;
    379         return this;
    380     }
    381 
    382     @Override
    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();
    389         return result;
    390     }
    391 
    392     @Override
    393     byte[] protectedArray() {
    394         return this.wrapped.protectedArray();
    395     }
    396 
    397     @Override
    398     int protectedArrayOffset() {
    399         return this.wrapped.protectedArrayOffset();
    400     }
    401 
    402     @Override
    403     boolean protectedHasArray() {
    404         return this.wrapped.protectedHasArray();
    405     }
    406 
    407     public PlatformAddress getBaseAddress() {
    408         return this.wrapped.getBaseAddress();
    409     }
    410 
    411     public boolean isAddressValid() {
    412         return this.wrapped.isAddressValid();
    413     }
    414 
    415     public void addressValidityCheck() {
    416         this.wrapped.addressValidityCheck();
    417     }
    418 
    419     public void free() {
    420         this.wrapped.free();
    421     }
    422 
    423     public int getByteCapacity() {
    424         return wrapped.getByteCapacity();
    425     }
    426 }
    427