2
|
1 |
/*
|
|
2 |
* Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.nio;
|
|
27 |
|
|
28 |
|
|
29 |
// ## If the sequence is a string, use reflection to share its array
|
|
30 |
|
|
31 |
class StringCharBuffer // package-private
|
|
32 |
extends CharBuffer
|
|
33 |
{
|
|
34 |
CharSequence str;
|
|
35 |
|
|
36 |
StringCharBuffer(CharSequence s, int start, int end) { // package-private
|
|
37 |
super(-1, start, end, s.length());
|
|
38 |
int n = s.length();
|
|
39 |
if ((start < 0) || (start > n) || (end < start) || (end > n))
|
|
40 |
throw new IndexOutOfBoundsException();
|
|
41 |
str = s;
|
|
42 |
}
|
|
43 |
|
|
44 |
public CharBuffer slice() {
|
|
45 |
return new StringCharBuffer(str,
|
|
46 |
-1,
|
|
47 |
0,
|
|
48 |
this.remaining(),
|
|
49 |
this.remaining(),
|
|
50 |
this.position());
|
|
51 |
}
|
|
52 |
|
|
53 |
private StringCharBuffer(CharSequence s,
|
|
54 |
int mark,
|
|
55 |
int pos,
|
|
56 |
int limit,
|
|
57 |
int cap,
|
|
58 |
int offset) {
|
|
59 |
super(mark, pos, limit, cap, null, offset);
|
|
60 |
str = s;
|
|
61 |
}
|
|
62 |
|
|
63 |
private StringCharBuffer(CharSequence s, int mark,
|
|
64 |
int pos, int limit, int cap)
|
|
65 |
{
|
|
66 |
super(mark, pos, limit, cap);
|
|
67 |
str = s;
|
|
68 |
}
|
|
69 |
|
|
70 |
public CharBuffer duplicate() {
|
|
71 |
return new StringCharBuffer(str, markValue(),
|
|
72 |
position(), limit(), capacity());
|
|
73 |
}
|
|
74 |
|
|
75 |
public CharBuffer asReadOnlyBuffer() {
|
|
76 |
return duplicate();
|
|
77 |
}
|
|
78 |
|
|
79 |
public final char get() {
|
|
80 |
return str.charAt(nextGetIndex());
|
|
81 |
}
|
|
82 |
|
|
83 |
public final char get(int index) {
|
|
84 |
return str.charAt(checkIndex(index));
|
|
85 |
}
|
|
86 |
|
|
87 |
// ## Override bulk get methods for better performance
|
|
88 |
|
|
89 |
public final CharBuffer put(char c) {
|
|
90 |
throw new ReadOnlyBufferException();
|
|
91 |
}
|
|
92 |
|
|
93 |
public final CharBuffer put(int index, char c) {
|
|
94 |
throw new ReadOnlyBufferException();
|
|
95 |
}
|
|
96 |
|
|
97 |
public final CharBuffer compact() {
|
|
98 |
throw new ReadOnlyBufferException();
|
|
99 |
}
|
|
100 |
|
|
101 |
public final boolean isReadOnly() {
|
|
102 |
return true;
|
|
103 |
}
|
|
104 |
|
|
105 |
final String toString(int start, int end) {
|
|
106 |
return str.toString().substring(start, end);
|
|
107 |
}
|
|
108 |
|
|
109 |
public final CharSequence subSequence(int start, int end) {
|
|
110 |
try {
|
|
111 |
int pos = position();
|
|
112 |
return new StringCharBuffer(str,
|
|
113 |
pos + checkIndex(start, pos),
|
|
114 |
pos + checkIndex(end, pos));
|
|
115 |
} catch (IllegalArgumentException x) {
|
|
116 |
throw new IndexOutOfBoundsException();
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
public boolean isDirect() {
|
|
121 |
return false;
|
|
122 |
}
|
|
123 |
|
|
124 |
public ByteOrder order() {
|
|
125 |
return ByteOrder.nativeOrder();
|
|
126 |
}
|
|
127 |
|
|
128 |
}
|