1 | /* CharBuffer.java --
|
---|
2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.nio;
|
---|
39 |
|
---|
40 | import gnu.java.nio.CharBufferImpl;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @since 1.4
|
---|
44 | */
|
---|
45 | public abstract class CharBuffer extends Buffer
|
---|
46 | implements Comparable, CharSequence
|
---|
47 | {
|
---|
48 | protected char [] backing_buffer;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Allocates a new <code>CharBuffer</code> object with a given capacity.
|
---|
52 | */
|
---|
53 | public static CharBuffer allocate (int capacity)
|
---|
54 | {
|
---|
55 | return new CharBufferImpl (capacity, 0, capacity);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Wraps a character array into a <code>CharBuffer</code> object.
|
---|
60 | *
|
---|
61 | * @exception IndexOutOfBoundsException If the preconditions on the offset
|
---|
62 | * and length parameters do not hold
|
---|
63 | */
|
---|
64 | final public static CharBuffer wrap (char[] array, int offset, int length)
|
---|
65 | {
|
---|
66 | return new CharBufferImpl (array, offset, length);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Wraps a character sequence into a <code>CharBuffer</code> object.
|
---|
71 | */
|
---|
72 | final public static CharBuffer wrap (CharSequence a)
|
---|
73 | {
|
---|
74 | return wrap (a, 0, a.length ());
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Wraps a character sequence into a <code>CharBuffer</code> object.
|
---|
79 | *
|
---|
80 | * @exception IndexOutOfBoundsException If the preconditions on the offset
|
---|
81 | * and length parameters do not hold
|
---|
82 | */
|
---|
83 | final public static CharBuffer wrap (CharSequence a, int offset, int length)
|
---|
84 | {
|
---|
85 | if ((offset < 0)
|
---|
86 | || (offset > a.length ())
|
---|
87 | || (length < 0)
|
---|
88 | || (length > (a.length () - offset)))
|
---|
89 | throw new IndexOutOfBoundsException ();
|
---|
90 |
|
---|
91 | char [] buffer = new char [a.length ()];
|
---|
92 |
|
---|
93 | for (int i = offset; i < length; i++)
|
---|
94 | {
|
---|
95 | buffer [i] = a.charAt (i);
|
---|
96 | }
|
---|
97 |
|
---|
98 | return wrap (buffer, offset, length);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Wraps a character array into a <code>CharBuffer</code> object.
|
---|
103 | */
|
---|
104 | final public static CharBuffer wrap (char[] array)
|
---|
105 | {
|
---|
106 | return wrap (array, 0, array.length);
|
---|
107 | }
|
---|
108 |
|
---|
109 | CharBuffer (int cap, int lim, int pos, int mark)
|
---|
110 | {
|
---|
111 | super (cap, lim, pos, mark);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Relative get method.
|
---|
116 | *
|
---|
117 | * @exception BufferUnderflowException If the buffer's current position is
|
---|
118 | * not smaller than its limit.
|
---|
119 | * @exception IndexOutOfBoundsException If the preconditions on the offset
|
---|
120 | * and length parameters do not hold
|
---|
121 | */
|
---|
122 | public CharBuffer get (char[] dst, int offset, int length)
|
---|
123 | {
|
---|
124 | for (int i = offset; i < offset + length; i++)
|
---|
125 | {
|
---|
126 | dst [i] = get ();
|
---|
127 | }
|
---|
128 |
|
---|
129 | return this;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Relative get method.
|
---|
134 | *
|
---|
135 | * @exception BufferUnderflowException If there are fewer than length
|
---|
136 | * characters remaining in this buffer.
|
---|
137 | */
|
---|
138 | public CharBuffer get (char[] dst)
|
---|
139 | {
|
---|
140 | return get (dst, 0, dst.length);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * @exception BufferOverflowException If there are fewer than length of
|
---|
145 | * source buffer characters remaining in this buffer.
|
---|
146 | * @exception IllegalArgumentException If the source buffer is this buffer.
|
---|
147 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
148 | */
|
---|
149 | public CharBuffer put (CharBuffer src)
|
---|
150 | {
|
---|
151 | if (src == this)
|
---|
152 | throw new IllegalArgumentException ();
|
---|
153 |
|
---|
154 | if (src.length () > 0)
|
---|
155 | {
|
---|
156 | char [] toPut = new char [src.length ()];
|
---|
157 | src.get (toPut);
|
---|
158 | src.put (toPut);
|
---|
159 | }
|
---|
160 |
|
---|
161 | return this;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * @exception BufferOverflowException If there are fewer then length
|
---|
166 | * characters remaining in this buffer.
|
---|
167 | * @exception IndexOutOfBoundsException If the preconditions on the offset
|
---|
168 | * and length parameters do not hold
|
---|
169 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
170 | */
|
---|
171 | public CharBuffer put (char[] src, int offset, int length)
|
---|
172 | {
|
---|
173 | if (offset < 0
|
---|
174 | || offset >= src.length
|
---|
175 | || length < 0
|
---|
176 | || length >= (src.length - offset))
|
---|
177 | throw new IndexOutOfBoundsException ();
|
---|
178 |
|
---|
179 | // Put nothing into this buffer when not enough space left.
|
---|
180 | if (length > remaining ())
|
---|
181 | throw new BufferOverflowException ();
|
---|
182 |
|
---|
183 | for (int i = offset; i < offset + length; i++)
|
---|
184 | {
|
---|
185 | put (src [i]);
|
---|
186 | }
|
---|
187 |
|
---|
188 | return this;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Relative put method.
|
---|
193 | *
|
---|
194 | * @exception BufferOverflowException If there are fewer then length of the
|
---|
195 | * array characters remaining in this buffer.
|
---|
196 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
197 | */
|
---|
198 | public final CharBuffer put (char[] src)
|
---|
199 | {
|
---|
200 | return put (src, 0, src.length);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Tells wether this is buffer is backed by an array or not.
|
---|
205 | */
|
---|
206 | public final boolean hasArray ()
|
---|
207 | {
|
---|
208 | return (backing_buffer != null
|
---|
209 | && ! isReadOnly ());
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Returns the array that backs this buffer.
|
---|
214 | *
|
---|
215 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
216 | * @exception UnsupportedOperationException If this buffer is not backed
|
---|
217 | * by an accessible array.
|
---|
218 | */
|
---|
219 | public final char[] array ()
|
---|
220 | {
|
---|
221 | if (backing_buffer == null)
|
---|
222 | throw new UnsupportedOperationException ();
|
---|
223 |
|
---|
224 | if (isReadOnly ())
|
---|
225 | throw new ReadOnlyBufferException ();
|
---|
226 |
|
---|
227 | return backing_buffer;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Returns the offset to the position of a character in this buffer.
|
---|
232 | *
|
---|
233 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
234 | * @exception UnsupportedOperationException If this buffer is not backed
|
---|
235 | * by an accessible array.
|
---|
236 | */
|
---|
237 | public final int arrayOffset ()
|
---|
238 | {
|
---|
239 | if (backing_buffer == null)
|
---|
240 | throw new UnsupportedOperationException ();
|
---|
241 |
|
---|
242 | if (isReadOnly ())
|
---|
243 | throw new ReadOnlyBufferException ();
|
---|
244 |
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Calculates a hash code for this buffer-
|
---|
250 | */
|
---|
251 | public int hashCode ()
|
---|
252 | {
|
---|
253 | // FIXME: Check what SUN calculates here.
|
---|
254 | return super.hashCode ();
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Checks if this buffer is equal to obj.
|
---|
259 | */
|
---|
260 | public boolean equals (Object obj)
|
---|
261 | {
|
---|
262 | if (obj instanceof CharBuffer)
|
---|
263 | return compareTo (obj) == 0;
|
---|
264 |
|
---|
265 | return false;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Compares two character buffer objects.
|
---|
270 | *
|
---|
271 | * @exception ClassCastException If obj is not an object derived from
|
---|
272 | * <code>CharBuffer</code>.
|
---|
273 | */
|
---|
274 | public int compareTo(Object obj)
|
---|
275 | {
|
---|
276 | CharBuffer a = (CharBuffer) obj;
|
---|
277 |
|
---|
278 | if (a.remaining () != remaining ())
|
---|
279 | return 1;
|
---|
280 |
|
---|
281 | if (! hasArray () || ! a.hasArray ())
|
---|
282 | return 1;
|
---|
283 |
|
---|
284 | int r = remaining ();
|
---|
285 | int i1 = position ();
|
---|
286 | int i2 = a.position ();
|
---|
287 |
|
---|
288 | for (int i = 0; i < r; i++)
|
---|
289 | {
|
---|
290 | int t = (int) (get (i1)- a.get (i2));
|
---|
291 |
|
---|
292 | if (t != 0)
|
---|
293 | return (int) t;
|
---|
294 | }
|
---|
295 |
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Relative get method.
|
---|
301 | *
|
---|
302 | * @exception BufferUnderflowException If there are no remaining characters
|
---|
303 | * in this buffer.
|
---|
304 | */
|
---|
305 | public abstract char get ();
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Relative put method.
|
---|
309 | *
|
---|
310 | * @exception BufferOverflowException If there no remaining characters in
|
---|
311 | * this buffer.
|
---|
312 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
313 | */
|
---|
314 | public abstract CharBuffer put (char b);
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Absolute get method.
|
---|
318 | *
|
---|
319 | * @exception IndexOutOfBoundsException If index is negative or not smaller
|
---|
320 | * than the buffer's limit.
|
---|
321 | */
|
---|
322 | public abstract char get (int index);
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Absolute put method.
|
---|
326 | *
|
---|
327 | * @exception IndexOutOfBoundsException If index is negative or not smaller
|
---|
328 | * than the buffer's limit.
|
---|
329 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
330 | */
|
---|
331 | public abstract CharBuffer put (int index, char b);
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
335 | */
|
---|
336 | public abstract CharBuffer compact ();
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Tells wether this buffer is direct or not.
|
---|
340 | */
|
---|
341 | public abstract boolean isDirect ();
|
---|
342 |
|
---|
343 | public abstract CharBuffer slice ();
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Duplicates this buffer.
|
---|
347 | */
|
---|
348 | public abstract CharBuffer duplicate ();
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Returns this buffer made read-only.
|
---|
352 | */
|
---|
353 | public abstract CharBuffer asReadOnlyBuffer ();
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Returns the remaining content of the buffer as a string.
|
---|
357 | */
|
---|
358 | public String toString ()
|
---|
359 | {
|
---|
360 | return new String (array (), position (), length ());
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * Returns the length of the remaining chars in this buffer.
|
---|
365 | */
|
---|
366 | public final int length ()
|
---|
367 | {
|
---|
368 | return remaining ();
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Returns the byte order of this buffer.
|
---|
373 | */
|
---|
374 | public abstract ByteOrder order ();
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Creates a new character buffer that represents the specified subsequence
|
---|
378 | * of this buffer, relative to the current position.
|
---|
379 | *
|
---|
380 | * @exception IndexOutOfBoundsException If the preconditions on start and
|
---|
381 | * end do not hold.
|
---|
382 | */
|
---|
383 | public abstract CharSequence subSequence (int start, int length);
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Relative put method.
|
---|
387 | *
|
---|
388 | * @exception BufferOverflowException If there is insufficient space in this
|
---|
389 | * buffer.
|
---|
390 | * @exception IndexOutOfBoundsException If the preconditions on the start
|
---|
391 | * and end parameters do not hold.
|
---|
392 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
393 | */
|
---|
394 | public CharBuffer put (String str, int start, int length)
|
---|
395 | {
|
---|
396 | return put (str.toCharArray (), start, length);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Relative put method.
|
---|
401 | *
|
---|
402 | * @exception BufferOverflowException If there is insufficient space in this
|
---|
403 | * buffer.
|
---|
404 | * @exception ReadOnlyBufferException If this buffer is read-only.
|
---|
405 | */
|
---|
406 | public final CharBuffer put (String str)
|
---|
407 | {
|
---|
408 | return put (str, 0, str.length ());
|
---|
409 | }
|
---|
410 |
|
---|
411 | /**
|
---|
412 | * Returns the character at <code>position() + index</code>.
|
---|
413 | *
|
---|
414 | * @exception IndexOutOfBoundsException If index is negative not smaller than
|
---|
415 | * <code>remaining()</code>.
|
---|
416 | */
|
---|
417 | public final char charAt (int index)
|
---|
418 | {
|
---|
419 | if (index < 0
|
---|
420 | || index >= remaining ())
|
---|
421 | throw new IndexOutOfBoundsException ();
|
---|
422 |
|
---|
423 | return get (position () + index);
|
---|
424 | }
|
---|
425 | }
|
---|