| 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);
|
|---|
|
|---|