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 package java.nio;
     19 
     20 /**
     21  * DoubleArrayBuffer, ReadWriteDoubleArrayBuffer and ReadOnlyDoubleArrayBuffer
     22  * compose the implementation of array based double buffers.
     23  * <p>
     24  * ReadWriteDoubleArrayBuffer extends DoubleArrayBuffer with all the write
     25  * methods.
     26  * </p>
     27  * <p>
     28  * This class is marked final for runtime performance.
     29  * </p>
     30  *
     31  */
     32 final class ReadWriteDoubleArrayBuffer extends DoubleArrayBuffer {
     33 
     34     static ReadWriteDoubleArrayBuffer copy(DoubleArrayBuffer other,
     35             int markOfOther) {
     36         ReadWriteDoubleArrayBuffer buf = new ReadWriteDoubleArrayBuffer(other
     37                 .capacity(), other.backingArray, other.offset);
     38         buf.limit = other.limit();
     39         buf.position = other.position();
     40         buf.mark = markOfOther;
     41         return buf;
     42     }
     43 
     44     ReadWriteDoubleArrayBuffer(double[] array) {
     45         super(array);
     46     }
     47 
     48     ReadWriteDoubleArrayBuffer(int capacity) {
     49         super(capacity);
     50     }
     51 
     52     ReadWriteDoubleArrayBuffer(int capacity, double[] backingArray,
     53             int arrayOffset) {
     54         super(capacity, backingArray, arrayOffset);
     55     }
     56 
     57     @Override
     58     public DoubleBuffer asReadOnlyBuffer() {
     59         return ReadOnlyDoubleArrayBuffer.copy(this, mark);
     60     }
     61 
     62     @Override
     63     public DoubleBuffer compact() {
     64         System.arraycopy(backingArray, position + offset, backingArray, offset,
     65                 remaining());
     66         position = limit - position;
     67         limit = capacity;
     68         mark = UNSET_MARK;
     69         return this;
     70     }
     71 
     72     @Override
     73     public DoubleBuffer duplicate() {
     74         return copy(this, mark);
     75     }
     76 
     77     @Override
     78     public boolean isReadOnly() {
     79         return false;
     80     }
     81 
     82     @Override
     83     protected double[] protectedArray() {
     84         return backingArray;
     85     }
     86 
     87     @Override
     88     protected int protectedArrayOffset() {
     89         return offset;
     90     }
     91 
     92     @Override
     93     protected boolean protectedHasArray() {
     94         return true;
     95     }
     96 
     97     @Override
     98     public DoubleBuffer put(double c) {
     99         if (position == limit) {
    100             throw new BufferOverflowException();
    101         }
    102         backingArray[offset + position++] = c;
    103         return this;
    104     }
    105 
    106     @Override
    107     public DoubleBuffer put(int index, double c) {
    108         if (index < 0 || index >= limit) {
    109             throw new IndexOutOfBoundsException();
    110         }
    111         backingArray[offset + index] = c;
    112         return this;
    113     }
    114 
    115     @Override
    116     public DoubleBuffer put(double[] src, int off, int len) {
    117         int length = src.length;
    118         if (off < 0 || len < 0 || (long) off + (long) len > length) {
    119             throw new IndexOutOfBoundsException();
    120         }
    121         if (len > remaining()) {
    122             throw new BufferOverflowException();
    123         }
    124         System.arraycopy(src, off, backingArray, offset + position, len);
    125         position += len;
    126         return this;
    127     }
    128 
    129     @Override
    130     public DoubleBuffer slice() {
    131         return new ReadWriteDoubleArrayBuffer(remaining(), backingArray, offset
    132                 + position);
    133     }
    134 
    135 }
    136