1 /*
2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 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.
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     CharSequence str;
34 
StringCharBuffer(CharSequence s, int start, int end)35     StringCharBuffer(CharSequence s, int start, int end) { // package-private
36         super(-1, start, end, s.length());
37         int n = s.length();
38         if ((start < 0) || (start > n) || (end < start) || (end > n))
39             throw new IndexOutOfBoundsException();
40         str = s;
41     }
42 
slice()43     public CharBuffer slice() {
44         return new StringCharBuffer(str,
45                 -1,
46                 0,
47                 this.remaining(),
48                 this.remaining(),
49                 offset + this.position());
50     }
51 
StringCharBuffer(CharSequence s, int mark, int pos, int limit, int cap, int offset)52     private StringCharBuffer(CharSequence s,
53                              int mark,
54                              int pos,
55                              int limit,
56                              int cap,
57                              int offset) {
58         super(mark, pos, limit, cap, null, offset);
59         str = s;
60     }
61 
duplicate()62     public CharBuffer duplicate() {
63         return new StringCharBuffer(str, markValue(),
64                 position(), limit(), capacity(), offset);
65     }
66 
asReadOnlyBuffer()67     public CharBuffer asReadOnlyBuffer() {
68         return duplicate();
69     }
70 
get()71     public final char get() {
72         return str.charAt(nextGetIndex() + offset);
73     }
74 
get(int index)75     public final char get(int index) {
76         return str.charAt(checkIndex(index) + offset);
77     }
78 
getUnchecked(int index)79     char getUnchecked(int index) {
80         return str.charAt(index + offset);
81     }
82 
83     // ## Override bulk get methods for better performance
84 
put(char c)85     public final CharBuffer put(char c) {
86         throw new ReadOnlyBufferException();
87     }
88 
put(int index, char c)89     public final CharBuffer put(int index, char c) {
90         throw new ReadOnlyBufferException();
91     }
92 
compact()93     public final CharBuffer compact() {
94         throw new ReadOnlyBufferException();
95     }
96 
isReadOnly()97     public final boolean isReadOnly() {
98         return true;
99     }
100 
toString(int start, int end)101     final String toString(int start, int end) {
102         return str.toString().substring(start + offset, end + offset);
103     }
104 
subSequence(int start, int end)105     public final CharBuffer subSequence(int start, int end) {
106         try {
107             int pos = position();
108             return new StringCharBuffer(str,
109                     -1,
110                     pos + checkIndex(start, pos),
111                     pos + checkIndex(end, pos),
112                     capacity(),
113                     offset);
114         } catch (IllegalArgumentException x) {
115             throw new IndexOutOfBoundsException();
116         }
117     }
118 
isDirect()119     public boolean isDirect() {
120         return false;
121     }
122 
order()123     public ByteOrder order() {
124         return ByteOrder.nativeOrder();
125     }
126 
127 }
128