libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / nio / CharBuffer.java
blob969b5bb3df34ec0b878f1dd629af46ff40c185c4
1 /* CharBuffer.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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.
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. */
39 package java.nio;
41 // GCJ LOCAL: Use RawData instead of gnu.classpath.Pointer
42 import gnu.gcj.RawData;
44 import java.io.IOException;
46 /**
47 * @since 1.4
49 public abstract class CharBuffer extends Buffer
50 implements Comparable<CharBuffer>, CharSequence, Readable, Appendable
52 final int array_offset;
53 final char[] backing_buffer;
55 CharBuffer (int capacity, int limit, int position, int mark,
56 RawData address, char[] backing_buffer, int array_offset)
58 super (capacity, limit, position, mark, address);
59 this.backing_buffer = backing_buffer;
60 this.array_offset = array_offset;
63 /**
64 * Allocates a new <code>CharBuffer</code> object with a given capacity.
66 public static CharBuffer allocate (int capacity)
68 return new CharBufferImpl (capacity);
71 /**
72 * Wraps a <code>char</code> array into a <code>CharBuffer</code>
73 * object.
75 * @param array the array to wrap
76 * @param offset the offset of the region in the array to wrap
77 * @param length the length of the region in the array to wrap
79 * @return a new <code>CharBuffer</code> object
81 * @exception IndexOutOfBoundsException If the preconditions on the offset
82 * and length parameters do not hold
84 public static final CharBuffer wrap(char[] array, int offset, int length)
86 return new CharBufferImpl(array, 0, array.length, offset + length, offset,
87 -1, false);
90 /**
91 * Wraps a character sequence into a <code>CharBuffer</code> object.
93 * @param seq the sequence to wrap
95 * @return a new <code>CharBuffer</code> object
97 public static final CharBuffer wrap(CharSequence seq)
99 return wrap(seq, 0, seq.length());
103 * Wraps a character sequence into a <code>CharBuffer</code> object.
105 * @param seq the sequence to wrap
106 * @param start the index of the first character to wrap
107 * @param end the index of the first character not to wrap
109 * @return a new <code>CharBuffer</code> object
111 * @exception IndexOutOfBoundsException If the preconditions on the offset
112 * and length parameters do not hold
114 public static final CharBuffer wrap(CharSequence seq, int start, int end)
116 return new CharSequenceBuffer(seq, start, end);
120 * Wraps a <code>char</code> array into a <code>CharBuffer</code>
121 * object.
123 * @param array the array to wrap
125 * @return a new <code>CharBuffer</code> object
127 public static final CharBuffer wrap(char[] array)
129 return wrap(array, 0, array.length);
133 * This method transfers <code>char</code>s from this buffer into the given
134 * destination array. Before the transfer, it checks if there are fewer than
135 * length <code>char</code>s remaining in this buffer.
137 * @param dst The destination array
138 * @param offset The offset within the array of the first <code>char</code>
139 * to be written; must be non-negative and no larger than dst.length.
140 * @param length The maximum number of bytes to be written to the given array;
141 * must be non-negative and no larger than dst.length - offset.
143 * @exception BufferUnderflowException If there are fewer than length
144 * <code>char</code>s remaining in this buffer.
145 * @exception IndexOutOfBoundsException If the preconditions on the offset
146 * and length parameters do not hold.
148 public CharBuffer get (char[] dst, int offset, int length)
150 checkArraySize(dst.length, offset, length);
151 checkForUnderflow(length);
153 for (int i = offset; i < offset + length; i++)
155 dst [i] = get ();
158 return this;
161 /** @since 1.5 */
162 public int read(CharBuffer buffer) throws IOException
164 // We want to call put(), so we don't manipulate the CharBuffer
165 // directly.
166 int rem = Math.min(buffer.remaining(), remaining());
167 char[] buf = new char[rem];
168 get(buf);
169 buffer.put(buf);
170 return rem;
174 * This method transfers <code>char</code>s from this buffer into the given
175 * destination array.
177 * @param dst The byte array to write into.
179 * @exception BufferUnderflowException If there are fewer than dst.length
180 * <code>char</code>s remaining in this buffer.
182 public CharBuffer get (char[] dst)
184 return get (dst, 0, dst.length);
188 * Writes the content of the the <code>CharBUFFER</code> src
189 * into the buffer. Before the transfer, it checks if there is fewer than
190 * <code>src.remaining()</code> space remaining in this buffer.
192 * @param src The source data.
194 * @exception BufferOverflowException If there is insufficient space in this
195 * buffer for the remaining <code>char</code>s in the source buffer.
196 * @exception IllegalArgumentException If the source buffer is this buffer.
197 * @exception ReadOnlyBufferException If this buffer is read-only.
199 public CharBuffer put (CharBuffer src)
201 if (src == this)
202 throw new IllegalArgumentException ();
204 checkForOverflow(src.remaining());
206 if (src.remaining () > 0)
208 char[] toPut = new char [src.remaining ()];
209 src.get (toPut);
210 put (toPut);
213 return this;
217 * Writes the content of the the <code>char array</code> src
218 * into the buffer. Before the transfer, it checks if there is fewer than
219 * length space remaining in this buffer.
221 * @param src The array to copy into the buffer.
222 * @param offset The offset within the array of the first byte to be read;
223 * must be non-negative and no larger than src.length.
224 * @param length The number of bytes to be read from the given array;
225 * must be non-negative and no larger than src.length - offset.
227 * @exception BufferOverflowException If there is insufficient space in this
228 * buffer for the remaining <code>char</code>s in the source array.
229 * @exception IndexOutOfBoundsException If the preconditions on the offset
230 * and length parameters do not hold
231 * @exception ReadOnlyBufferException If this buffer is read-only.
233 public CharBuffer put (char[] src, int offset, int length)
235 checkArraySize(src.length, offset, length);
236 checkForOverflow(length);
238 for (int i = offset; i < offset + length; i++)
239 put (src [i]);
241 return this;
245 * Writes the content of the the <code>char array</code> src
246 * into the buffer.
248 * @param src The array to copy into the buffer.
250 * @exception BufferOverflowException If there is insufficient space in this
251 * buffer for the remaining <code>char</code>s in the source array.
252 * @exception ReadOnlyBufferException If this buffer is read-only.
254 public final CharBuffer put (char[] src)
256 return put (src, 0, src.length);
260 * Tells whether ot not this buffer is backed by an accessible
261 * <code>char</code> array.
263 public final boolean hasArray ()
265 return (backing_buffer != null
266 && !isReadOnly ());
270 * Returns the <code>char</code> array that backs this buffer.
272 * @exception ReadOnlyBufferException If this buffer is read-only.
273 * @exception UnsupportedOperationException If this buffer is not backed
274 * by an accessible array.
276 public final char[] array ()
278 if (backing_buffer == null)
279 throw new UnsupportedOperationException ();
281 checkIfReadOnly();
283 return backing_buffer;
287 * Returns the offset within this buffer's backing array of the first element.
289 * @exception ReadOnlyBufferException If this buffer is read-only.
290 * @exception UnsupportedOperationException If this buffer is not backed
291 * by an accessible array.
293 public final int arrayOffset ()
295 if (backing_buffer == null)
296 throw new UnsupportedOperationException ();
298 checkIfReadOnly();
300 return array_offset;
304 * Calculates a hash code for this buffer.
306 * This is done with int arithmetic,
307 * where ** represents exponentiation, by this formula:<br>
308 * <code>s[position()] + 31 + (s[position()+1] + 30)*31**1 + ... +
309 * (s[limit()-1]+30)*31**(limit()-1)</code>.
310 * Where s is the buffer data. Note that the hashcode is dependent
311 * on buffer content, and therefore is not useful if the buffer
312 * content may change.
314 public int hashCode ()
316 int hashCode = get(position()) + 31;
317 int multiplier = 1;
318 for (int i = position() + 1; i < limit(); ++i)
320 multiplier *= 31;
321 hashCode += (get(i) + 30)*multiplier;
323 return hashCode;
327 * Checks if this buffer is equal to obj.
329 public boolean equals (Object obj)
331 if (obj instanceof CharBuffer)
333 return compareTo ((CharBuffer) obj) == 0;
336 return false;
340 * Compares two <code>CharBuffer</code> objects.
342 * @exception ClassCastException If obj is not an object derived from
343 * <code>CharBuffer</code>.
345 public int compareTo (CharBuffer other)
347 int num = Math.min(remaining(), other.remaining());
348 int pos_this = position();
349 int pos_other = other.position();
351 for (int count = 0; count < num; count++)
353 char a = get(pos_this++);
354 char b = other.get(pos_other++);
356 if (a == b)
357 continue;
359 if (a < b)
360 return -1;
362 return 1;
365 return remaining() - other.remaining();
369 * Returns the byte order of this buffer.
371 public abstract ByteOrder order ();
374 * Reads the <code>char</code> at this buffer's current position,
375 * and then increments the position.
377 * @exception BufferUnderflowException If there are no remaining
378 * <code>char</code>s in this buffer.
380 public abstract char get ();
383 * Writes the <code>char</code> at this buffer's current position,
384 * and then increments the position.
386 * @exception BufferOverflowException If there no remaining
387 * <code>char</code>s in this buffer.
388 * @exception ReadOnlyBufferException If this buffer is read-only.
390 public abstract CharBuffer put (char b);
393 * Absolute get method.
395 * @exception IndexOutOfBoundsException If index is negative or not smaller
396 * than the buffer's limit.
398 public abstract char get (int index);
401 * Absolute put method.
403 * @exception IndexOutOfBoundsException If index is negative or not smaller
404 * than the buffer's limit.
405 * @exception ReadOnlyBufferException If this buffer is read-only.
407 public abstract CharBuffer put (int index, char b);
410 * Compacts this buffer.
412 * @exception ReadOnlyBufferException If this buffer is read-only.
414 public abstract CharBuffer compact ();
417 * Tells wether or not this buffer is direct.
419 public abstract boolean isDirect ();
422 * Creates a new <code>CharBuffer</code> whose content is a shared
423 * subsequence of this buffer's content.
425 public abstract CharBuffer slice ();
428 * Creates a new <code>CharBuffer</code> that shares this buffer's
429 * content.
431 public abstract CharBuffer duplicate ();
434 * Creates a new read-only <code>CharBuffer</code> that shares this
435 * buffer's content.
437 public abstract CharBuffer asReadOnlyBuffer ();
440 * Returns the remaining content of the buffer as a string.
442 public String toString ()
444 if (hasArray ())
445 return new String (array (), position (), length ());
447 char[] buf = new char [length ()];
448 int pos = position ();
449 get (buf, 0, buf.length);
450 position (pos);
451 return new String (buf);
455 * Returns the length of the remaining chars in this buffer.
457 public final int length ()
459 return remaining ();
463 * Creates a new character buffer that represents the specified subsequence
464 * of this buffer, relative to the current position.
466 * @exception IndexOutOfBoundsException If the preconditions on start and
467 * end do not hold.
469 public abstract CharSequence subSequence (int start, int length);
472 * Relative put method.
474 * @exception BufferOverflowException If there is insufficient space in this
475 * buffer.
476 * @exception IndexOutOfBoundsException If the preconditions on the start
477 * and end parameters do not hold.
478 * @exception ReadOnlyBufferException If this buffer is read-only.
480 public CharBuffer put (String str, int start, int length)
482 return put (str.toCharArray (), start, length);
486 * Relative put method.
488 * @exception BufferOverflowException If there is insufficient space in this
489 * buffer.
490 * @exception ReadOnlyBufferException If this buffer is read-only.
492 public final CharBuffer put (String str)
494 return put (str.toCharArray (), 0, str.length ());
498 * Returns the character at <code>position() + index</code>.
500 * @exception IndexOutOfBoundsException If index is negative not smaller than
501 * <code>remaining()</code>.
503 public final char charAt (int index)
505 if (index < 0
506 || index >= remaining ())
507 throw new IndexOutOfBoundsException ();
509 return get (position () + index);
512 /** @since 1.5 */
513 public CharBuffer append(char c)
515 put(c);
516 return this;
519 /** @since 1.5 */
520 public CharBuffer append(CharSequence cs)
522 put(cs == null ? "null" : cs.toString());
523 return this;
526 /** @since 1.5 */
527 public CharBuffer append(CharSequence cs, int start, int end)
529 put(cs == null ? "null" : cs.subSequence(start, end).toString());
530 return this;