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 * LongArrayBuffer implements long[]-based LongBuffers. 22 */ 23final class LongArrayBuffer extends LongBuffer { 24 25 private final long[] backingArray; 26 27 private final int arrayOffset; 28 29 private final boolean isReadOnly; 30 31 LongArrayBuffer(long[] array) { 32 this(array.length, array, 0, false); 33 } 34 35 private LongArrayBuffer(int capacity, long[] 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 LongArrayBuffer copy(LongArrayBuffer other, int markOfOther, boolean isReadOnly) { 43 LongArrayBuffer buf = new LongArrayBuffer(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 LongBuffer asReadOnlyBuffer() { 51 return copy(this, mark, true); 52 } 53 54 @Override public LongBuffer 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 LongBuffer duplicate() { 66 return copy(this, mark, isReadOnly); 67 } 68 69 @Override public LongBuffer slice() { 70 return new LongArrayBuffer(remaining(), backingArray, arrayOffset + position, isReadOnly); 71 } 72 73 @Override public boolean isReadOnly() { 74 return isReadOnly; 75 } 76 77 @Override long[] 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 long get() { 99 if (position == limit) { 100 throw new BufferUnderflowException(); 101 } 102 return backingArray[arrayOffset + position++]; 103 } 104 105 @Override public final long get(int index) { 106 checkIndex(index); 107 return backingArray[arrayOffset + index]; 108 } 109 110 @Override public final LongBuffer get(long[] dst, int dstOffset, int longCount) { 111 if (longCount > remaining()) { 112 throw new BufferUnderflowException(); 113 } 114 System.arraycopy(backingArray, arrayOffset + position, dst, dstOffset, longCount); 115 position += longCount; 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 LongBuffer put(long 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 LongBuffer put(int index, long 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 LongBuffer put(long[] src, int srcOffset, int longCount) { 148 if (isReadOnly) { 149 throw new ReadOnlyBufferException(); 150 } 151 if (longCount > remaining()) { 152 throw new BufferOverflowException(); 153 } 154 System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, longCount); 155 position += longCount; 156 return this; 157 } 158 159} 160