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 org.apache.harmony.luni.platform.PlatformAddress;
     25 import org.apache.harmony.nio.internal.DirectBuffer;
     26 
     27 final class MappedByteBufferAdapter extends MappedByteBuffer implements
     28         DirectBuffer {
     29 
     30     private static final int CHAR_SIZE = 2;
     31 
     32     private static final int SHORT_SIZE = 2;
     33 
     34     private static final int INTEGER_SIZE = 4;
     35 
     36     private static final int LONG_SIZE = 8;
     37 
     38     private static final int FLOAT_SIZE = 4;
     39 
     40     private static final int DOUBLE_SIZE = 8;
     41 
     42     public MappedByteBufferAdapter(ByteBuffer buffer) {
     43         super(buffer);
     44     }
     45 
     46     public MappedByteBufferAdapter(PlatformAddress addr, int capa, int offset,
     47             int mode) {
     48         super(addr, capa, offset, mode);
     49     }
     50 
     51     @Override
     52     public CharBuffer asCharBuffer() {
     53         return this.wrapped.asCharBuffer();
     54     }
     55 
     56     @Override
     57     public DoubleBuffer asDoubleBuffer() {
     58         return this.wrapped.asDoubleBuffer();
     59     }
     60 
     61     @Override
     62     public FloatBuffer asFloatBuffer() {
     63         return this.wrapped.asFloatBuffer();
     64     }
     65 
     66     @Override
     67     public IntBuffer asIntBuffer() {
     68         return this.wrapped.asIntBuffer();
     69     }
     70 
     71     @Override
     72     public LongBuffer asLongBuffer() {
     73         return this.wrapped.asLongBuffer();
     74     }
     75 
     76     @Override
     77     public ByteBuffer asReadOnlyBuffer() {
     78         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
     79                 .asReadOnlyBuffer());
     80         buf.limit = this.limit;
     81         buf.position = this.position;
     82         buf.mark = this.mark;
     83         return buf;
     84     }
     85 
     86     @Override
     87     public ShortBuffer asShortBuffer() {
     88         return this.wrapped.asShortBuffer();
     89     }
     90 
     91     @Override
     92     public ByteBuffer compact() {
     93         if (this.wrapped.isReadOnly()) {
     94             throw new ReadOnlyBufferException();
     95         }
     96         this.wrapped.limit(this.limit);
     97         this.wrapped.position(this.position);
     98         this.wrapped.compact();
     99         this.wrapped.clear();
    100         this.position = this.limit - this.position;
    101         this.limit = this.capacity;
    102         this.mark = UNSET_MARK;
    103         return this;
    104     }
    105 
    106     @Override
    107     public ByteBuffer duplicate() {
    108         MappedByteBufferAdapter buf = new MappedByteBufferAdapter(this.wrapped
    109                 .duplicate());
    110         buf.limit = this.limit;
    111         buf.position = this.position;
    112         buf.mark = this.mark;
    113         return buf;
    114     }
    115 
    116     @Override
    117     public byte get() {
    118         this.wrapped.limit(this.limit);
    119         this.wrapped.position(this.position);
    120         byte result = this.wrapped.get();
    121         this.position++;
    122         return result;
    123     }
    124 
    125     @Override
    126     public byte get(int index) {
    127         this.wrapped.limit(this.limit);
    128         this.wrapped.position(this.position);
    129         return this.wrapped.get(index);
    130     }
    131 
    132     @Override
    133     public char getChar() {
    134         this.wrapped.limit(this.limit);
    135         this.wrapped.position(this.position);
    136         char result = this.wrapped.getChar();
    137         this.position += CHAR_SIZE;
    138         return result;
    139     }
    140 
    141     @Override
    142     public char getChar(int index) {
    143         this.wrapped.limit(this.limit);
    144         this.wrapped.position(this.position);
    145         return this.wrapped.getChar(index);
    146     }
    147 
    148     @Override
    149     public double getDouble() {
    150         this.wrapped.limit(this.limit);
    151         this.wrapped.position(this.position);
    152         double result = this.wrapped.getDouble();
    153         this.position += DOUBLE_SIZE;
    154         return result;
    155     }
    156 
    157     @Override
    158     public double getDouble(int index) {
    159         this.wrapped.limit(this.limit);
    160         this.wrapped.position(this.position);
    161         return this.wrapped.getDouble(index);
    162     }
    163 
    164     public PlatformAddress getEffectiveAddress() {
    165         // BEGIN android-changed
    166         PlatformAddress addr = ((DirectBuffer) this.wrapped).getEffectiveAddress();
    167         effectiveDirectAddress = addr.toInt();
    168         return addr;
    169         // END android-changed
    170     }
    171 
    172     @Override
    173     public float getFloat() {
    174         this.wrapped.limit(this.limit);
    175         this.wrapped.position(this.position);
    176         float result = this.wrapped.getFloat();
    177         this.position += FLOAT_SIZE;
    178         return result;
    179     }
    180 
    181     @Override
    182     public float getFloat(int index) {
    183         this.wrapped.limit(this.limit);
    184         this.wrapped.position(this.position);
    185         return this.wrapped.getFloat(index);
    186     }
    187 
    188     @Override
    189     public int getInt() {
    190         this.wrapped.limit(this.limit);
    191         this.wrapped.position(this.position);
    192         int result = this.wrapped.getInt();
    193         this.position += INTEGER_SIZE;
    194         return result;
    195     }
    196 
    197     @Override
    198     public int getInt(int index) {
    199         this.wrapped.limit(this.limit);
    200         this.wrapped.position(this.position);
    201         return this.wrapped.getInt(index);
    202     }
    203 
    204     @Override
    205     public long getLong() {
    206         this.wrapped.limit(this.limit);
    207         this.wrapped.position(this.position);
    208         long result = this.wrapped.getLong();
    209         this.position += LONG_SIZE;
    210         return result;
    211     }
    212 
    213     @Override
    214     public long getLong(int index) {
    215         this.wrapped.limit(this.limit);
    216         this.wrapped.position(this.position);
    217         return this.wrapped.getLong(index);
    218     }
    219 
    220     @Override
    221     public short getShort() {
    222         this.wrapped.limit(this.limit);
    223         this.wrapped.position(this.position);
    224         short result = this.wrapped.getShort();
    225         this.position += SHORT_SIZE;
    226         return result;
    227     }
    228 
    229     @Override
    230     public short getShort(int index) {
    231         this.wrapped.limit(this.limit);
    232         this.wrapped.position(this.position);
    233         return this.wrapped.getShort(index);
    234     }
    235 
    236     @Override
    237     public boolean isDirect() {
    238         return true;
    239     }
    240 
    241     @Override
    242     public boolean isReadOnly() {
    243         return this.wrapped.isReadOnly();
    244     }
    245 
    246     @Override
    247     ByteBuffer orderImpl(ByteOrder byteOrder) {
    248         super.orderImpl(byteOrder);
    249         return this.wrapped.order(byteOrder);
    250     }
    251 
    252     @Override
    253     public ByteBuffer put(byte b) {
    254         this.wrapped.limit(this.limit);
    255         this.wrapped.position(this.position);
    256         this.wrapped.put(b);
    257         this.position++;
    258         return this;
    259     }
    260 
    261     @Override
    262     public ByteBuffer put(byte[] src, int off, int len) {
    263         this.wrapped.limit(this.limit);
    264         this.wrapped.position(this.position);
    265         this.wrapped.put(src, off, len);
    266         this.position += len;
    267         return this;
    268     }
    269 
    270     @Override
    271     public ByteBuffer put(int index, byte b) {
    272         this.wrapped.limit(this.limit);
    273         this.wrapped.position(this.position);
    274         this.wrapped.put(index, b);
    275         return this;
    276     }
    277 
    278     @Override
    279     public ByteBuffer putChar(char value) {
    280         this.wrapped.limit(this.limit);
    281         this.wrapped.position(this.position);
    282         this.wrapped.putChar(value);
    283         this.position += CHAR_SIZE;
    284         return this;
    285     }
    286 
    287     @Override
    288     public ByteBuffer putChar(int index, char value) {
    289         this.wrapped.limit(this.limit);
    290         this.wrapped.position(this.position);
    291         this.wrapped.putChar(index, value);
    292         return this;
    293     }
    294 
    295     @Override
    296     public ByteBuffer putDouble(double value) {
    297         this.wrapped.limit(this.limit);
    298         this.wrapped.position(this.position);
    299         this.wrapped.putDouble(value);
    300         this.position += DOUBLE_SIZE;
    301         return this;
    302     }
    303 
    304     @Override
    305     public ByteBuffer putDouble(int index, double value) {
    306         this.wrapped.limit(this.limit);
    307         this.wrapped.position(this.position);
    308         this.wrapped.putDouble(index, value);
    309         return this;
    310     }
    311 
    312     @Override
    313     public ByteBuffer putFloat(float value) {
    314         this.wrapped.limit(this.limit);
    315         this.wrapped.position(this.position);
    316         this.wrapped.putFloat(value);
    317         this.position += FLOAT_SIZE;
    318         return this;
    319     }
    320 
    321     @Override
    322     public ByteBuffer putFloat(int index, float value) {
    323         this.wrapped.limit(this.limit);
    324         this.wrapped.position(this.position);
    325         this.wrapped.putFloat(index, value);
    326         return this;
    327     }
    328 
    329     @Override
    330     public ByteBuffer putInt(int index, int value) {
    331         this.wrapped.limit(this.limit);
    332         this.wrapped.position(this.position);
    333         this.wrapped.putInt(index, value);
    334         return this;
    335     }
    336 
    337     @Override
    338     public ByteBuffer putInt(int value) {
    339         this.wrapped.limit(this.limit);
    340         this.wrapped.position(this.position);
    341         this.wrapped.putInt(value);
    342         this.position += INTEGER_SIZE;
    343         return this;
    344     }
    345 
    346     @Override
    347     public ByteBuffer putLong(int index, long value) {
    348         this.wrapped.limit(this.limit);
    349         this.wrapped.position(this.position);
    350         this.wrapped.putLong(index, value);
    351         return this;
    352     }
    353 
    354     @Override
    355     public ByteBuffer putLong(long value) {
    356         this.wrapped.limit(this.limit);
    357         this.wrapped.position(this.position);
    358         this.wrapped.putLong(value);
    359         this.position += LONG_SIZE;
    360         return this;
    361     }
    362 
    363     @Override
    364     public ByteBuffer putShort(int index, short value) {
    365         this.wrapped.limit(this.limit);
    366         this.wrapped.position(this.position);
    367         this.wrapped.putShort(index, value);
    368         return this;
    369     }
    370 
    371     @Override
    372     public ByteBuffer putShort(short value) {
    373         this.wrapped.limit(this.limit);
    374         this.wrapped.position(this.position);
    375         this.wrapped.putShort(value);
    376         this.position += SHORT_SIZE;
    377         return this;
    378     }
    379 
    380     @Override
    381     public ByteBuffer slice() {
    382         this.wrapped.limit(this.limit);
    383         this.wrapped.position(this.position);
    384         MappedByteBufferAdapter result = new MappedByteBufferAdapter(
    385                 this.wrapped.slice());
    386         this.wrapped.clear();
    387         return result;
    388     }
    389 
    390     @Override
    391     byte[] protectedArray() {
    392         return this.wrapped.protectedArray();
    393     }
    394 
    395     @Override
    396     int protectedArrayOffset() {
    397         return this.wrapped.protectedArrayOffset();
    398     }
    399 
    400     @Override
    401     boolean protectedHasArray() {
    402         return this.wrapped.protectedHasArray();
    403     }
    404 
    405     public PlatformAddress getBaseAddress() {
    406         return this.wrapped.getBaseAddress();
    407     }
    408 
    409     public boolean isAddressValid() {
    410         return this.wrapped.isAddressValid();
    411     }
    412 
    413     public void addressValidityCheck() {
    414         this.wrapped.addressValidityCheck();
    415     }
    416 
    417     public void free() {
    418         this.wrapped.free();
    419     }
    420 
    421     public int getByteCapacity() {
    422         return wrapped.getByteCapacity();
    423     }
    424 }
    425