1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/* 2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership. 5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with 7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License. You may obtain a copy of the License at 8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * 11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software 12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and 15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License. 16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.nio; 19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/** 21fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes * ShortArrayBuffer implements short[]-based ShortBuffers. 22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 23fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughesfinal class ShortArrayBuffer extends ShortBuffer { 24fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 25fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final short[] backingArray; 26fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 27fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final int arrayOffset; 28fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 29fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final boolean isReadOnly; 30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 31fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes ShortArrayBuffer(short[] array) { 32fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes this(array.length, array, 0, false); 33fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 34adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 35fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private ShortArrayBuffer(int capacity, short[] backingArray, int arrayOffset, boolean isReadOnly) { 362589e301d1fb85960045c9ef29682d7e73b1aee7Narayan Kamath super(capacity, 0); 37fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes this.backingArray = backingArray; 38fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes this.arrayOffset = arrayOffset; 39fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes this.isReadOnly = isReadOnly; 40fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 41adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 42fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private static ShortArrayBuffer copy(ShortArrayBuffer other, int markOfOther, boolean isReadOnly) { 43fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes ShortArrayBuffer buf = new ShortArrayBuffer(other.capacity(), other.backingArray, other.arrayOffset, isReadOnly); 44fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes buf.limit = other.limit; 45fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes buf.position = other.position(); 46fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes buf.mark = markOfOther; 47fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return buf; 48fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 49fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 50fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer asReadOnlyBuffer() { 51fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return copy(this, mark, true); 52fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 53fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 54fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer compact() { 55fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 56fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 57adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 58fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes System.arraycopy(backingArray, position + arrayOffset, backingArray, arrayOffset, remaining()); 59fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes position = limit - position; 60fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes limit = capacity; 61fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes mark = UNSET_MARK; 62fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 63fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 64fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 65fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer duplicate() { 66fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return copy(this, mark, isReadOnly); 67fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 68fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 69fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer slice() { 70fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return new ShortArrayBuffer(remaining(), backingArray, arrayOffset + position, isReadOnly); 71fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 73fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public boolean isReadOnly() { 74fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return isReadOnly; 75fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 76fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 77fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override short[] protectedArray() { 78fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 79fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 80adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 81fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return backingArray; 82fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 84fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override int protectedArrayOffset() { 85fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 86fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 87adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 88fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return arrayOffset; 89fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 91fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override boolean protectedHasArray() { 92fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 93fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return false; 94adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 95fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return true; 96fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 97adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 98fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final short get() { 99fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (position == limit) { 100fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferUnderflowException(); 101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 102fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return backingArray[arrayOffset + position++]; 103fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 104fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 105fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final short get(int index) { 106fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes checkIndex(index); 107fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return backingArray[arrayOffset + index]; 108fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 110fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final ShortBuffer get(short[] dst, int dstOffset, int shortCount) { 111fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (shortCount > remaining()) { 112fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferUnderflowException(); 113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 114fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes System.arraycopy(backingArray, arrayOffset + position, dst, dstOffset, shortCount); 115fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes position += shortCount; 116fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 117fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 118fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 119fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final boolean isDirect() { 120fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return false; 121fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 122fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 123fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final ByteOrder order() { 124fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return ByteOrder.nativeOrder(); 125fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 126eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson 127fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer put(short c) { 128fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 129fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 131fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (position == limit) { 132fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferOverflowException(); 133fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 134fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes backingArray[arrayOffset + position++] = c; 135fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 136fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 138fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer put(int index, short c) { 139fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 140fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 142fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes checkIndex(index); 143fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes backingArray[arrayOffset + index] = c; 144fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 145fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 147fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public ShortBuffer put(short[] src, int srcOffset, int shortCount) { 148fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 149fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 150fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 151fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (shortCount > remaining()) { 152fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferOverflowException(); 153fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 154fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, shortCount); 155fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes position += shortCount; 156fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 157fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 159