author | darcy |
Fri, 19 Aug 2011 17:42:24 -0700 | |
changeset 10350 | 6d009f117062 |
parent 7668 | d4a77089c587 |
child 17922 | d56eec572de5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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(), |
|
7275
608d2708ab12
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offser
alanb
parents:
5506
diff
changeset
|
50 |
offset + this.position()); |
2 | 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 |
public CharBuffer duplicate() { |
|
64 |
return new StringCharBuffer(str, markValue(), |
|
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
65 |
position(), limit(), capacity(), offset); |
2 | 66 |
} |
67 |
||
68 |
public CharBuffer asReadOnlyBuffer() { |
|
69 |
return duplicate(); |
|
70 |
} |
|
71 |
||
72 |
public final char get() { |
|
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
73 |
return str.charAt(nextGetIndex() + offset); |
2 | 74 |
} |
75 |
||
76 |
public final char get(int index) { |
|
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
77 |
return str.charAt(checkIndex(index) + offset); |
2 | 78 |
} |
79 |
||
80 |
// ## Override bulk get methods for better performance |
|
81 |
||
82 |
public final CharBuffer put(char c) { |
|
83 |
throw new ReadOnlyBufferException(); |
|
84 |
} |
|
85 |
||
86 |
public final CharBuffer put(int index, char c) { |
|
87 |
throw new ReadOnlyBufferException(); |
|
88 |
} |
|
89 |
||
90 |
public final CharBuffer compact() { |
|
91 |
throw new ReadOnlyBufferException(); |
|
92 |
} |
|
93 |
||
94 |
public final boolean isReadOnly() { |
|
95 |
return true; |
|
96 |
} |
|
97 |
||
98 |
final String toString(int start, int end) { |
|
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
99 |
return str.toString().substring(start + offset, end + offset); |
2 | 100 |
} |
101 |
||
1224
8c1bc7c5dd00
6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents:
715
diff
changeset
|
102 |
public final CharBuffer subSequence(int start, int end) { |
2 | 103 |
try { |
104 |
int pos = position(); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1224
diff
changeset
|
105 |
return new StringCharBuffer(str, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1224
diff
changeset
|
106 |
-1, |
2 | 107 |
pos + checkIndex(start, pos), |
69
c7cd7bde95f3
6546113: (bf) CharSequence.slice() on wrapped CharSequence doesn't start at buffer position
alanb
parents:
2
diff
changeset
|
108 |
pos + checkIndex(end, pos), |
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1224
diff
changeset
|
109 |
capacity(), |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1224
diff
changeset
|
110 |
offset); |
2 | 111 |
} catch (IllegalArgumentException x) { |
112 |
throw new IndexOutOfBoundsException(); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
public boolean isDirect() { |
|
117 |
return false; |
|
118 |
} |
|
119 |
||
120 |
public ByteOrder order() { |
|
121 |
return ByteOrder.nativeOrder(); |
|
122 |
} |
|
123 |
||
124 |
} |