Home | History | Annotate | Download | only in nio
      1 /*
      2  * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package java.nio;
     27 
     28 import libcore.io.Memory;
     29 
     30 class ByteBufferAsShortBuffer extends ShortBuffer {       // package-private
     31 
     32     protected final ByteBuffer bb;
     33     protected final int offset;
     34     private final ByteOrder order;
     35 
     36     ByteBufferAsShortBuffer(ByteBuffer bb,
     37                             int mark, int pos, int lim, int cap,
     38                             int off, ByteOrder order) {
     39         super(mark, pos, lim, cap);
     40         this.bb = bb.duplicate();
     41         this.isReadOnly = bb.isReadOnly;
     42         // There are only two possibilities for the type of ByteBuffer "bb", viz, DirectByteBuffer and
     43         // HeapByteBuffer. We only have to initialize the field when bb is an instance of
     44         // DirectByteBuffer.
     45         // The address field is use by NIOAccess#getBasePointer and GetDirectBufferAddress method
     46         // in art which return the address of the first usable byte of the underlying memory, i.e,
     47         // the position of parent buffer. Therefore, value of "off" will be equal to parent buffer's
     48         // position when the method is called from either HeapByteBuffer or DirectByteBuffer.
     49         if (bb instanceof DirectByteBuffer) {
     50             this.address = bb.address + off;
     51         }
     52         this.bb.order(order);
     53         this.order = order;
     54         offset = off;
     55     }
     56 
     57     public ShortBuffer slice() {
     58         int pos = this.position();
     59         int lim = this.limit();
     60         assert (pos <= lim);
     61         int rem = (pos <= lim ? lim - pos : 0);
     62         int off = (pos << 1) + offset;
     63         assert (off >= 0);
     64         return new ByteBufferAsShortBuffer(bb, -1, 0, rem, rem, off, order);
     65     }
     66 
     67     public ShortBuffer duplicate() {
     68         return new ByteBufferAsShortBuffer(bb,
     69                 this.markValue(),
     70                 this.position(),
     71                 this.limit(),
     72                 this.capacity(),
     73                 offset, order);
     74     }
     75 
     76     public ShortBuffer asReadOnlyBuffer() {
     77         return new ByteBufferAsShortBuffer(bb.asReadOnlyBuffer(),
     78                 this.markValue(),
     79                 this.position(),
     80                 this.limit(),
     81                 this.capacity(),
     82                 offset, order);
     83     }
     84 
     85     protected int ix(int i) {
     86         return (i << 1) + offset;
     87     }
     88 
     89     public short get() {
     90         return get(nextGetIndex());
     91     }
     92 
     93     public short get(int i) {
     94         return bb.getShortUnchecked(ix(checkIndex(i)));
     95 
     96     }
     97 
     98     public ShortBuffer get(short[] dst, int offset, int length) {
     99         checkBounds(offset, length, dst.length);
    100         if (length > remaining())
    101             throw new BufferUnderflowException();
    102         bb.getUnchecked(ix(position), dst, offset, length);
    103         position += length;
    104         return this;
    105     }
    106 
    107     public ShortBuffer put(short x) {
    108         put(nextPutIndex(), x);
    109         return this;
    110     }
    111 
    112     public ShortBuffer put(int i, short x) {
    113         if (isReadOnly) {
    114             throw new ReadOnlyBufferException();
    115         }
    116         bb.putShortUnchecked(ix(checkIndex(i)), x);
    117         return this;
    118     }
    119 
    120     public ShortBuffer put(short[] src, int offset, int length) {
    121         checkBounds(offset, length, src.length);
    122         if (length > remaining())
    123             throw new BufferOverflowException();
    124         bb.putUnchecked(ix(position), src, offset, length);
    125         position += length;
    126         return this;
    127     }
    128 
    129     public ShortBuffer compact() {
    130         if (isReadOnly) {
    131             throw new ReadOnlyBufferException();
    132         }
    133         int pos = position();
    134         int lim = limit();
    135         assert (pos <= lim);
    136         int rem = (pos <= lim ? lim - pos : 0);
    137         if (!(bb instanceof DirectByteBuffer)) {
    138             System.arraycopy(bb.array(), ix(pos), bb.array(), ix(0), rem << 1);
    139         } else {
    140             Memory.memmove(this, ix(0), this, ix(pos), rem << 1);
    141         }
    142         position(rem);
    143         limit(capacity());
    144         discardMark();
    145         return this;
    146     }
    147 
    148     public boolean isDirect() {
    149         return bb.isDirect();
    150     }
    151 
    152     public boolean isReadOnly() {
    153         return isReadOnly;
    154     }
    155 
    156     public ByteOrder order() {
    157         return order;
    158     }
    159 }
    160