⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringbuffer.java

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* StringBuffer.java -- Growable strings   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.lang;import java.io.Serializable;/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * Updated using online JDK 1.2 docs. * Believed complete and correct to JDK 1.2. * Merged with Classpath. *//** * <code>StringBuffer</code> represents a changeable <code>String</code>. * It provides the operations required to modify the * <code>StringBuffer</code> including insert, replace, delete, append, * and reverse. * <P> * * <code>StringBuffer</code>s are variable-length in nature, so even if * you initialize them to a certain size, they can still grow larger than * that.  <EM>Capacity</EM> indicates the number of characters the * <code>StringBuffer</code> can have in it before it has to grow (growing * the char array is an expensive operation involving <code>new</code>). * <P> * * Incidentally, the String operator "+" actually is turned into a * <code>StringBuffer</code> operation: * <BR> * <code>a + b</code> * <BR> * is the same as * <BR> * <code>new StringBuffer(a).append(b).toString()</code>. * * @implnote Classpath's StringBuffer is capable of sharing memory with *           Strings for efficiency.  This will help in two instances: *           first, when a StringBuffer is created from a String but is *           never changed, and second, when a StringBuffer is converted *           to a String and the StringBuffer is not changed after that. * * @since JDK1.0 * @author Paul Fisher * @author John Keiser * @author Tom Tromey * @see java.lang.String */public final class StringBuffer implements Serializable, CharSequence{  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param bool the <code>boolean</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(boolean)   */  public StringBuffer append (boolean bool)  {    return append (String.valueOf(bool));  }  /** Append the <code>char</code> to this <code>StringBuffer</code>.   *  @param c the <code>char</code> to append.   *  @return this <code>StringBuffer</code>.   */  public synchronized StringBuffer append (char ch)  {    ensureCapacity_unsynchronized (count + 1);    value[count++] = ch;    return this;  }  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param inum the <code>int</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(int)   */  public native StringBuffer append (int inum);  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param lnum the <code>long</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(long)   */  public StringBuffer append (long lnum)  {    return append (String.valueOf(lnum));  }  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param fnum the <code>float</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(float)   */  public StringBuffer append (float fnum)  {    return append (String.valueOf(fnum));  }  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param dnum the <code>double</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(double)   */  public StringBuffer append (double dnum)  {    return append (String.valueOf(dnum));  }  /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param obj the <code>Object</code> to convert and append.   *  @return this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(java.lang.Object)   */  public StringBuffer append (Object obj)  {    return append (String.valueOf(obj));  }  /** Append the <code>String</code> to this <code>StringBuffer</code>.   *  @param str the <code>String</code> to append.   *  @return this <code>StringBuffer</code>.   */  public synchronized StringBuffer append (String str)  {    if (str == null)      str = "null";    int len = str.length();    ensureCapacity_unsynchronized (count + len);    str.getChars(0, len, value, count);    count += len;    return this;  }  /** Append the <code>char</code> array to this <code>StringBuffer</code>.   *  @param data the <code>char[]</code> to append.   *  @return this <code>StringBuffer</code>.   *  @exception NullPointerException if <code>str</code> is <code>null</code>.   */  public StringBuffer append (char[] data)  {    return append (data, 0, data.length);  }  /** Append the <code>char</code> array to this <code>StringBuffer</code>.   *  @param data the <code>char[]</code> to append.   *  @param offset the place to start grabbing characters from   *         <code>str</code>.   *  @param count the number of characters to get from <code>str</code>.   *  @return this <code>StringBuffer</code>.   *  @exception NullPointerException if <code>str</code> is <code>null</code>.   *  @exception IndexOutOfBoundsException if <code>offset</code> or   *             <code>offset+len</code> is out of range.   */  public synchronized StringBuffer append (char[] data, int offset, int count)  {    ensureCapacity_unsynchronized (this.count + count);    System.arraycopy(data, offset, value, this.count, count);    this.count += count;    return this;  }   /** Get the total number of characters this <code>StringBuffer</code>   *  can support before it must be grown.  Not to be confused with   *  <em>length</em>.   *  @return the capacity of this <code>StringBuffer</code>   *  @see #length()   *  @see #ensureCapacity(int)   */  public int capacity ()  {    return value.length;  }  /** Get the character at the specified index.   *  @param index the index of the character to get, starting at 0.   *  @return the character at the specified index.   *  @exception IndexOutOfBoundsException if the desired character index   *             is negative or greater then length() - 1.   */  public synchronized char charAt (int index)  {    if (index >= count)      throw new StringIndexOutOfBoundsException (index);    return value[index];  }  /** Delete characters from this <code>StringBuffer</code>.   *  <code>delete(10, 12)</code> will delete 10 and 11, but not 12.   *  @param start the first character to delete.   *  @param end the index after the last character to delete.   *  @return this <code>StringBuffer</code>.   *  @exception StringIndexOutOfBoundsException if <code>start</code>   *             or <code>end-1</code> are out of bounds, or if   *             <code>start > end</code>.   */  public synchronized StringBuffer delete (int start, int end)  {    if (start < 0 || start > count || start > end)      throw new StringIndexOutOfBoundsException (start);    if (end > count)      end = count;    // This will unshare if required.    ensureCapacity_unsynchronized (count);    if (count - end != 0)      System.arraycopy (value, end, value, start, count - end);    count -= (end - start);    return this;  }  /** Delete a character from this <code>StringBuffer</code>.   *  @param index the index of the character to delete.   *  @return this <code>StringBuffer</code>.   *  @exception StringIndexOutOfBoundsException if <code>index</code>   *             is out of bounds.   */  public StringBuffer deleteCharAt(int index)  {    return delete (index, index + 1);  }  /** Increase the capacity of this <code>StringBuffer</code>.   *  This will ensure that an expensive growing operation will not occur   *  until <code>minimumCapacity</code> is reached.   *  If the capacity is actually already greater than <code>minimumCapacity</code>   *  @param minimumCapacity the new capacity.   *  @see #capacity()   */  public synchronized void ensureCapacity (int minimumCapacity)  {    if (shared || minimumCapacity > value.length)      {	// We don't want to make a larger vector when `shared' is	// set.  If we do, then setLength becomes very inefficient	// when repeatedly reusing a StringBuffer in a loop.	int max = (minimumCapacity > value.length		   ? value.length*2+2		   : value.length);	minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);	char[] nb = new char[minimumCapacity];	System.arraycopy(value, 0, nb, 0, count);	value = nb;	shared = false;      }  }  // ensureCapacity is used by several synchronized methods in StringBuffer.  // There's no need to synchronize again.  private void ensureCapacity_unsynchronized (int minimumCapacity)  {    if (shared || minimumCapacity > value.length)      {	// We don't want to make a larger vector when `shared' is	// set.  If we do, then setLength becomes very inefficient	// when repeatedly reusing a StringBuffer in a loop.	int max = (minimumCapacity > value.length		   ? value.length*2+2		   : value.length);	minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);	char[] nb = new char[minimumCapacity];	System.arraycopy(value, 0, nb, 0, count);	value = nb;	shared = false;      }  }  /**   * Get the specified array of characters. <code>srcOffset - srcEnd</code>   * characters will be copied into the array you pass in.   *   * @param srcOffset the index to start copying from (inclusive)   * @param srcEnd the index to stop copying from (exclusive)   * @param dst the array to copy into   * @param dstOffset the index to start copying into   * @throws NullPointerException if dst is null   * @throws IndexOutOfBoundsException if any source or target indices are   *         out of range (while unspecified, source problems cause a   *         StringIndexOutOfBoundsException, and dest problems cause an   *         ArrayIndexOutOfBoundsException)   * @see System#arraycopy(Object, int, Object, int, int)   */  public synchronized void getChars(int srcOffset, int srcEnd,                                    char[] dst, int dstOffset)  {    int todo = srcEnd - srcOffset;    if (srcOffset < 0 || srcEnd > count || todo < 0)      throw new StringIndexOutOfBoundsException();    System.arraycopy(value, srcOffset, dst, dstOffset, todo);  }  /** Insert the <code>String</code> value of the argument into this <code>StringBuffer</code>.   *  Uses <code>String.valueOf()</code> to convert to   *  <code>String</code>.   *  @param offset the place to insert.   *  @param bool the <code>boolean</code> to convert and insert.   *  @return this <code>StringBuffer</code>.   *  @exception IndexOutOfBoundsException if <code>offset</code> is out   *             of range for this <code>StringBuffer</code>.   *  @see java.lang.String#valueOf(boolean)   */  public StringBuffer insert (int offset, boolean bool)  {    return insert (offset, bool ? "true" : "false");  }  /** Insert the <code>char</code> argument into this <code>StringBuffer</code>.   *  @param offset the place to insert.   *  @param ch the <code>char</code> to insert.   *  @return this <code>StringBuffer</code>.   *  @exception IndexOutOfBoundsException if <code>offset</code> is out   *             of range for this <code>StringBuffer</code>.   */  public synchronized StringBuffer insert (int offset, char ch)  {    if (offset < 0 || offset > count)      throw new StringIndexOutOfBoundsException (offset);    ensureCapacity_unsynchronized (count+1);    System.arraycopy(value, offset, value, offset+1, count-offset);    value[offset] = ch;    count++;    return this;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -