001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.nio;
019:
020: /**
021: * CharArrayBuffer, ReadWriteCharArrayBuffer and ReadOnlyCharArrayBuffer compose
022: * the implementation of array based char buffers.
023: * <p>
024: * CharArrayBuffer implements all the shared readonly methods and is extended by
025: * the other two classes.
026: * </p>
027: * <p>
028: * All methods are marked final for runtime performance.
029: * </p>
030: *
031: */
032: abstract class CharArrayBuffer extends CharBuffer {
033:
034: protected final char[] backingArray;
035:
036: protected final int offset;
037:
038: CharArrayBuffer(char[] array) {
039: this (array.length, array, 0);
040: }
041:
042: CharArrayBuffer(int capacity) {
043: this (capacity, new char[capacity], 0);
044: }
045:
046: CharArrayBuffer(int capacity, char[] backingArray, int offset) {
047: super (capacity);
048: this .backingArray = backingArray;
049: this .offset = offset;
050: }
051:
052: @Override
053: public final char get() {
054: if (position == limit) {
055: throw new BufferUnderflowException();
056: }
057: return backingArray[offset + position++];
058: }
059:
060: @Override
061: public final char get(int index) {
062: if (index < 0 || index >= limit) {
063: throw new IndexOutOfBoundsException();
064: }
065: return backingArray[offset + index];
066: }
067:
068: @Override
069: public final CharBuffer get(char[] dest, int off, int len) {
070: int length = dest.length;
071: if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
072: throw new IndexOutOfBoundsException();
073: }
074: if (len > remaining()) {
075: throw new BufferUnderflowException();
076: }
077: System.arraycopy(backingArray, offset + position, dest, off,
078: len);
079: position += len;
080: return this ;
081: }
082:
083: @Override
084: public final boolean isDirect() {
085: return false;
086: }
087:
088: @Override
089: public final ByteOrder order() {
090: return ByteOrder.nativeOrder();
091: }
092:
093: @Override
094: public final CharSequence subSequence(int start, int end) {
095: if (start < 0 || end < start || end > remaining()) {
096: throw new IndexOutOfBoundsException();
097: }
098:
099: CharBuffer result = duplicate();
100: result.limit(position + end);
101: result.position(position + start);
102: return result;
103: }
104:
105: @Override
106: public final String toString() {
107: return String.copyValueOf(backingArray, offset + position,
108: remaining());
109: }
110: }
|