| 1 | /* StringBuffer.java -- Growable strings
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001 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.lang;
|
|---|
| 39 | import java.io.Serializable;
|
|---|
| 40 |
|
|---|
| 41 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 42 | * Updated using online JDK 1.2 docs.
|
|---|
| 43 | * Believed complete and correct to JDK 1.2.
|
|---|
| 44 | * Merged with Classpath.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * <code>StringBuffer</code> represents a changeable <code>String</code>.
|
|---|
| 49 | * It provides the operations required to modify the
|
|---|
| 50 | * <code>StringBuffer</code> including insert, replace, delete, append,
|
|---|
| 51 | * and reverse.
|
|---|
| 52 | * <P>
|
|---|
| 53 | *
|
|---|
| 54 | * <code>StringBuffer</code>s are variable-length in nature, so even if
|
|---|
| 55 | * you initialize them to a certain size, they can still grow larger than
|
|---|
| 56 | * that. <EM>Capacity</EM> indicates the number of characters the
|
|---|
| 57 | * <code>StringBuffer</code> can have in it before it has to grow (growing
|
|---|
| 58 | * the char array is an expensive operation involving <code>new</code>).
|
|---|
| 59 | * <P>
|
|---|
| 60 | *
|
|---|
| 61 | * Incidentally, the String operator "+" actually is turned into a
|
|---|
| 62 | * <code>StringBuffer</code> operation:
|
|---|
| 63 | * <BR>
|
|---|
| 64 | * <code>a + b</code>
|
|---|
| 65 | * <BR>
|
|---|
| 66 | * is the same as
|
|---|
| 67 | * <BR>
|
|---|
| 68 | * <code>new StringBuffer(a).append(b).toString()</code>.
|
|---|
| 69 | *
|
|---|
| 70 | * @implnote Classpath's StringBuffer is capable of sharing memory with
|
|---|
| 71 | * Strings for efficiency. This will help in two instances:
|
|---|
| 72 | * first, when a StringBuffer is created from a String but is
|
|---|
| 73 | * never changed, and second, when a StringBuffer is converted
|
|---|
| 74 | * to a String and the StringBuffer is not changed after that.
|
|---|
| 75 | *
|
|---|
| 76 | * @since JDK1.0
|
|---|
| 77 | * @author Paul Fisher
|
|---|
| 78 | * @author John Keiser
|
|---|
| 79 | * @author Tom Tromey
|
|---|
| 80 | * @see java.lang.String
|
|---|
| 81 | */
|
|---|
| 82 | public final class StringBuffer implements Serializable, CharSequence
|
|---|
| 83 | {
|
|---|
| 84 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 85 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 86 | * <code>String</code>.
|
|---|
| 87 | * @param bool the <code>boolean</code> to convert and append.
|
|---|
| 88 | * @return this <code>StringBuffer</code>.
|
|---|
| 89 | * @see java.lang.String#valueOf(boolean)
|
|---|
| 90 | */
|
|---|
| 91 | public StringBuffer append (boolean bool)
|
|---|
| 92 | {
|
|---|
| 93 | return append (String.valueOf(bool));
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Append the <code>char</code> to this <code>StringBuffer</code>.
|
|---|
| 97 | * @param c the <code>char</code> to append.
|
|---|
| 98 | * @return this <code>StringBuffer</code>.
|
|---|
| 99 | */
|
|---|
| 100 | public synchronized StringBuffer append (char ch)
|
|---|
| 101 | {
|
|---|
| 102 | ensureCapacity_unsynchronized (count + 1);
|
|---|
| 103 | value[count++] = ch;
|
|---|
| 104 | return this;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 108 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 109 | * <code>String</code>.
|
|---|
| 110 | * @param inum the <code>int</code> to convert and append.
|
|---|
| 111 | * @return this <code>StringBuffer</code>.
|
|---|
| 112 | * @see java.lang.String#valueOf(int)
|
|---|
| 113 | */
|
|---|
| 114 | public native StringBuffer append (int inum);
|
|---|
| 115 |
|
|---|
| 116 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 117 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 118 | * <code>String</code>.
|
|---|
| 119 | * @param lnum the <code>long</code> to convert and append.
|
|---|
| 120 | * @return this <code>StringBuffer</code>.
|
|---|
| 121 | * @see java.lang.String#valueOf(long)
|
|---|
| 122 | */
|
|---|
| 123 | public StringBuffer append (long lnum)
|
|---|
| 124 | {
|
|---|
| 125 | return append (String.valueOf(lnum));
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 129 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 130 | * <code>String</code>.
|
|---|
| 131 | * @param fnum the <code>float</code> to convert and append.
|
|---|
| 132 | * @return this <code>StringBuffer</code>.
|
|---|
| 133 | * @see java.lang.String#valueOf(float)
|
|---|
| 134 | */
|
|---|
| 135 | public StringBuffer append (float fnum)
|
|---|
| 136 | {
|
|---|
| 137 | return append (String.valueOf(fnum));
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 141 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 142 | * <code>String</code>.
|
|---|
| 143 | * @param dnum the <code>double</code> to convert and append.
|
|---|
| 144 | * @return this <code>StringBuffer</code>.
|
|---|
| 145 | * @see java.lang.String#valueOf(double)
|
|---|
| 146 | */
|
|---|
| 147 | public StringBuffer append (double dnum)
|
|---|
| 148 | {
|
|---|
| 149 | return append (String.valueOf(dnum));
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
|
|---|
| 153 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 154 | * <code>String</code>.
|
|---|
| 155 | * @param obj the <code>Object</code> to convert and append.
|
|---|
| 156 | * @return this <code>StringBuffer</code>.
|
|---|
| 157 | * @see java.lang.String#valueOf(java.lang.Object)
|
|---|
| 158 | */
|
|---|
| 159 | public StringBuffer append (Object obj)
|
|---|
| 160 | {
|
|---|
| 161 | return append (String.valueOf(obj));
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /** Append the <code>String</code> to this <code>StringBuffer</code>.
|
|---|
| 165 | * @param str the <code>String</code> to append.
|
|---|
| 166 | * @return this <code>StringBuffer</code>.
|
|---|
| 167 | */
|
|---|
| 168 | public synchronized StringBuffer append (String str)
|
|---|
| 169 | {
|
|---|
| 170 | if (str == null)
|
|---|
| 171 | str = "null";
|
|---|
| 172 | int len = str.length();
|
|---|
| 173 | ensureCapacity_unsynchronized (count + len);
|
|---|
| 174 | str.getChars(0, len, value, count);
|
|---|
| 175 | count += len;
|
|---|
| 176 | return this;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /** Append the <code>char</code> array to this <code>StringBuffer</code>.
|
|---|
| 180 | * @param data the <code>char[]</code> to append.
|
|---|
| 181 | * @return this <code>StringBuffer</code>.
|
|---|
| 182 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
|---|
| 183 | */
|
|---|
| 184 | public StringBuffer append (char[] data)
|
|---|
| 185 | {
|
|---|
| 186 | return append (data, 0, data.length);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /** Append the <code>char</code> array to this <code>StringBuffer</code>.
|
|---|
| 190 | * @param data the <code>char[]</code> to append.
|
|---|
| 191 | * @param offset the place to start grabbing characters from
|
|---|
| 192 | * <code>str</code>.
|
|---|
| 193 | * @param count the number of characters to get from <code>str</code>.
|
|---|
| 194 | * @return this <code>StringBuffer</code>.
|
|---|
| 195 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
|---|
| 196 | * @exception IndexOutOfBoundsException if <code>offset</code> or
|
|---|
| 197 | * <code>offset+len</code> is out of range.
|
|---|
| 198 | */
|
|---|
| 199 | public synchronized StringBuffer append (char[] data, int offset, int count)
|
|---|
| 200 | {
|
|---|
| 201 | ensureCapacity_unsynchronized (this.count + count);
|
|---|
| 202 | System.arraycopy(data, offset, value, this.count, count);
|
|---|
| 203 | this.count += count;
|
|---|
| 204 | return this;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /** Get the total number of characters this <code>StringBuffer</code>
|
|---|
| 208 | * can support before it must be grown. Not to be confused with
|
|---|
| 209 | * <em>length</em>.
|
|---|
| 210 | * @return the capacity of this <code>StringBuffer</code>
|
|---|
| 211 | * @see #length()
|
|---|
| 212 | * @see #ensureCapacity(int)
|
|---|
| 213 | */
|
|---|
| 214 | public int capacity ()
|
|---|
| 215 | {
|
|---|
| 216 | return value.length;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /** Get the character at the specified index.
|
|---|
| 220 | * @param index the index of the character to get, starting at 0.
|
|---|
| 221 | * @return the character at the specified index.
|
|---|
| 222 | * @exception IndexOutOfBoundsException if the desired character index
|
|---|
| 223 | * is negative or greater then length() - 1.
|
|---|
| 224 | */
|
|---|
| 225 | public synchronized char charAt (int index)
|
|---|
| 226 | {
|
|---|
| 227 | if (index >= count)
|
|---|
| 228 | throw new StringIndexOutOfBoundsException (index);
|
|---|
| 229 | return value[index];
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /** Delete characters from this <code>StringBuffer</code>.
|
|---|
| 233 | * <code>delete(10, 12)</code> will delete 10 and 11, but not 12.
|
|---|
| 234 | * @param start the first character to delete.
|
|---|
| 235 | * @param end the index after the last character to delete.
|
|---|
| 236 | * @return this <code>StringBuffer</code>.
|
|---|
| 237 | * @exception StringIndexOutOfBoundsException if <code>start</code>
|
|---|
| 238 | * or <code>end-1</code> are out of bounds, or if
|
|---|
| 239 | * <code>start > end</code>.
|
|---|
| 240 | */
|
|---|
| 241 | public synchronized StringBuffer delete (int start, int end)
|
|---|
| 242 | {
|
|---|
| 243 | if (start < 0 || start > count || start > end)
|
|---|
| 244 | throw new StringIndexOutOfBoundsException (start);
|
|---|
| 245 | if (end > count)
|
|---|
| 246 | end = count;
|
|---|
| 247 | // This will unshare if required.
|
|---|
| 248 | ensureCapacity_unsynchronized (count);
|
|---|
| 249 | if (count - end != 0)
|
|---|
| 250 | System.arraycopy (value, end, value, start, count - end);
|
|---|
| 251 | count -= (end - start);
|
|---|
| 252 | return this;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Delete a character from this <code>StringBuffer</code>.
|
|---|
| 256 | * @param index the index of the character to delete.
|
|---|
| 257 | * @return this <code>StringBuffer</code>.
|
|---|
| 258 | * @exception StringIndexOutOfBoundsException if <code>index</code>
|
|---|
| 259 | * is out of bounds.
|
|---|
| 260 | */
|
|---|
| 261 | public StringBuffer deleteCharAt(int index)
|
|---|
| 262 | {
|
|---|
| 263 | return delete (index, index + 1);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /** Increase the capacity of this <code>StringBuffer</code>.
|
|---|
| 267 | * This will ensure that an expensive growing operation will not occur
|
|---|
| 268 | * until <code>minimumCapacity</code> is reached.
|
|---|
| 269 | * If the capacity is actually already greater than <code>minimumCapacity</code>
|
|---|
| 270 | * @param minimumCapacity the new capacity.
|
|---|
| 271 | * @see #capacity()
|
|---|
| 272 | */
|
|---|
| 273 | public synchronized void ensureCapacity (int minimumCapacity)
|
|---|
| 274 | {
|
|---|
| 275 | if (shared || minimumCapacity > value.length)
|
|---|
| 276 | {
|
|---|
| 277 | // We don't want to make a larger vector when `shared' is
|
|---|
| 278 | // set. If we do, then setLength becomes very inefficient
|
|---|
| 279 | // when repeatedly reusing a StringBuffer in a loop.
|
|---|
| 280 | int max = (minimumCapacity > value.length
|
|---|
| 281 | ? value.length*2+2
|
|---|
| 282 | : value.length);
|
|---|
| 283 | minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
|
|---|
| 284 | char[] nb = new char[minimumCapacity];
|
|---|
| 285 | System.arraycopy(value, 0, nb, 0, count);
|
|---|
| 286 | value = nb;
|
|---|
| 287 | shared = false;
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | // ensureCapacity is used by several synchronized methods in StringBuffer.
|
|---|
| 292 | // There's no need to synchronize again.
|
|---|
| 293 | private void ensureCapacity_unsynchronized (int minimumCapacity)
|
|---|
| 294 | {
|
|---|
| 295 | if (shared || minimumCapacity > value.length)
|
|---|
| 296 | {
|
|---|
| 297 | // We don't want to make a larger vector when `shared' is
|
|---|
| 298 | // set. If we do, then setLength becomes very inefficient
|
|---|
| 299 | // when repeatedly reusing a StringBuffer in a loop.
|
|---|
| 300 | int max = (minimumCapacity > value.length
|
|---|
| 301 | ? value.length*2+2
|
|---|
| 302 | : value.length);
|
|---|
| 303 | minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
|
|---|
| 304 | char[] nb = new char[minimumCapacity];
|
|---|
| 305 | System.arraycopy(value, 0, nb, 0, count);
|
|---|
| 306 | value = nb;
|
|---|
| 307 | shared = false;
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | /** Get the specified array of characters.
|
|---|
| 312 | * The characters will be copied into the array you pass in.
|
|---|
| 313 | * @param srcOffset the index to start copying from in the
|
|---|
| 314 | * <code>StringBuffer</code>.
|
|---|
| 315 | * @param srcEnd the number of characters to copy.
|
|---|
| 316 | * @param dst the array to copy into.
|
|---|
| 317 | * @param dstOffset the index to start copying into <code>dst</code>.
|
|---|
| 318 | * @exception NullPointerException if dst is null.
|
|---|
| 319 | * @exception IndexOutOfBoundsException if any source or target
|
|---|
| 320 | * indices are out of range.
|
|---|
| 321 | * @see java.lang.System#arraycopy(java.lang.Object,int,java.lang.Object,int,int)
|
|---|
| 322 | */
|
|---|
| 323 | public synchronized void getChars (int srcOffset, int srcEnd,
|
|---|
| 324 | char[] dst, int dstOffset)
|
|---|
| 325 | {
|
|---|
| 326 | if (srcOffset < 0 || srcOffset > srcEnd)
|
|---|
| 327 | throw new StringIndexOutOfBoundsException (srcOffset);
|
|---|
| 328 | int todo = srcEnd - srcOffset;
|
|---|
| 329 | if (srcEnd > count || dstOffset + todo > count)
|
|---|
| 330 | throw new StringIndexOutOfBoundsException (srcEnd);
|
|---|
| 331 | System.arraycopy(value, srcOffset, dst, dstOffset, todo);
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 335 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 336 | * <code>String</code>.
|
|---|
| 337 | * @param offset the place to insert.
|
|---|
| 338 | * @param bool the <code>boolean</code> to convert and insert.
|
|---|
| 339 | * @return this <code>StringBuffer</code>.
|
|---|
| 340 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 341 | * of range for this <code>StringBuffer</code>.
|
|---|
| 342 | * @see java.lang.String#valueOf(boolean)
|
|---|
| 343 | */
|
|---|
| 344 | public StringBuffer insert (int offset, boolean bool)
|
|---|
| 345 | {
|
|---|
| 346 | return insert (offset, bool ? "true" : "false");
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | /** Insert the <code>char</code> argument into this <code>StringBuffer</code>.
|
|---|
| 350 | * @param offset the place to insert.
|
|---|
| 351 | * @param ch the <code>char</code> to insert.
|
|---|
| 352 | * @return this <code>StringBuffer</code>.
|
|---|
| 353 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 354 | * of range for this <code>StringBuffer</code>.
|
|---|
| 355 | */
|
|---|
| 356 | public synchronized StringBuffer insert (int offset, char ch)
|
|---|
| 357 | {
|
|---|
| 358 | if (offset < 0 || offset > count)
|
|---|
| 359 | throw new StringIndexOutOfBoundsException (offset);
|
|---|
| 360 | ensureCapacity_unsynchronized (count+1);
|
|---|
| 361 | System.arraycopy(value, offset, value, offset+1, count-offset);
|
|---|
| 362 | value[offset] = ch;
|
|---|
| 363 | count++;
|
|---|
| 364 | return this;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 368 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 369 | * <code>String</code>.
|
|---|
| 370 | * @param offset the place to insert.
|
|---|
| 371 | * @param inum the <code>int</code> to convert and insert.
|
|---|
| 372 | * @return this <code>StringBuffer</code>.
|
|---|
| 373 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 374 | * of range for this <code>StringBuffer</code>.
|
|---|
| 375 | * @see java.lang.String#valueOf(int)
|
|---|
| 376 | */
|
|---|
| 377 | public StringBuffer insert (int offset, int inum)
|
|---|
| 378 | {
|
|---|
| 379 | return insert (offset, String.valueOf(inum));
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 383 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 384 | * <code>String</code>.
|
|---|
| 385 | * @param offset the place to insert.
|
|---|
| 386 | * @param lnum the <code>long</code> to convert and insert.
|
|---|
| 387 | * @return this <code>StringBuffer</code>.
|
|---|
| 388 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 389 | * of range for this <code>StringBuffer</code>.
|
|---|
| 390 | * @see java.lang.String#valueOf(long)
|
|---|
| 391 | */
|
|---|
| 392 | public StringBuffer insert (int offset, long lnum)
|
|---|
| 393 | {
|
|---|
| 394 | return insert (offset, String.valueOf(lnum));
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 398 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 399 | * <code>String</code>.
|
|---|
| 400 | * @param offset the place to insert.
|
|---|
| 401 | * @param fnum the <code>float</code> to convert and insert.
|
|---|
| 402 | * @return this <code>StringBuffer</code>.
|
|---|
| 403 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 404 | * of range for this <code>StringBuffer</code>.
|
|---|
| 405 | * @see java.lang.String#valueOf(float)
|
|---|
| 406 | */
|
|---|
| 407 | public StringBuffer insert (int offset, float fnum)
|
|---|
| 408 | {
|
|---|
| 409 | return insert (offset, String.valueOf(fnum));
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 413 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 414 | * <code>String</code>.
|
|---|
| 415 | * @param offset the place to insert.
|
|---|
| 416 | * @param dnum the <code>double</code> to convert and insert.
|
|---|
| 417 | * @return this <code>StringBuffer</code>.
|
|---|
| 418 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 419 | * of range for this <code>StringBuffer</code>.
|
|---|
| 420 | * @see java.lang.String#valueOf(double)
|
|---|
| 421 | */
|
|---|
| 422 | public StringBuffer insert (int offset, double dnum)
|
|---|
| 423 | {
|
|---|
| 424 | return insert (offset, String.valueOf(dnum));
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.
|
|---|
| 428 | * Uses <code>String.valueOf()</code> to convert to
|
|---|
| 429 | * <code>String</code>.
|
|---|
| 430 | * @param offset the place to insert.
|
|---|
| 431 | * @param obj the <code>Object</code> to convert and insert.
|
|---|
| 432 | * @return this <code>StringBuffer</code>.
|
|---|
| 433 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 434 | * of range for this <code>StringBuffer</code>.
|
|---|
| 435 | * @see java.lang.String#valueOf(java.lang.Object)
|
|---|
| 436 | */
|
|---|
| 437 | public StringBuffer insert (int offset, Object obj)
|
|---|
| 438 | {
|
|---|
| 439 | return insert (offset, String.valueOf(obj));
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /** Insert the <code>String</code> argument into this <code>StringBuffer</code>.
|
|---|
| 443 | * @param offset the place to insert.
|
|---|
| 444 | * @param str the <code>String</code> to insert.
|
|---|
| 445 | * @return this <code>StringBuffer</code>.
|
|---|
| 446 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 447 | * of range for this <code>StringBuffer</code>.
|
|---|
| 448 | */
|
|---|
| 449 | public synchronized StringBuffer insert (int offset, String str)
|
|---|
| 450 | {
|
|---|
| 451 | if (offset < 0 || offset > count)
|
|---|
| 452 | throw new StringIndexOutOfBoundsException (offset);
|
|---|
| 453 | // Note that using `null' is from JDK 1.2.
|
|---|
| 454 | if (str == null)
|
|---|
| 455 | str = "null";
|
|---|
| 456 | int len = str.length();
|
|---|
| 457 | ensureCapacity_unsynchronized (count+len);
|
|---|
| 458 | System.arraycopy(value, offset, value, offset+len, count-offset);
|
|---|
| 459 | str.getChars(0, len, value, offset);
|
|---|
| 460 | count += len;
|
|---|
| 461 | return this;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /** Insert the <code>char[]</code> argument into this
|
|---|
| 465 | * <code>StringBuffer</code>.
|
|---|
| 466 | * @param offset the place to insert.
|
|---|
| 467 | * @param data the <code>char[]</code> to insert.
|
|---|
| 468 | * @return this <code>StringBuffer</code>.
|
|---|
| 469 | * @exception NullPointerException if <code>data</code> is
|
|---|
| 470 | * <code>null</code>.
|
|---|
| 471 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 472 | * of range for this <code>StringBuffer</code>.
|
|---|
| 473 | */
|
|---|
| 474 | public StringBuffer insert (int offset, char[] data)
|
|---|
| 475 | {
|
|---|
| 476 | // One could check if offset is invalid here instead of making sure that
|
|---|
| 477 | // data isn't null before dereferencing, but this works just as well.
|
|---|
| 478 | return insert (offset, data, 0, data == null ? 0 : data.length);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 | /** Insert the <code>char[]</code> argument into this
|
|---|
| 482 | * <code>StringBuffer</code>.
|
|---|
| 483 | * @param offset the place to insert.
|
|---|
| 484 | * @param str the <code>char[]</code> to insert.
|
|---|
| 485 | * @param str_offset the index in <code>str</code> to start inserting
|
|---|
| 486 | * from.
|
|---|
| 487 | * @param len the number of characters to insert.
|
|---|
| 488 | * @return this <code>StringBuffer</code>.
|
|---|
| 489 | * @exception NullPointerException if <code>str</code> is <code>null</code>.
|
|---|
| 490 | * @exception IndexOutOfBoundsException if <code>offset</code> is out
|
|---|
| 491 | * of range, for this <code>StringBuffer</code>, or if
|
|---|
| 492 | * <code>str_offset</code> or <code>str_offset+len</code>
|
|---|
| 493 | * are out of range for <code>str</code>.
|
|---|
| 494 | */
|
|---|
| 495 | public synchronized StringBuffer insert(int offset, char[] str,
|
|---|
| 496 | int str_offset, int len)
|
|---|
| 497 | {
|
|---|
| 498 | if (offset < 0 || offset > count)
|
|---|
| 499 | throw new StringIndexOutOfBoundsException (offset);
|
|---|
| 500 | if (len < 0)
|
|---|
| 501 | throw new StringIndexOutOfBoundsException (len);
|
|---|
| 502 | if (str_offset < 0 || str_offset + len > str.length)
|
|---|
| 503 | throw new StringIndexOutOfBoundsException (str_offset);
|
|---|
| 504 | ensureCapacity_unsynchronized (count + len);
|
|---|
| 505 | System.arraycopy(value, offset, value, offset + len, count - offset);
|
|---|
| 506 | System.arraycopy(str, str_offset, value, offset, len);
|
|---|
| 507 | count += len;
|
|---|
| 508 | return this;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | /** Get the length of the <code>String</code> this
|
|---|
| 512 | * <code>StringBuffer</code> would create. Not to be confused with the
|
|---|
| 513 | * <em>capacity</em> of the <code>StringBuffer</code>.
|
|---|
| 514 | * @return the length of this <code>StringBuffer</code>.
|
|---|
| 515 | * @see #capacity()
|
|---|
| 516 | * @see #setLength(int)
|
|---|
| 517 | */
|
|---|
| 518 | public int length ()
|
|---|
| 519 | {
|
|---|
| 520 | return count;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /** Replace characters between index <code>start</code> (inclusive) and
|
|---|
| 524 | * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
|
|---|
| 525 | * is larger than the size of this StringBuffer, all characters after
|
|---|
| 526 | * <code>start</code> are replaced.
|
|---|
| 527 | * @param start the beginning index of characters to delete (inclusive).
|
|---|
| 528 | * @param end the ending index of characters to delete (exclusive).
|
|---|
| 529 | * @param str the new <code>String</code> to insert.
|
|---|
| 530 | * @return this <code>StringBuffer</code>.
|
|---|
| 531 | */
|
|---|
| 532 | public synchronized StringBuffer replace (int start, int end, String str)
|
|---|
| 533 | {
|
|---|
| 534 | if (start < 0 || start > count || start > end)
|
|---|
| 535 | throw new StringIndexOutOfBoundsException (start);
|
|---|
| 536 |
|
|---|
| 537 | int len = str.length();
|
|---|
| 538 | // Calculate the difference in 'count' after the replace.
|
|---|
| 539 | int delta = len - ((end > count ? count : end) - start);
|
|---|
| 540 | ensureCapacity_unsynchronized (count + delta);
|
|---|
| 541 |
|
|---|
| 542 | if (delta != 0 && end < count)
|
|---|
| 543 | System.arraycopy(value, end, value, end + delta, count - end);
|
|---|
| 544 |
|
|---|
| 545 | str.getChars (0, len, value, start);
|
|---|
| 546 | count += delta;
|
|---|
| 547 | return this;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /** Reverse the characters in this StringBuffer.
|
|---|
| 551 | * @return this <code>StringBuffer</code>.
|
|---|
| 552 | */
|
|---|
| 553 | public synchronized StringBuffer reverse ()
|
|---|
| 554 | {
|
|---|
| 555 | // Call ensureCapacity to enforce copy-on-write.
|
|---|
| 556 | ensureCapacity_unsynchronized (count);
|
|---|
| 557 | for (int i = 0; i < count / 2; ++i)
|
|---|
| 558 | {
|
|---|
| 559 | char c = value[i];
|
|---|
| 560 | value[i] = value[count - i - 1];
|
|---|
| 561 | value[count - i - 1] = c;
|
|---|
| 562 | }
|
|---|
| 563 | return this;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | /** Set the character at the specified index.
|
|---|
| 567 | * @param index the index of the character to set starting at 0.
|
|---|
| 568 | * @param ch the value to set that character to.
|
|---|
| 569 | * @exception IndexOutOfBoundsException if the specified character
|
|---|
| 570 | * index is not between 0 and length() - 1 (inclusive).
|
|---|
| 571 | */
|
|---|
| 572 | public synchronized void setCharAt (int index, char ch)
|
|---|
| 573 | {
|
|---|
| 574 | if (index < 0 || index >= count)
|
|---|
| 575 | throw new StringIndexOutOfBoundsException (index);
|
|---|
| 576 | // Call ensureCapacity to enforce copy-on-write.
|
|---|
| 577 | ensureCapacity_unsynchronized (count);
|
|---|
| 578 | value[index] = ch;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | /** Set the length of this StringBuffer.
|
|---|
| 582 | * <P>
|
|---|
| 583 | * If the new length is greater than the current length, all the new
|
|---|
| 584 | * characters are set to '\0'.
|
|---|
| 585 | * <P>
|
|---|
| 586 | * If the new length is less than the current length, the first
|
|---|
| 587 | * <code>newLength</code> characters of the old array will be
|
|---|
| 588 | * @param newLength the new length
|
|---|
| 589 | * @exception IndexOutOfBoundsException if the new length is
|
|---|
| 590 | * negative.
|
|---|
| 591 | * @see #length()
|
|---|
| 592 | */
|
|---|
| 593 | public synchronized void setLength (int newLength)
|
|---|
| 594 | {
|
|---|
| 595 | if (newLength < 0)
|
|---|
| 596 | throw new StringIndexOutOfBoundsException (newLength);
|
|---|
| 597 |
|
|---|
| 598 | ensureCapacity_unsynchronized (newLength);
|
|---|
| 599 | for (int i = count; i < newLength; ++i)
|
|---|
| 600 | value[i] = '\0';
|
|---|
| 601 | count = newLength;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | /** Create a new StringBuffer with default capacity 16.
|
|---|
| 605 | * @see JLS 20.13.1
|
|---|
| 606 | */
|
|---|
| 607 | public StringBuffer ()
|
|---|
| 608 | {
|
|---|
| 609 | this (DEFAULT_CAPACITY);
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
|
|---|
| 613 | * @param capacity the initial capacity.
|
|---|
| 614 | */
|
|---|
| 615 | public StringBuffer (int capacity)
|
|---|
| 616 | {
|
|---|
| 617 | count = 0;
|
|---|
| 618 | value = new char[capacity];
|
|---|
| 619 | shared = false;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>.
|
|---|
| 623 | * Initial capacity will be the size of the String plus 16.
|
|---|
| 624 | * @param str the <code>String</code> to make a <code>StringBuffer</code> out of.
|
|---|
| 625 | * @XXX optimize for sharing.
|
|---|
| 626 | */
|
|---|
| 627 | public StringBuffer (String str)
|
|---|
| 628 | {
|
|---|
| 629 | // The documentation is not clear, but experimentation with
|
|---|
| 630 | // other implementations indicates that StringBuffer(null)
|
|---|
| 631 | // should throw a NullPointerException.
|
|---|
| 632 | count = str.length();
|
|---|
| 633 | // JLS: The initial capacity of the string buffer is 16 plus the
|
|---|
| 634 | // length of the argument string.
|
|---|
| 635 | value = new char[count + DEFAULT_CAPACITY];
|
|---|
| 636 | str.getChars(0, count, value, 0);
|
|---|
| 637 | shared = false;
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | /**
|
|---|
| 641 | * Creates a substring of this StringBuffer, starting at a specified index
|
|---|
| 642 | * and ending at the end of this StringBuffer.
|
|---|
| 643 | *
|
|---|
| 644 | * @param beginIndex index to start substring (base 0)
|
|---|
| 645 | *
|
|---|
| 646 | * @return new String which is a substring of this StringBuffer
|
|---|
| 647 | *
|
|---|
| 648 | * @exception StringIndexOutOfBoundsException
|
|---|
| 649 | * if (beginIndex < 0 || beginIndex > this.length())
|
|---|
| 650 | */
|
|---|
| 651 | public String substring (int beginIndex)
|
|---|
| 652 | {
|
|---|
| 653 | return substring (beginIndex, count);
|
|---|
| 654 | }
|
|---|
| 655 |
|
|---|
| 656 | /**
|
|---|
| 657 | * Creates a substring of this StringBuffer, starting at a specified index
|
|---|
| 658 | * and ending at one character before a specified index.
|
|---|
| 659 | *
|
|---|
| 660 | * @param beginIndex index to start substring (base 0)
|
|---|
| 661 | * @param endIndex index after the last character to be
|
|---|
| 662 | * copied into the substring
|
|---|
| 663 | *
|
|---|
| 664 | * @return new String which is a substring of this StringBuffer
|
|---|
| 665 | *
|
|---|
| 666 | * @exception StringIndexOutOfBoundsException
|
|---|
| 667 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
|---|
| 668 | */
|
|---|
| 669 | public synchronized String substring (int beginIndex, int endIndex)
|
|---|
| 670 | {
|
|---|
| 671 | if (beginIndex < 0 || endIndex > count || beginIndex > endIndex)
|
|---|
| 672 | throw new StringIndexOutOfBoundsException ();
|
|---|
| 673 | // FIXME: for libgcj it would be possible, and more efficient, to
|
|---|
| 674 | // enable sharing here.
|
|---|
| 675 | return new String (value, beginIndex, endIndex - beginIndex);
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | /**
|
|---|
| 679 | * Creates a substring of this StringBuffer, starting at a specified index
|
|---|
| 680 | * and ending at one character before a specified index.
|
|---|
| 681 | * <p>
|
|---|
| 682 | * To implement <code>CharSequence</code>.
|
|---|
| 683 | * Calls <code>substring(beginIndex, endIndex)</code>.
|
|---|
| 684 | *
|
|---|
| 685 | * @param beginIndex index to start substring (base 0)
|
|---|
| 686 | * @param endIndex index after the last character to be
|
|---|
| 687 | * copied into the substring
|
|---|
| 688 | *
|
|---|
| 689 | * @return new String which is a substring of this StringBuffer
|
|---|
| 690 | *
|
|---|
| 691 | * @exception StringIndexOutOfBoundsException
|
|---|
| 692 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
|---|
| 693 | */
|
|---|
| 694 | public CharSequence subSequence (int beginIndex, int endIndex)
|
|---|
| 695 | {
|
|---|
| 696 | return substring(beginIndex, endIndex);
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 | /** Convert this <code>StringBuffer</code> to a <code>String</code>.
|
|---|
| 701 | * @return the characters in this StringBuffer
|
|---|
| 702 | */
|
|---|
| 703 | public String toString ()
|
|---|
| 704 | {
|
|---|
| 705 | // Note: in libgcj this causes the StringBuffer to be shared. In
|
|---|
| 706 | // Classpath it does not.
|
|---|
| 707 | return new String (this);
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | // Index of next available character. Note that this has
|
|---|
| 711 | // permissions set this way so that String can get the value.
|
|---|
| 712 | int count;
|
|---|
| 713 |
|
|---|
| 714 | // The buffer. Note that this has permissions set this way so that
|
|---|
| 715 | // String can get the value.
|
|---|
| 716 | char[] value;
|
|---|
| 717 |
|
|---|
| 718 | // True if we need to copy the buffer before writing to it again.
|
|---|
| 719 | // FIXME: JDK 1.2 doesn't specify this. The new buffer-growing
|
|---|
| 720 | // semantics make this less useful in that case, too. Note that
|
|---|
| 721 | // this has permissions set this way so that String can get the
|
|---|
| 722 | // value.
|
|---|
| 723 | boolean shared;
|
|---|
| 724 |
|
|---|
| 725 | static final long serialVersionUID = 3388685877147921107L;
|
|---|
| 726 | private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1
|
|---|
| 727 | }
|
|---|