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
20/**
21 * CharArrayBuffer implements char[]-based CharBuffers.
22 */
23final class CharArrayBuffer extends CharBuffer {
24
25  private final char[] backingArray;
26
27  private final int arrayOffset;
28
29  private final boolean isReadOnly;
30
31  CharArrayBuffer(char[] array) {
32    this(array.length, array, 0, false);
33  }
34
35  private CharArrayBuffer(int capacity, char[] backingArray, int arrayOffset, boolean isReadOnly) {
36    super(capacity);
37    this.backingArray = backingArray;
38    this.arrayOffset = arrayOffset;
39    this.isReadOnly = isReadOnly;
40  }
41
42  private static CharArrayBuffer copy(CharArrayBuffer other, int markOfOther, boolean isReadOnly) {
43    CharArrayBuffer buf = new CharArrayBuffer(other.capacity(), other.backingArray, other.arrayOffset, isReadOnly);
44    buf.limit = other.limit;
45    buf.position = other.position();
46    buf.mark = markOfOther;
47    return buf;
48  }
49
50  @Override public CharBuffer asReadOnlyBuffer() {
51    return copy(this, mark, true);
52  }
53
54  @Override public CharBuffer compact() {
55    if (isReadOnly) {
56      throw new ReadOnlyBufferException();
57    }
58    System.arraycopy(backingArray, position + arrayOffset, backingArray, arrayOffset, remaining());
59    position = limit - position;
60    limit = capacity;
61    mark = UNSET_MARK;
62    return this;
63  }
64
65  @Override public CharBuffer duplicate() {
66    return copy(this, mark, isReadOnly);
67  }
68
69  @Override public CharBuffer slice() {
70    return new CharArrayBuffer(remaining(), backingArray, arrayOffset + position, isReadOnly);
71  }
72
73  @Override public boolean isReadOnly() {
74    return isReadOnly;
75  }
76
77  @Override char[] protectedArray() {
78    if (isReadOnly) {
79      throw new ReadOnlyBufferException();
80    }
81    return backingArray;
82  }
83
84  @Override int protectedArrayOffset() {
85    if (isReadOnly) {
86      throw new ReadOnlyBufferException();
87    }
88    return arrayOffset;
89  }
90
91  @Override boolean protectedHasArray() {
92    if (isReadOnly) {
93      return false;
94    }
95    return true;
96  }
97
98  @Override public final char get() {
99    if (position == limit) {
100      throw new BufferUnderflowException();
101    }
102    return backingArray[arrayOffset + position++];
103  }
104
105  @Override public final char get(int index) {
106    checkIndex(index);
107    return backingArray[arrayOffset + index];
108  }
109
110  @Override public final CharBuffer get(char[] dst, int srcOffset, int charCount) {
111    if (charCount > remaining()) {
112      throw new BufferUnderflowException();
113    }
114    System.arraycopy(backingArray, arrayOffset + position, dst, srcOffset, charCount);
115    position += charCount;
116    return this;
117  }
118
119  @Override public final boolean isDirect() {
120    return false;
121  }
122
123  @Override public final ByteOrder order() {
124    return ByteOrder.nativeOrder();
125  }
126
127  @Override public final CharBuffer subSequence(int start, int end) {
128    checkStartEndRemaining(start, end);
129    CharBuffer result = duplicate();
130    result.limit(position + end);
131    result.position(position + start);
132    return result;
133  }
134
135  @Override public CharBuffer put(char c) {
136    if (isReadOnly) {
137      throw new ReadOnlyBufferException();
138    }
139    if (position == limit) {
140      throw new BufferOverflowException();
141    }
142    backingArray[arrayOffset + position++] = c;
143    return this;
144  }
145
146  @Override public CharBuffer put(int index, char c) {
147    if (isReadOnly) {
148      throw new ReadOnlyBufferException();
149    }
150    checkIndex(index);
151    backingArray[arrayOffset + index] = c;
152    return this;
153  }
154
155  @Override public CharBuffer put(char[] src, int srcOffset, int charCount) {
156    if (isReadOnly) {
157      throw new ReadOnlyBufferException();
158    }
159    if (charCount > remaining()) {
160      throw new BufferOverflowException();
161    }
162    System.arraycopy(src, srcOffset, backingArray, arrayOffset + position, charCount);
163    position += charCount;
164    return this;
165  }
166
167  @Override public final String toString() {
168    return String.copyValueOf(backingArray, arrayOffset + position, remaining());
169  }
170}
171