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 implements double[]-based DoubleBuffers. 22 */ 23 final class DoubleArrayBuffer extends DoubleBuffer { 24 25 private final double[] backingArray; 26 27 private final int arrayOffset; 28 29 private final boolean isReadOnly; 30 31 DoubleArrayBuffer(double[] array) { 32 this(array.length, array, 0, false); 33 } 34 35 private DoubleArrayBuffer(int capacity, double[] backingArray, int arrayOffset, boolean isReadOnly) { 36 super(capacity); 37 this.backingArray = backingArray; 38 this.arrayOffset = arrayOffset; 39 this.isReadOnly = isReadOnly; 40 } 41 42 private static DoubleArrayBuffer copy(DoubleArrayBuffer other, int markOfOther, boolean isReadOnly) { 43 DoubleArrayBuffer buf = new DoubleArrayBuffer(other.capacity(), other.backingArray, other.arrayOffset, isReadOnly); 44 buf.limit = other.limit; 45 buf.position = other.position(); 46 buf.mark = markOfOther; 47 return buf; 48 } 49 50 @Override public DoubleBuffer asReadOnlyBuffer() { 51 return copy(this, mark, true); 52 } 53 54 @Override public DoubleBuffer compact() { 55 if (isReadOnly) { 56 throw new ReadOnlyBufferException(); 57 } 58 System.arraycopy(backingArray, position + arrayOffset, backingArray, arrayOffset, remaining()); 59 position = limit - position; 60 limit = capacity; 61 mark = UNSET_MARK; 62 return this; 63 } 64 65 @Override public DoubleBuffer duplicate() { 66 return copy(this, mark, isReadOnly); 67 } 68 69 @Override public DoubleBuffer slice() { 70 return new DoubleArrayBuffer(remaining(), backingArray, arrayOffset + position, isReadOnly); 71 } 72 73 @Override public boolean isReadOnly() { 74 return isReadOnly; 75 } 76 77 @Override double[] protectedArray() { 78 if (isReadOnly) { 79 throw new ReadOnlyBufferException(); 80 } 81 return backingArray; 82 } 83 84 @Override int protectedArrayOffset() { 85 if (isReadOnly) { 86 throw new ReadOnlyBufferException(); 87 } 88 return arrayOffset; 89 } 90 91 @Override boolean protectedHasArray() { 92 if (isReadOnly) { 93 return false; 94 } 95 return true; 96 } 97 98 @Override public final double get() { 99 if (position == limit) { 100 throw new BufferUnderflowException(); 101 } 102 return backingArray[arrayOffset + position++]; 103 } 104 105 @Override public final double get(int index) { 106 checkIndex(index); 107 return backingArray[arrayOffset + index]; 108 } 109 110 @Override public final DoubleBuffer get(double[] dst, int dstOffset, int doubleCount) { 111 if (doubleCount > remaining()) { 112 throw new BufferUnderflowException(); 113 } 114 System.arraycopy(backingArray, arrayOffset + position, dst, dstOffset, doubleCount); 115 position += doubleCount; 116 return this; 117 } 118 119 @Override public final boolean isDirect() { 120 return false; 121 } 122 123 @Override public final ByteOrder order() { 124 return ByteOrder.nativeOrder(); 125 } 126 127 @Override public DoubleBuffer put(double c) { 128 if (isReadOnly) { 129 throw new ReadOnlyBufferException(); 130 } 131 if (position == limit) { 132 throw new BufferOverflowException(); 133 } 134 backingArray[arrayOffset + position++] = c; 135 return this; 136 } 137 138 @Override public DoubleBuffer put(int index, double c) { 139 if (isReadOnly) { 140 throw new ReadOnlyBufferException(); 141 } 142 checkIndex(index); 143 backingArray[arrayOffset + index] = c; 144 return this; 145 } 146 147 @Override public DoubleBuffer put(double[] src, int srcOffset, int doubleCount) { 148 if (isReadOnly) { 149 throw new ReadOnlyBufferException(); 150 } 151 if (doubleCount > remaining()) { 152 throw new BufferOverflowException(); 153 } 154 System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, doubleCount); 155 position += doubleCount; 156 return this; 157 } 158 } 159