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: * This class wraps a char sequence to be a char buffer.
022: * <p>
023: * Implementation notice:
024: * <ul>
025: * <li>Char sequence based buffer is always readonly.</li>
026: * </ul>
027: * </p>
028: *
029: */
030: final class CharSequenceAdapter extends CharBuffer {
031:
032: static CharSequenceAdapter copy(CharSequenceAdapter other) {
033: CharSequenceAdapter buf = new CharSequenceAdapter(
034: other.sequence);
035: buf.limit = other.limit;
036: buf.position = other.position;
037: buf.mark = other.mark;
038: return buf;
039: }
040:
041: final CharSequence sequence;
042:
043: CharSequenceAdapter(CharSequence chseq) {
044: super (chseq.length());
045: sequence = chseq;
046: }
047:
048: @Override
049: public CharBuffer asReadOnlyBuffer() {
050: return duplicate();
051: }
052:
053: @Override
054: public CharBuffer compact() {
055: throw new ReadOnlyBufferException();
056: }
057:
058: @Override
059: public CharBuffer duplicate() {
060: return copy(this );
061: }
062:
063: @Override
064: public char get() {
065: if (position == limit) {
066: throw new BufferUnderflowException();
067: }
068: return sequence.charAt(position++);
069: }
070:
071: @Override
072: public char get(int index) {
073: if (index < 0 || index >= limit) {
074: throw new IndexOutOfBoundsException();
075: }
076: return sequence.charAt(index);
077: }
078:
079: @Override
080: public final CharBuffer get(char[] dest, int off, int len) {
081: int length = dest.length;
082: if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
083: throw new IndexOutOfBoundsException();
084: }
085: if (len > remaining()) {
086: throw new BufferUnderflowException();
087: }
088: int newPosition = position + len;
089: sequence.toString().getChars(position, newPosition, dest, off);
090: position = newPosition;
091: return this ;
092: }
093:
094: @Override
095: public boolean isDirect() {
096: return false;
097: }
098:
099: @Override
100: public boolean isReadOnly() {
101: return true;
102: }
103:
104: @Override
105: public ByteOrder order() {
106: return ByteOrder.nativeOrder();
107: }
108:
109: @Override
110: protected char[] protectedArray() {
111: throw new UnsupportedOperationException();
112: }
113:
114: @Override
115: protected int protectedArrayOffset() {
116: throw new UnsupportedOperationException();
117: }
118:
119: @Override
120: protected boolean protectedHasArray() {
121: return false;
122: }
123:
124: @Override
125: public CharBuffer put(char c) {
126: throw new ReadOnlyBufferException();
127: }
128:
129: @Override
130: public CharBuffer put(int index, char c) {
131: throw new ReadOnlyBufferException();
132: }
133:
134: @Override
135: public final CharBuffer put(char[] src, int off, int len) {
136: if ((off < 0) || (len < 0)
137: || (long) off + (long) len > src.length) {
138: throw new IndexOutOfBoundsException();
139: }
140:
141: if (len > remaining()) {
142: throw new BufferOverflowException();
143: }
144:
145: throw new ReadOnlyBufferException();
146: }
147:
148: @Override
149: public CharBuffer put(String src, int start, int end) {
150: if ((start < 0) || (end < 0)
151: || (long) start + (long) end > src.length()) {
152: throw new IndexOutOfBoundsException();
153: }
154: throw new ReadOnlyBufferException();
155: }
156:
157: @Override
158: public CharBuffer slice() {
159: return new CharSequenceAdapter(sequence.subSequence(position,
160: limit));
161: }
162:
163: @Override
164: public CharSequence subSequence(int start, int end) {
165: if (end < start || start < 0 || end > remaining()) {
166: throw new IndexOutOfBoundsException();
167: }
168:
169: CharSequenceAdapter result = copy(this);
170: result.position = position + start;
171: result.limit = position + end;
172: return result;
173: }
174: }
|