KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > StringCharBuffer


1 /*
2  * @(#)StringCharBuffer.java 1.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio;
9
10
11 // ## If the sequence is a string, use reflection to share its array
12

13 class StringCharBuffer // package-private
14
extends CharBuffer JavaDoc
15 {
16     CharSequence JavaDoc str;
17
18     StringCharBuffer(CharSequence JavaDoc s, int start, int end) { // package-private
19
super(-1, start, end, s.length());
20     int n = s.length();
21     if ((start < 0) || (start > n) || (end < start) || (end > n))
22         throw new IndexOutOfBoundsException JavaDoc();
23     str = s;
24     }
25
26     public CharBuffer JavaDoc slice() {
27     return new StringCharBuffer JavaDoc(str, position(), limit());
28     }
29
30
31     private StringCharBuffer(CharSequence JavaDoc s, int mark,
32                  int pos, int limit, int cap)
33     {
34     super(mark, pos, limit, cap);
35     str = s;
36     }
37
38     public CharBuffer JavaDoc duplicate() {
39     return new StringCharBuffer JavaDoc(str, markValue(),
40                     position(), limit(), capacity());
41     }
42
43     public CharBuffer JavaDoc asReadOnlyBuffer() {
44     return duplicate();
45     }
46
47     public final char get() {
48     return str.charAt(nextGetIndex());
49     }
50
51     public final char get(int index) {
52     return str.charAt(checkIndex(index));
53     }
54
55     // ## Override bulk get methods for better performance
56

57     public final CharBuffer JavaDoc put(char c) {
58     throw new ReadOnlyBufferException JavaDoc();
59     }
60
61     public final CharBuffer JavaDoc put(int index, char c) {
62     throw new ReadOnlyBufferException JavaDoc();
63     }
64
65     public final CharBuffer JavaDoc compact() {
66     throw new ReadOnlyBufferException JavaDoc();
67     }
68
69     public final boolean isReadOnly() {
70     return true;
71     }
72
73     final String JavaDoc toString(int start, int end) {
74     return str.toString().substring(start, end);
75     }
76
77     public final CharSequence JavaDoc subSequence(int start, int end) {
78     try {
79         int pos = position();
80         return new StringCharBuffer JavaDoc(str,
81                     pos + checkIndex(start, pos),
82                     pos + checkIndex(end, pos));
83     } catch (IllegalArgumentException JavaDoc x) {
84         throw new IndexOutOfBoundsException JavaDoc();
85     }
86     }
87
88     public boolean isDirect() {
89     return false;
90     }
91
92     public ByteOrder JavaDoc order() {
93     return ByteOrder.nativeOrder();
94     }
95
96 }
97
Popular Tags