1
25
26 package java.nio;
27
28
29
30
31 class StringCharBuffer
32 extends CharBuffer
33 {
34 CharSequence str;
35
36 StringCharBuffer(CharSequence s, int start, int end) {
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 this.isReadOnly = true;
43 }
44
45 public CharBuffer slice() {
46 return new StringCharBuffer(str,
47 -1,
48 0,
49 this.remaining(),
50 this.remaining(),
51 offset + this.position());
52 }
53
54 private StringCharBuffer(CharSequence s,
55 int mark,
56 int pos,
57 int limit,
58 int cap,
59 int offset) {
60 super(mark, pos, limit, cap, null, offset);
61 str = s;
62 this.isReadOnly = true;
63 }
64
65 public CharBuffer duplicate() {
66 return new StringCharBuffer(str, markValue(),
67 position(), limit(), capacity(), offset);
68 }
69
70 public CharBuffer asReadOnlyBuffer() {
71 return duplicate();
72 }
73
74 public final char get() {
75 return str.charAt(nextGetIndex() + offset);
76 }
77
78 public final char get(int index) {
79 return str.charAt(checkIndex(index) + offset);
80 }
81
82 char getUnchecked(int index) {
83 return str.charAt(index + offset);
84 }
85
86
87
88 public final CharBuffer put(char c) {
89 throw new ReadOnlyBufferException();
90 }
91
92 public final CharBuffer put(int index, char c) {
93 throw new ReadOnlyBufferException();
94 }
95
96 public final CharBuffer compact() {
97 throw new ReadOnlyBufferException();
98 }
99
100 public final boolean isReadOnly() {
101 return true;
102 }
103
104 final String toString(int start, int end) {
105 return str.subSequence(start + offset, end + offset).toString();
106 }
107
108 public final CharBuffer subSequence(int start, int end) {
109 try {
110 int pos = position();
111 return new StringCharBuffer(str,
112 -1,
113 pos + checkIndex(start, pos),
114 pos + checkIndex(end, pos),
115 capacity(),
116 offset);
117 } catch (IllegalArgumentException x) {
118 throw new IndexOutOfBoundsException();
119 }
120 }
121
122 public boolean isDirect() {
123 return false;
124 }
125
126 public ByteOrder order() {
127 return ByteOrder.nativeOrder();
128 }
129
130 ByteOrder charRegionOrder() {
131 return null;
132 }
133
134 public boolean equals(Object ob) {
135 if (this == ob)
136 return true;
137 if (!(ob instanceof CharBuffer))
138 return false;
139 CharBuffer that = (CharBuffer)ob;
140 if (this.remaining() != that.remaining())
141 return false;
142 return BufferMismatch.mismatch(this, this.position(),
143 that, that.position(),
144 this.remaining()) < 0;
145 }
146
147 public int compareTo(CharBuffer that) {
148 int i = BufferMismatch.mismatch(this, this.position(),
149 that, that.position(),
150 Math.min(this.remaining(), that.remaining()));
151 if (i >= 0) {
152 return Character.compare(this.get(this.position() + i), that.get(that.position() + i));
153 }
154 return this.remaining() - that.remaining();
155 }
156 }
157