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