home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / StringBuffer.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  27.3 KB  |  774 lines

  1. /*
  2.  * @(#)StringBuffer.java    1.39 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * A string buffer implements a mutable sequence of characters. 
  19.  * <p>
  20.  * String buffers are safe for use by multiple threads. The methods 
  21.  * are synchronized where necessary so that all the operations on any 
  22.  * particular instance behave as if they occur in some serial order. 
  23.  * <p>
  24.  * String buffers are used by the compiler to implement the binary 
  25.  * string concatenation operator <code>+</code>. For example, the code:
  26.  * <p><blockquote><pre>
  27.  *     x = "a" + 4 + "c"
  28.  * </pre></blockquote><p>
  29.  * is compiled to the equivalent of: 
  30.  * <p><blockquote><pre>
  31.  *     x = new StringBuffer().append("a").append(4).append("c")
  32.  *                           .toString()
  33.  * </pre></blockquote><p>
  34.  * The principal operations on a <code>StringBuffer</code> are the 
  35.  * <code>append</code> and <code>insert</code> methods, which are 
  36.  * overloaded so as to accept data of any type. Each effectively 
  37.  * converts a given datum to a string and then appends or inserts the 
  38.  * characters of that string to the string buffer. The 
  39.  * <code>append</code> method always adds these characters at the end 
  40.  * of the buffer; the <code>insert</code> method adds the characters at 
  41.  * a specified point. 
  42.  * <p>
  43.  * For example, if <code>z</code> refers to a string buffer object 
  44.  * whose current contents are "<code>start</code>", then 
  45.  * the method call <code>z.append("le")</code> would cause the string 
  46.  * buffer to contain "<code>startle</code>", whereas 
  47.  * <code>z.insert(4, "le")</code> would alter the string buffer to 
  48.  * contain "<code>starlet</code>". 
  49.  * <p>
  50.  * Every string buffer has a capacity. As long as the length of the 
  51.  * character sequence contained in the string buffer does not exceed 
  52.  * the capacity, it is not necessary to allocate a new internal 
  53.  * buffer array. If the internal buffer overflows, it is 
  54.  * automatically made larger. 
  55.  *
  56.  * @author    Arthur van Hoff
  57.  * @version     1.39, 03/18/98
  58.  * @see     java.io.ByteArrayOutputStream
  59.  * @see     java.lang.String
  60.  * @since   JDK1.0
  61.  */
  62.  
  63. public final class StringBuffer implements java.io.Serializable {
  64.     /** The value is used for character storage. */
  65.     private char value[];
  66.  
  67.     /** The count is the number of characters in the buffer. */
  68.     private int count;
  69.  
  70.     /** A flag indicating whether the buffer is shared */
  71.     private boolean shared;
  72.  
  73.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  74.     static final long serialVersionUID = 3388685877147921107L;
  75.  
  76.     /**
  77.      * Constructs a string buffer with no characters in it and an 
  78.      * initial capacity of 16 characters. 
  79.      */
  80.     public StringBuffer() {
  81.     this(16);
  82.     }
  83.  
  84.     /**
  85.      * Constructs a string buffer with no characters in it and an 
  86.      * initial capacity specified by the <code>length</code> argument. 
  87.      *
  88.      * @param      length   the initial capacity.
  89.      * @exception  NegativeArraySizeException  if the <code>length</code>
  90.      *               argument is less than <code>0</code>.
  91.      */
  92.     public StringBuffer(int length) {
  93.     value = new char[length];
  94.     shared = false;
  95.     }
  96.  
  97.     /**
  98.      * Constructs a string buffer so that it represents the same 
  99.      * sequence of characters as the string argument. The initial 
  100.      * capacity of the string buffer is <code>16</code> plus the length 
  101.      * of the string argument. 
  102.      *
  103.      * @param   str   the initial contents of the buffer.
  104.      */
  105.     public StringBuffer(String str) {
  106.     this(str.length() + 16);
  107.     append(str);
  108.     }
  109.  
  110.     /**
  111.      * Returns the length (character count) of this string buffer.
  112.      *
  113.      * @return  the number of characters in this string buffer.
  114.      */
  115.     public int length() {
  116.     return count;
  117.     }
  118.  
  119.     /**
  120.      * Returns the current capacity of the String buffer. The capacity
  121.      * is the amount of storage available for newly inserted
  122.      * characters; beyond which an allocation will occur.
  123.      *
  124.      * @return  the current capacity of this string buffer.
  125.      */
  126.     public int capacity() {
  127.     return value.length;
  128.     }
  129.  
  130.     /**
  131.      * Copies the buffer value.  This is normally only called when shared
  132.      * is true.  It should only be called from a synchronized method.
  133.      */
  134.     private final void copy() {
  135.     char newValue[] = new char[value.length];
  136.     System.arraycopy(value, 0, newValue, 0, count);
  137.     value = newValue;
  138.     shared = false;
  139.     }
  140.  
  141.     /**
  142.      * Ensures that the capacity of the buffer is at least equal to the
  143.      * specified minimum.
  144.      * If the current capacity of this string buffer is less than the 
  145.      * argument, then a new internal buffer is allocated with greater 
  146.      * capacity. The new capacity is the larger of: 
  147.      * <ul>
  148.      * <li>The <code>minimumCapacity</code> argument. 
  149.      * <li>Twice the old capacity, plus <code>2</code>. 
  150.      * </ul>
  151.      * If the <code>minimumCapacity</code> argument is nonpositive, this
  152.      * method takes no action and simply returns.
  153.      *
  154.      * @param   minimumCapacity   the minimum desired capacity.
  155.      */
  156.     public synchronized void ensureCapacity(int minimumCapacity) {
  157.     if (minimumCapacity > value.length) {
  158.         expandCapacity(minimumCapacity);
  159.     }
  160.     }
  161.  
  162.     /**
  163.      * This implements the expansion semantics of ensureCapacity but is
  164.      * unsynchronized for use internally by methods which are already
  165.      * synchronized.
  166.      *
  167.      * @see java.lang.StringBuffer#ensureCapacity(int)
  168.      */
  169.     private void expandCapacity(int minimumCapacity) {
  170.     int newCapacity = (value.length + 1) * 2;
  171.     if (minimumCapacity > newCapacity) {
  172.         newCapacity = minimumCapacity;
  173.     }
  174.     
  175.     char newValue[] = new char[newCapacity];
  176.     System.arraycopy(value, 0, newValue, 0, count);
  177.     value = newValue;
  178.     shared = false;
  179.     }
  180.  
  181.     /**
  182.      * Sets the length of this String buffer.
  183.      * If the <code>newLength</code> argument is less than the current 
  184.      * length of the string buffer, the string buffer is truncated to 
  185.      * contain exactly the number of characters given by the 
  186.      * <code>newLength</code> argument. 
  187.      * <p>
  188.      * If the <code>newLength</code> argument is greater than or equal 
  189.      * to the current length, sufficient null characters 
  190.      * (<code>'\u0000'</code>) are appended to the string buffer so that 
  191.      * length becomes the <code>newLength</code> argument. 
  192.      * <p>
  193.      * The <code>newLength</code> argument must be greater than or equal 
  194.      * to <code>0</code>. 
  195.      *
  196.      * @param      newLength   the new length of the buffer.
  197.      * @exception  StringIndexOutOfBoundsException  if the
  198.      *               <code>newLength</code> argument is invalid.
  199.      * @see        java.lang.StringBuffer#length()
  200.      */
  201.     public synchronized void setLength(int newLength) {
  202.     if (newLength < 0) {
  203.         throw new StringIndexOutOfBoundsException(newLength);
  204.     }
  205.     
  206.     if (newLength > value.length) {
  207.         expandCapacity(newLength);
  208.     }
  209.  
  210.     if (count < newLength) {
  211.         if (shared) copy();
  212.         for (; count < newLength; count++) {
  213.         value[count] = '\0';
  214.         }
  215.     } else {
  216.             count = newLength;
  217.             if (shared) copy();
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Returns the character at a specific index in this string buffer. 
  223.      * <p>
  224.      * The first character of a string buffer is at index 
  225.      * <code>0</code>, the next at index <code>1</code>, and so on, for 
  226.      * array indexing. 
  227.      * <p>
  228.      * The index argument must be greater than or equal to 
  229.      * <code>0</code>, and less than the length of this string buffer. 
  230.      *
  231.      * @param      index   the index of the desired character.
  232.      * @return     the character at the specified index of this string buffer.
  233.      * @exception  StringIndexOutOfBoundsException  if the index is invalid.
  234.      * @see        java.lang.StringBuffer#length()
  235.      */
  236.     public synchronized char charAt(int index) {
  237.     if ((index < 0) || (index >= count)) {
  238.         throw new StringIndexOutOfBoundsException(index);
  239.     }
  240.     return value[index];
  241.     }
  242.  
  243.     /**
  244.      * Characters are copied from this string buffer into the 
  245.      * destination character array <code>dst</code>. The first character to 
  246.      * be copied is at index <code>srcBegin</code>; the last character to 
  247.      * be copied is at index <code>srcEnd-1.</code> The total number of 
  248.      * characters to be copied is <code>srcEnd-srcBegin</code>. The 
  249.      * characters are copied into the subarray of <code>dst</code> starting 
  250.      * at index <code>dstBegin</code> and ending at index:
  251.      * <p><blockquote><pre>
  252.      *     dstbegin + (srcEnd-srcBegin) - 1
  253.      * </pre></blockquote>
  254.      *
  255.      * @param      srcBegin   start copying at this offset in the string buffer.
  256.      * @param      srcEnd     stop copying at this offset in the string buffer.
  257.      * @param      dst        the array to copy the data into.
  258.      * @param      dstBegin   offset into <code>dst</code>.
  259.      * @exception  StringIndexOutOfBoundsException  if there is an invalid
  260.      *               index into the buffer.
  261.      */
  262.     public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  263.     if ((srcBegin < 0) || (srcBegin >= count)) {
  264.         throw new StringIndexOutOfBoundsException(srcBegin);
  265.     }
  266.     if ((srcEnd < 0) || (srcEnd > count)) {
  267.         throw new StringIndexOutOfBoundsException(srcEnd);
  268.     }
  269.     if (srcBegin < srcEnd) {
  270.         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  271.     } else {
  272.         if (srcBegin > srcEnd) {
  273.         throw new StringIndexOutOfBoundsException
  274.             ("StringBuffer.getChars(): begin > end");
  275.         }
  276.         /* We do nothing when srcBegin == srcEnd. */
  277.     }
  278.     }
  279.  
  280.     /**
  281.      * The character at the specified index of this string buffer is set 
  282.      * to <code>ch</code>. 
  283.      * <p>
  284.      * The offset argument must be greater than or equal to 
  285.      * <code>0</code>, and less than the length of this string buffer. 
  286.      *
  287.      * @param      index   the index of the character to modify.
  288.      * @param      ch      the new character.
  289.      * @exception  StringIndexOutOfBoundsException  if the index is invalid.
  290.      * @see        java.lang.StringBuffer#length()
  291.      */
  292.     public synchronized void setCharAt(int index, char ch) {
  293.     if ((index < 0) || (index >= count)) {
  294.         throw new StringIndexOutOfBoundsException(index);
  295.     }
  296.     if (shared) copy();
  297.     value[index] = ch;
  298.     }
  299.  
  300.     /**
  301.      * Appends the string representation of the <code>Object</code> 
  302.      * argument to this string buffer. 
  303.      * <p>
  304.      * The argument is converted to a string as if by the method 
  305.      * <code>String.valueOf</code>, and the characters of that 
  306.      * string are then appended to this string buffer. 
  307.      *
  308.      * @param   obj   an <code>Object</code>.
  309.      * @return  this string buffer.
  310.      * @see     java.lang.String#valueOf(java.lang.Object)
  311.      * @see     java.lang.StringBuffer#append(java.lang.String)
  312.      */
  313.     public synchronized StringBuffer append(Object obj) {
  314.     return append(String.valueOf(obj));
  315.     }
  316.  
  317.     /**
  318.      * Appends the string to this string buffer. 
  319.      * <p>
  320.      * The characters of the <code>String</code> argument are appended, in 
  321.      * order, to the contents of this string buffer, increasing the 
  322.      * length of this string buffer by the length of the argument. 
  323.      *
  324.      * @param   str   a string.
  325.      * @return  this string buffer.
  326.      */
  327.     public synchronized StringBuffer append(String str) {
  328.     if (str == null) {
  329.         str = String.valueOf(str);
  330.     }
  331.  
  332.     int len = str.length();
  333.     int newcount = count + len;
  334.     if (newcount > value.length)
  335.         expandCapacity(newcount);
  336.     str.getChars(0, len, value, count);
  337.     count = newcount;
  338.     return this;
  339.     }
  340.  
  341.     /**
  342.      * Appends the string representation of the <code>char</code> array 
  343.      * argument to this string buffer. 
  344.      * <p>
  345.      * The characters of the array argument are appended, in order, to 
  346.      * the contents of this string buffer. The length of this string 
  347.      * buffer increases by the length of the argument. 
  348.      *
  349.      * @param   str   the characters to be appended.
  350.      * @return  this string buffer.
  351.      */
  352.     public synchronized StringBuffer append(char str[]) {
  353.     int len = str.length;
  354.     int newcount = count + len;
  355.     if (newcount > value.length)
  356.         expandCapacity(newcount);
  357.     System.arraycopy(str, 0, value, count, len);
  358.     count = newcount;
  359.     return this;
  360.     }
  361.  
  362.     /**
  363.      * Appends the string representation of a subarray of the 
  364.      * <code>char</code> array argument to this string buffer. 
  365.      * <p>
  366.      * Characters of the character array <code>str</code>, starting at 
  367.      * index <code>offset</code>, are appended, in order, to the contents 
  368.      * of this string buffer. The length of this string buffer increases 
  369.      * by the value of <code>len</code>. 
  370.      *
  371.      * @param   str      the characters to be appended.
  372.      * @param   offset   the index of the first character to append.
  373.      * @param   len      the number of characters to append.
  374.      * @return  this string buffer.
  375.      */
  376.     public synchronized StringBuffer append(char str[], int offset, int len) {
  377.         int newcount = count + len;
  378.     if (newcount > value.length)
  379.         expandCapacity(newcount);
  380.     System.arraycopy(str, offset, value, count, len);
  381.     count = newcount;
  382.     return this;
  383.     }
  384.  
  385.     /**
  386.      * Appends the string representation of the <code>boolean</code> 
  387.      * argument to the string buffer. 
  388.      * <p>
  389.      * The argument is converted to a string as if by the method 
  390.      * <code>String.valueOf</code>, and the characters of that 
  391.      * string are then appended to this string buffer. 
  392.      *
  393.      * @param   b   a <code>boolean</code>.
  394.      * @return  this string buffer.
  395.      * @see     java.lang.String#valueOf(boolean)
  396.      * @see     java.lang.StringBuffer#append(java.lang.String)
  397.      */
  398.     public StringBuffer append(boolean b) {
  399.     return append(String.valueOf(b));
  400.     }
  401.  
  402.     /**
  403.      * Appends the string representation of the <code>char</code> 
  404.      * argument to this string buffer. 
  405.      * <p>
  406.      * The argument is appended to the contents of this string buffer. 
  407.      * The length of this string buffer increases by <code>1</code>. 
  408.      *
  409.      * @param   ch   a <code>char</code>.
  410.      * @return  this string buffer.
  411.      */
  412.     public synchronized StringBuffer append(char c) {
  413.         int newcount = count + 1;
  414.     if (newcount > value.length)
  415.         expandCapacity(newcount);
  416.     value[count++] = c;
  417.     return this;
  418.     }
  419.  
  420.     /**
  421.      * Appends the string representation of the <code>int</code> 
  422.      * argument to this string buffer. 
  423.      * <p>
  424.      * The argument is converted to a string as if by the method 
  425.      * <code>String.valueOf</code>, and the characters of that 
  426.      * string are then appended to this string buffer. 
  427.      *
  428.      * @param   i   an <code>int</code>.
  429.      * @return  this string buffer.
  430.      * @see     java.lang.String#valueOf(int)
  431.      * @see     java.lang.StringBuffer#append(java.lang.String)
  432.      */
  433.     public StringBuffer append(int i) {
  434.     return append(String.valueOf(i));
  435.     }
  436.  
  437.     /**
  438.      * Appends the string representation of the <code>long</code> 
  439.      * argument to this string buffer. 
  440.      * <p>
  441.      * The argument is converted to a string as if by the method 
  442.      * <code>String.valueOf</code>, and the characters of that 
  443.      * string are then appended to this string buffer. 
  444.      *
  445.      * @param   l   a <code>long</code>.
  446.      * @return  this string buffer.
  447.      * @see     java.lang.String#valueOf(long)
  448.      * @see     java.lang.StringBuffer#append(java.lang.String)
  449.      */
  450.     public StringBuffer append(long l) {
  451.     return append(String.valueOf(l));
  452.     }
  453.  
  454.     /**
  455.      * Appends the string representation of the <code>float</code> 
  456.      * argument to this string buffer. 
  457.      * <p>
  458.      * The argument is converted to a string as if by the method 
  459.      * <code>String.valueOf</code>, and the characters of that 
  460.      * string are then appended to this string buffer. 
  461.      *
  462.      * @param   f   a <code>float</code>.
  463.      * @return  this string buffer.
  464.      * @see     java.lang.String#valueOf(float)
  465.      * @see     java.lang.StringBuffer#append(java.lang.String)
  466.      */
  467.     public StringBuffer append(float f) {
  468.     return append(String.valueOf(f));
  469.     }
  470.  
  471.     /**
  472.      * Appends the string representation of the <code>double</code> 
  473.      * argument to this string buffer. 
  474.      * <p>
  475.      * The argument is converted to a string as if by the method 
  476.      * <code>String.valueOf</code>, and the characters of that 
  477.      * string are then appended to this string buffer. 
  478.      *
  479.      * @param   d   a <code>double</code>.
  480.      * @return  this string buffer.
  481.      * @see     java.lang.String#valueOf(double)
  482.      * @see     java.lang.StringBuffer#append(java.lang.String)
  483.      */
  484.     public StringBuffer append(double d) {
  485.     return append(String.valueOf(d));
  486.     }
  487.  
  488.     /**
  489.      * Inserts the string representation of the <code>Object</code> 
  490.      * argument into this string buffer. 
  491.      * <p>
  492.      * The second argument is converted to a string as if by the method 
  493.      * <code>String.valueOf</code>, and the characters of that 
  494.      * string are then inserted into this string buffer at the indicated 
  495.      * offset. 
  496.      * <p>
  497.      * The offset argument must be greater than or equal to 
  498.      * <code>0</code>, and less than or equal to the length of this 
  499.      * string buffer. 
  500.      *
  501.      * @param      offset   the offset.
  502.      * @param      b        an <code>Object</code>.
  503.      * @return     this string buffer.
  504.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  505.      * @see        java.lang.String#valueOf(java.lang.Object)
  506.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  507.      * @see        java.lang.StringBuffer#length()
  508.      */
  509.     public synchronized StringBuffer insert(int offset, Object obj) {
  510.     return insert(offset, String.valueOf(obj));
  511.     }
  512.  
  513.     /**
  514.      * Inserts the string into this string buffer. 
  515.      * <p>
  516.      * The characters of the <code>String</code> argument are inserted, in 
  517.      * order, into this string buffer at the indicated offset. The length 
  518.      * of this string buffer is increased by the length of the argument. 
  519.      * <p>
  520.      * The offset argument must be greater than or equal to 
  521.      * <code>0</code>, and less than or equal to the length of this 
  522.      * string buffer. 
  523.      *
  524.      * @param      offset   the offset.
  525.      * @param      str      a string.
  526.      * @return     this string buffer.
  527.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  528.      * @see        java.lang.StringBuffer#length()
  529.      */
  530.     public synchronized StringBuffer insert(int offset, String str) {
  531.     if ((offset < 0) || (offset > count)) {
  532.         throw new StringIndexOutOfBoundsException();
  533.     }
  534.  
  535.     if (str == null) {
  536.         str = String.valueOf(str);
  537.     }
  538.     int len = str.length();
  539.     int newcount = count + len;
  540.     if (newcount > value.length)
  541.         expandCapacity(newcount);
  542.     else if (shared)
  543.         copy();
  544.     System.arraycopy(value, offset, value, offset + len, count - offset);
  545.     str.getChars(0, len, value, offset);
  546.     count = newcount;
  547.     return this;
  548.     }
  549.  
  550.     /**
  551.      * Inserts the string representation of the <code>char</code> array 
  552.      * argument into this string buffer. 
  553.      * <p>
  554.      * The characters of the array argument are inserted into the 
  555.      * contents of this string buffer at the position indicated by 
  556.      * <code>offset</code>. The length of this string buffer increases by 
  557.      * the length of the argument. 
  558.      *
  559.      * @param      offset   the offset.
  560.      * @param      ch       a character array.
  561.      * @return     this string buffer.
  562.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  563.      */
  564.     public synchronized StringBuffer insert(int offset, char str[]) {
  565.     if ((offset < 0) || (offset > count)) {
  566.         throw new StringIndexOutOfBoundsException();
  567.     }
  568.     int len = str.length;
  569.     int newcount = count + len;
  570.     if (newcount > value.length)
  571.         expandCapacity(newcount);
  572.     else if (shared)
  573.         copy();
  574.     System.arraycopy(value, offset, value, offset + len, count - offset);
  575.     System.arraycopy(str, 0, value, offset, len);
  576.     count = newcount;
  577.     return this;
  578.     }
  579.  
  580.     /**
  581.      * Inserts the string representation of the <code>boolean</code> 
  582.      * argument into this string buffer. 
  583.      * <p>
  584.      * The second argument is converted to a string as if by the method 
  585.      * <code>String.valueOf</code>, and the characters of that 
  586.      * string are then inserted into this string buffer at the indicated 
  587.      * offset. 
  588.      * <p>
  589.      * The offset argument must be greater than or equal to 
  590.      * <code>0</code>, and less than or equal to the length of this 
  591.      * string buffer. 
  592.      *
  593.      * @param      offset   the offset.
  594.      * @param      b        a <code>boolean</code>.
  595.      * @return     this string buffer.
  596.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  597.      * @see        java.lang.String#valueOf(boolean)
  598.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  599.      * @see        java.lang.StringBuffer#length()
  600.      */
  601.     public StringBuffer insert(int offset, boolean b) {
  602.     return insert(offset, String.valueOf(b));
  603.     }
  604.  
  605.     /**
  606.      * Inserts the string representation of the <code>char</code> 
  607.      * argument into this string buffer. 
  608.      * <p>
  609.      * The second argument is inserted into the contents of this string 
  610.      * buffer at the position indicated by <code>offset</code>. The length 
  611.      * of this string buffer increases by one. 
  612.      * <p>
  613.      * The offset argument must be greater than or equal to 
  614.      * <code>0</code>, and less than or equal to the length of this 
  615.      * string buffer. 
  616.      *
  617.      * @param      offset   the offset.
  618.      * @param      ch       a <code>char</code>.
  619.      * @return     this string buffer.
  620.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  621.      * @see        java.lang.StringBuffer#length()
  622.      */
  623.     public synchronized StringBuffer insert(int offset, char c) {
  624.     int newcount = count + 1;
  625.     if (newcount > value.length)
  626.         expandCapacity(newcount);
  627.     else if (shared)
  628.         copy();
  629.     System.arraycopy(value, offset, value, offset + 1, count - offset);
  630.     value[offset] = c;
  631.     count = newcount;
  632.     return this;
  633.     }
  634.  
  635.     /**
  636.      * Inserts the string representation of the second <code>int</code> 
  637.      * argument into this string buffer. 
  638.      * <p>
  639.      * The second argument is converted to a string as if by the method 
  640.      * <code>String.valueOf</code>, and the characters of that 
  641.      * string are then inserted into this string buffer at the indicated 
  642.      * offset. 
  643.      * <p>
  644.      * The offset argument must be greater than or equal to 
  645.      * <code>0</code>, and less than or equal to the length of this 
  646.      * string buffer. 
  647.      *
  648.      * @param      offset   the offset.
  649.      * @param      b        an <code>int</code>.
  650.      * @return     this string buffer.
  651.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  652.      * @see        java.lang.String#valueOf(int)
  653.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  654.      * @see        java.lang.StringBuffer#length()
  655.      */
  656.     public StringBuffer insert(int offset, int i) {
  657.     return insert(offset, String.valueOf(i));
  658.     }
  659.  
  660.     /**
  661.      * Inserts the string representation of the <code>long</code> 
  662.      * argument into this string buffer. 
  663.      * <p>
  664.      * The second argument is converted to a string as if by the method 
  665.      * <code>String.valueOf</code>, and the characters of that 
  666.      * string are then inserted into this string buffer at the indicated 
  667.      * offset. 
  668.      * <p>
  669.      * The offset argument must be greater than or equal to 
  670.      * <code>0</code>, and less than or equal to the length of this 
  671.      * string buffer. 
  672.      *
  673.      * @param      offset   the offset.
  674.      * @param      b        a <code>long</code>.
  675.      * @return     this string buffer.
  676.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  677.      * @see        java.lang.String#valueOf(long)
  678.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  679.      * @see        java.lang.StringBuffer#length()
  680.      */
  681.     public StringBuffer insert(int offset, long l) {
  682.     return insert(offset, String.valueOf(l));
  683.     }
  684.  
  685.     /**
  686.      * Inserts the string representation of the <code>float</code> 
  687.      * argument into this string buffer. 
  688.      * <p>
  689.      * The second argument is converted to a string as if by the method 
  690.      * <code>String.valueOf</code>, and the characters of that 
  691.      * string are then inserted into this string buffer at the indicated 
  692.      * offset. 
  693.      * <p>
  694.      * The offset argument must be greater than or equal to 
  695.      * <code>0</code>, and less than or equal to the length of this 
  696.      * string buffer. 
  697.      *
  698.      * @param      offset   the offset.
  699.      * @param      b        a <code>float</code>.
  700.      * @return     this string buffer.
  701.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  702.      * @see        java.lang.String#valueOf(float)
  703.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  704.      * @see        java.lang.StringBuffer#length()
  705.      */
  706.     public StringBuffer insert(int offset, float f) {
  707.     return insert(offset, String.valueOf(f));
  708.     }
  709.  
  710.     /**
  711.      * Inserts the string representation of the <code>double</code> 
  712.      * argument into this string buffer. 
  713.      * <p>
  714.      * The second argument is converted to a string as if by the method 
  715.      * <code>String.valueOf</code>, and the characters of that 
  716.      * string are then inserted into this string buffer at the indicated 
  717.      * offset. 
  718.      * <p>
  719.      * The offset argument must be greater than or equal to 
  720.      * <code>0</code>, and less than or equal to the length of this 
  721.      * string buffer. 
  722.      *
  723.      * @param      offset   the offset.
  724.      * @param      b        a <code>double</code>.
  725.      * @return     this string buffer.
  726.      * @exception  StringIndexOutOfBoundsException  if the offset is invalid.
  727.      * @see        java.lang.String#valueOf(double)
  728.      * @see        java.lang.StringBuffer#insert(int, java.lang.String)
  729.      * @see        java.lang.StringBuffer#length()
  730.      */
  731.     public StringBuffer insert(int offset, double d) {
  732.     return insert(offset, String.valueOf(d));
  733.     }
  734.  
  735.     /**
  736.      * The character sequence contained in this string buffer is 
  737.      * replaced by the reverse of the sequence. 
  738.      *
  739.      * @return  this string buffer.
  740.      * @since   JDK1.0.2
  741.      */
  742.     public synchronized StringBuffer reverse() {
  743.     if (shared) copy();
  744.     int n = count - 1;
  745.     for (int j = (n-1) >> 1; j >= 0; --j) {
  746.         char temp = value[j];
  747.         value[j] = value[n - j];
  748.         value[n - j] = temp;
  749.     }
  750.     return this;
  751.     }
  752.  
  753.     /**
  754.      * Converts to a string representing the data in this string buffer.
  755.      * A new <code>String</code> object is allocated and initialized to 
  756.      * contain the character sequence currently represented by this 
  757.      * string buffer. This <code>String</code> is then returned. Subsequent 
  758.      * changes to the string buffer do not affect the contents of the 
  759.      * <code>String</code>. 
  760.      *
  761.      * @return  a string representation of the string buffer.
  762.      */
  763.     public String toString() {
  764.     return new String(this);
  765.     }
  766.  
  767.     //
  768.     // The following two methods are needed by String to efficiently
  769.     // convert a StringBuffer into a String.  They are not public.
  770.     // They shouldn't be called by anyone but String.
  771.     final void setShared() { shared = true; } 
  772.     final char[] getValue() { return value; }
  773. }
  774.