1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package java.nio;
19
20import java.util.Arrays;
21
22/**
23 * This class wraps a char sequence to be a char buffer.
24 * <p>
25 * Implementation notice:
26 * <ul>
27 * <li>Char sequence based buffer is always readonly.</li>
28 * </ul>
29 * </p>
30 *
31 */
32final class CharSequenceAdapter extends CharBuffer {
33
34    static CharSequenceAdapter copy(CharSequenceAdapter other) {
35        CharSequenceAdapter buf = new CharSequenceAdapter(other.sequence);
36        buf.limit = other.limit;
37        buf.position = other.position;
38        buf.mark = other.mark;
39        return buf;
40    }
41
42    final CharSequence sequence;
43
44    CharSequenceAdapter(CharSequence chseq) {
45        super(chseq.length(), 0);
46        sequence = chseq;
47    }
48
49    @Override
50    public CharBuffer asReadOnlyBuffer() {
51        return duplicate();
52    }
53
54    @Override
55    public CharBuffer compact() {
56        throw new ReadOnlyBufferException();
57    }
58
59    @Override
60    public CharBuffer duplicate() {
61        return copy(this);
62    }
63
64    @Override
65    public char get() {
66        if (position == limit) {
67            throw new BufferUnderflowException();
68        }
69        return sequence.charAt(position++);
70    }
71
72    @Override
73    public char get(int index) {
74        checkIndex(index);
75        return sequence.charAt(index);
76    }
77
78    @Override
79    public final CharBuffer get(char[] dst, int dstOffset, int charCount) {
80        Arrays.checkOffsetAndCount(dst.length, dstOffset, charCount);
81        if (charCount > remaining()) {
82            throw new BufferUnderflowException();
83        }
84        int newPosition = position + charCount;
85        sequence.toString().getChars(position, newPosition, dst, dstOffset);
86        position = newPosition;
87        return this;
88    }
89
90    @Override
91    public boolean isDirect() {
92        return false;
93    }
94
95    @Override
96    public boolean isReadOnly() {
97        return true;
98    }
99
100    @Override
101    public ByteOrder order() {
102        return ByteOrder.nativeOrder();
103    }
104
105    @Override char[] protectedArray() {
106        throw new UnsupportedOperationException();
107    }
108
109    @Override int protectedArrayOffset() {
110        throw new UnsupportedOperationException();
111    }
112
113    @Override boolean protectedHasArray() {
114        return false;
115    }
116
117    @Override
118    public CharBuffer put(char c) {
119        throw new ReadOnlyBufferException();
120    }
121
122    @Override
123    public CharBuffer put(int index, char c) {
124        throw new ReadOnlyBufferException();
125    }
126
127    @Override
128    public final CharBuffer put(char[] src, int srcOffset, int charCount) {
129        throw new ReadOnlyBufferException();
130    }
131
132    @Override
133    public CharBuffer put(String src, int start, int end) {
134        throw new ReadOnlyBufferException();
135    }
136
137    @Override
138    public CharBuffer slice() {
139        return new CharSequenceAdapter(sequence.subSequence(position, limit));
140    }
141
142    @Override public CharBuffer subSequence(int start, int end) {
143        checkStartEndRemaining(start, end);
144        CharSequenceAdapter result = copy(this);
145        result.position = position + start;
146        result.limit = position + end;
147        return result;
148    }
149}
150