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 * CharArrayBuffer implements char[]-based CharBuffers. 22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */ 23fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughesfinal class CharArrayBuffer extends CharBuffer { 24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 25fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final char[] backingArray; 26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 27fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final int arrayOffset; 28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 29fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private final boolean isReadOnly; 30fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 31fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes CharArrayBuffer(char[] array) { 32fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes this(array.length, array, 0, false); 33fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 34fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 35fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes private CharArrayBuffer(int capacity, char[] 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 CharArrayBuffer copy(CharArrayBuffer other, int markOfOther, boolean isReadOnly) { 43fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes CharArrayBuffer buf = new CharArrayBuffer(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 CharBuffer asReadOnlyBuffer() { 51fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return copy(this, mark, true); 52fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 53fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 54fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public CharBuffer 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 CharBuffer duplicate() { 66fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return copy(this, mark, isReadOnly); 67fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 68fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 69fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public CharBuffer slice() { 70fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return new CharArrayBuffer(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 char[] 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 char 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 char get(int index) { 106fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes checkIndex(index); 107fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return backingArray[arrayOffset + index]; 108fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 109eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson 110fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final CharBuffer get(char[] dst, int srcOffset, int charCount) { 111fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (charCount > remaining()) { 112fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferUnderflowException(); 113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 114fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes System.arraycopy(backingArray, arrayOffset + position, dst, srcOffset, charCount); 115fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes position += charCount; 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 } 126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 12740b4f1db4031eff92cb0b31740b6e644b8e4c733Elliott Hughes @Override public final CharBuffer subSequence(int start, int end) { 128fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes checkStartEndRemaining(start, end); 129fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes CharBuffer result = duplicate(); 130fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes result.limit(position + end); 131fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes result.position(position + start); 132fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return result; 133fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 134fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 135fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public CharBuffer put(char c) { 136fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 137fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 138fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 139fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (position == limit) { 140fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferOverflowException(); 141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 142fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes backingArray[arrayOffset + position++] = c; 143fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 144fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 146fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public CharBuffer put(int index, char c) { 147fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 148fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 149adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 150fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes checkIndex(index); 151fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes backingArray[arrayOffset + index] = c; 152fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 153fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project 155fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public CharBuffer put(char[] src, int srcOffset, int charCount) { 156fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (isReadOnly) { 157fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new ReadOnlyBufferException(); 158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project } 159fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes if (charCount > remaining()) { 160fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes throw new BufferOverflowException(); 161fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 162fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, charCount); 163fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes position += charCount; 164fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return this; 165fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 166fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes 167fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes @Override public final String toString() { 168fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes return String.copyValueOf(backingArray, arrayOffset + position, remaining()); 169fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes } 170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project} 171