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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * This class wraps a char sequence to be a char buffer.
23 * Implementation notice:
25 * <li>Char sequence based buffer is always readonly.</li>
30 final class CharSequenceAdapter extends CharBuffer {
32 static CharSequenceAdapter copy(CharSequenceAdapter other) {
33 CharSequenceAdapter buf = new CharSequenceAdapter(other.sequence);
34 buf.limit = other.limit;
35 buf.position = other.position;
36 buf.mark = other.mark;
40 final CharSequence sequence;
42 CharSequenceAdapter(CharSequence chseq) {
43 super(chseq.length());
48 public CharBuffer asReadOnlyBuffer() {
53 public CharBuffer compact() {
54 throw new ReadOnlyBufferException();
58 public CharBuffer duplicate() {
64 if (position == limit) {
65 throw new BufferUnderflowException();
67 return sequence.charAt(position++);
71 public char get(int index) {
72 if (index < 0 || index >= limit) {
73 throw new IndexOutOfBoundsException();
75 return sequence.charAt(index);
79 public final CharBuffer get(char[] dst, int off, int len) {
80 int length = dst.length;
81 if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
82 throw new IndexOutOfBoundsException();
84 if (len > remaining()) {
85 throw new BufferUnderflowException();
87 int newPosition = position + len;
88 sequence.toString().getChars(position, newPosition, dst, off);
89 position = newPosition;
94 public boolean isDirect() {
99 public boolean isReadOnly() {
104 public ByteOrder order() {
105 return ByteOrder.nativeOrder();
109 protected char[] protectedArray() {
110 throw new UnsupportedOperationException();
114 protected int protectedArrayOffset() {
115 throw new UnsupportedOperationException();
119 protected boolean protectedHasArray() {
124 public CharBuffer put(char c) {
125 throw new ReadOnlyBufferException();
129 public CharBuffer put(int index, char c) {
130 throw new ReadOnlyBufferException();
134 public final CharBuffer put(char[] src, int off, int len) {
135 if ((off < 0) || (len < 0) || (long) off + (long) len > src.length) {
136 throw new IndexOutOfBoundsException();
139 if (len > remaining()) {
140 throw new BufferOverflowException();
143 throw new ReadOnlyBufferException();
147 public CharBuffer put(String src, int start, int end) {
148 if ((start < 0) || (end < 0)
149 || (long) start + (long) end > src.length()) {
150 throw new IndexOutOfBoundsException();
152 throw new ReadOnlyBufferException();
156 public CharBuffer slice() {
157 return new CharSequenceAdapter(sequence.subSequence(position, limit));
161 public CharSequence subSequence(int start, int end) {
162 if (end < start || start < 0 || end > remaining()) {
163 throw new IndexOutOfBoundsException();
166 CharSequenceAdapter result = copy(this);
167 result.position = position + start;
168 result.limit = position + end;