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 /** ShortArrayBuffer, ReadWriteShortArrayBuffer and ReadOnlyShortArrayBuffer compose the implementation of array based short 21 * buffers. 22 * <p> 23 * ShortArrayBuffer implements all the shared readonly methods and is extended by the other two classes. 24 * </p> 25 * <p> 26 * All methods are marked final for runtime performance. 27 * </p> */ 28 abstract class ShortArrayBuffer extends ShortBuffer { 29 30 protected final short[] backingArray; 31 32 protected final int offset; 33 34 ShortArrayBuffer (short[] array) { 35 this(array.length, array, 0); 36 } 37 38 ShortArrayBuffer (int capacity) { 39 this(capacity, new short[capacity], 0); 40 } 41 42 ShortArrayBuffer (int capacity, short[] backingArray, int offset) { 43 super(capacity); 44 this.backingArray = backingArray; 45 this.offset = offset; 46 } 47 48 public final short get () { 49 if (position == limit) { 50 throw new BufferUnderflowException(); 51 } 52 return backingArray[offset + position++]; 53 } 54 55 public final short get (int index) { 56 if (index < 0 || index >= limit) { 57 throw new IndexOutOfBoundsException(); 58 } 59 return backingArray[offset + index]; 60 } 61 62 public final ShortBuffer get (short[] dest, int off, int len) { 63 int length = dest.length; 64 if (off < 0 || len < 0 || (long)off + (long)len > length) { 65 throw new IndexOutOfBoundsException(); 66 } 67 if (len > remaining()) { 68 throw new BufferUnderflowException(); 69 } 70 System.arraycopy(backingArray, offset + position, dest, off, len); 71 position += len; 72 return this; 73 } 74 75 public final boolean isDirect () { 76 return false; 77 } 78 79 public final ByteOrder order () { 80 return ByteOrder.nativeOrder(); 81 } 82 83 } 84