Home | History | Annotate | Download | only in nio
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 package java.nio;
     28 
     29 /**
     30  * A read/write HeapLongBuffer.
     31  */
     32 
     33 class HeapLongBuffer
     34         extends LongBuffer {
     35 
     36     // For speed these fields are actually declared in X-Buffer;
     37     // these declarations are here as documentation
     38     /*
     39 
     40       protected final long[] hb;
     41       protected final int offset;
     42 
     43     */
     44 
     45     HeapLongBuffer(int cap, int lim) {            // package-private
     46         this(cap, lim, false);
     47     }
     48 
     49     HeapLongBuffer(int cap, int lim, boolean isReadOnly) {            // package-private
     50         super(-1, 0, lim, cap, new long[cap], 0);
     51         this.isReadOnly = isReadOnly;
     52     }
     53 
     54     HeapLongBuffer(long[] buf, int off, int len) { // package-private
     55         this(buf, off, len, false);
     56     }
     57 
     58     HeapLongBuffer(long[] buf, int off, int len, boolean isReadOnly) { // package-private
     59         super(-1, off, off + len, buf.length, buf, 0);
     60         this.isReadOnly = isReadOnly;
     61     }
     62 
     63     protected HeapLongBuffer(long[] buf,
     64                              int mark, int pos, int lim, int cap,
     65                              int off) {
     66         this(buf, mark, pos, lim, cap, off, false);
     67     }
     68 
     69     protected HeapLongBuffer(long[] buf,
     70                              int mark, int pos, int lim, int cap,
     71                              int off, boolean isReadOnly) {
     72         super(mark, pos, lim, cap, buf, off);
     73         this.isReadOnly = isReadOnly;
     74     }
     75 
     76     public LongBuffer slice() {
     77         return new HeapLongBuffer(hb,
     78                 -1,
     79                 0,
     80                 this.remaining(),
     81                 this.remaining(),
     82                 this.position() + offset,
     83                 isReadOnly);
     84     }
     85 
     86     public LongBuffer duplicate() {
     87         return new HeapLongBuffer(hb,
     88                 this.markValue(),
     89                 this.position(),
     90                 this.limit(),
     91                 this.capacity(),
     92                 offset,
     93                 isReadOnly);
     94     }
     95 
     96     public LongBuffer asReadOnlyBuffer() {
     97         return new HeapLongBuffer(hb,
     98                 this.markValue(),
     99                 this.position(),
    100                 this.limit(),
    101                 this.capacity(),
    102                 offset, true);
    103     }
    104 
    105 
    106     protected int ix(int i) {
    107         return i + offset;
    108     }
    109 
    110     public long get() {
    111         return hb[ix(nextGetIndex())];
    112     }
    113 
    114     public long get(int i) {
    115         return hb[ix(checkIndex(i))];
    116     }
    117 
    118     public LongBuffer get(long[] dst, int offset, int length) {
    119         checkBounds(offset, length, dst.length);
    120         if (length > remaining())
    121             throw new BufferUnderflowException();
    122         System.arraycopy(hb, ix(position()), dst, offset, length);
    123         position(position() + length);
    124         return this;
    125     }
    126 
    127     public boolean isDirect() {
    128         return false;
    129     }
    130 
    131     public boolean isReadOnly() {
    132         return isReadOnly;
    133     }
    134 
    135     public LongBuffer put(long x) {
    136         if (isReadOnly) {
    137             throw new ReadOnlyBufferException();
    138         }
    139         hb[ix(nextPutIndex())] = x;
    140         return this;
    141     }
    142 
    143     public LongBuffer put(int i, long x) {
    144         if (isReadOnly) {
    145             throw new ReadOnlyBufferException();
    146         }
    147         hb[ix(checkIndex(i))] = x;
    148         return this;
    149     }
    150 
    151     public LongBuffer put(long[] src, int offset, int length) {
    152         if (isReadOnly) {
    153             throw new ReadOnlyBufferException();
    154         }
    155         checkBounds(offset, length, src.length);
    156         if (length > remaining())
    157             throw new BufferOverflowException();
    158         System.arraycopy(src, offset, hb, ix(position()), length);
    159         position(position() + length);
    160         return this;
    161     }
    162 
    163     public LongBuffer put(LongBuffer src) {
    164         if (isReadOnly) {
    165             throw new ReadOnlyBufferException();
    166         }
    167         if (src instanceof HeapLongBuffer) {
    168             if (src == this)
    169                 throw new IllegalArgumentException();
    170             HeapLongBuffer sb = (HeapLongBuffer) src;
    171             int n = sb.remaining();
    172             if (n > remaining())
    173                 throw new BufferOverflowException();
    174             System.arraycopy(sb.hb, sb.ix(sb.position()),
    175                     hb, ix(position()), n);
    176             sb.position(sb.position() + n);
    177             position(position() + n);
    178         } else if (src.isDirect()) {
    179             int n = src.remaining();
    180             if (n > remaining())
    181                 throw new BufferOverflowException();
    182             src.get(hb, ix(position()), n);
    183             position(position() + n);
    184         } else {
    185             super.put(src);
    186         }
    187         return this;
    188     }
    189 
    190     public LongBuffer compact() {
    191         if (isReadOnly) {
    192             throw new ReadOnlyBufferException();
    193         }
    194         System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
    195         position(remaining());
    196         limit(capacity());
    197         discardMark();
    198         return this;
    199     }
    200 
    201     public ByteOrder order() {
    202         return ByteOrder.nativeOrder();
    203     }
    204 }
    205