1 // This is a simplified copy of java.lang.StringBuffer with
2 // `synchronized' removed.
3 
4 /* StringBuffer.java -- Growable strings
5    Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
6 
7 This file is part of GNU Classpath.
8 
9 GNU Classpath is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13 
14 GNU Classpath is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GNU Classpath; see the file COPYING.  If not, write to the
21 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA.
23 
24 Linking this library statically or dynamically with other modules is
25 making a combined work based on this library.  Thus, the terms and
26 conditions of the GNU General Public License cover the whole
27 combination.
28 
29 As a special exception, the copyright holders of this library give you
30 permission to link this library with independent modules to produce an
31 executable, regardless of the license terms of these independent
32 modules, and to copy and distribute the resulting executable under
33 terms of your choice, provided that you also meet, for each linked
34 independent module, the terms and conditions of the license of that
35 module.  An independent module is a module which is not derived from
36 or based on this library.  If you modify this library, you may extend
37 this exception to your version of the library, but you are not
38 obligated to do so.  If you do not wish to do so, delete this
39 exception statement from your version. */
40 
41 package gnu.gcj.runtime;
42 
43 public final class StringBuffer
44 {
45   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
46    *  Uses <code>String.valueOf()</code> to convert to
47    *  <code>String</code>.
48    *  @param bool the <code>boolean</code> to convert and append.
49    *  @return this <code>StringBuffer</code>.
50    *  @see java.lang.String#valueOf(boolean)
51    */
append(boolean bool)52   public StringBuffer append (boolean bool)
53   {
54     return append (bool ? "true" : "false");
55   }
56 
57   /** Append the <code>char</code> to this <code>StringBuffer</code>.
58    *  @param c the <code>char</code> to append.
59    *  @return this <code>StringBuffer</code>.
60    */
append(char ch)61   public StringBuffer append (char ch)
62   {
63     ensureCapacity_unsynchronized (count + 1);
64     value[count++] = ch;
65     return this;
66   }
67 
68   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
69    *  Uses <code>String.valueOf()</code> to convert to
70    *  <code>String</code>.
71    *  @param inum the <code>int</code> to convert and append.
72    *  @return this <code>StringBuffer</code>.
73    *  @see java.lang.String#valueOf(int)
74    */
append(int inum)75   public native StringBuffer append (int inum);
76 
77   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
78    *  Uses <code>String.valueOf()</code> to convert to
79    *  <code>String</code>.
80    *  @param lnum the <code>long</code> to convert and append.
81    *  @return this <code>StringBuffer</code>.
82    *  @see java.lang.String#valueOf(long)
83    */
append(long lnum)84   public StringBuffer append (long lnum)
85   {
86     return append (Long.toString (lnum));
87   }
88 
89   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
90    *  Uses <code>String.valueOf()</code> to convert to
91    *  <code>String</code>.
92    *  @param fnum the <code>float</code> to convert and append.
93    *  @return this <code>StringBuffer</code>.
94    *  @see java.lang.String#valueOf(float)
95    */
append(float fnum)96   public StringBuffer append (float fnum)
97   {
98     return append (Float.toString (fnum));
99   }
100 
101   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
102    *  Uses <code>String.valueOf()</code> to convert to
103    *  <code>String</code>.
104    *  @param dnum the <code>double</code> to convert and append.
105    *  @return this <code>StringBuffer</code>.
106    *  @see java.lang.String#valueOf(double)
107    */
append(double dnum)108   public StringBuffer append (double dnum)
109   {
110     return append (Double.toString (dnum));
111   }
112 
113   /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>.
114    *  Uses <code>String.valueOf()</code> to convert to
115    *  <code>String</code>.
116    *  @param obj the <code>Object</code> to convert and append.
117    *  @return this <code>StringBuffer</code>.
118    *  @see java.lang.String#valueOf(java.lang.Object)
119    */
append(Object obj)120   public StringBuffer append (Object obj)
121   {
122     return append (String.valueOf(obj));
123   }
124 
125   /** Append the <code>String</code> to this <code>StringBuffer</code>.
126    *  @param str the <code>String</code> to append.
127    *  @return this <code>StringBuffer</code>.
128    */
append(String str)129   public StringBuffer append (String str)
130   {
131     if (str == null)
132       str = "null";
133     int len = str.length();
134     ensureCapacity_unsynchronized (count + len);
135     str.getChars(0, len, value, count);
136     count += len;
137     return this;
138   }
139 
ensureCapacity_unsynchronized(int minimumCapacity)140   private void ensureCapacity_unsynchronized (int minimumCapacity)
141   {
142     if (minimumCapacity > value.length)
143       {
144 	minimumCapacity = Math.max (minimumCapacity, value.length * 2 + 2);
145 	char[] nb = new char[minimumCapacity];
146 	System.arraycopy(value, 0, nb, 0, count);
147 	value = nb;
148       }
149   }
150 
151   /** Create a new StringBuffer with default capacity 16.
152    *  @see JLS 20.13.1
153    */
StringBuffer()154   public StringBuffer ()
155   {
156     this (DEFAULT_CAPACITY);
157   }
158 
159   /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
160    *  @param capacity the initial capacity.
161    */
StringBuffer(int capacity)162   public StringBuffer (int capacity)
163   {
164     count = 0;
165     value = new char[capacity];
166   }
167 
168   /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>.
169    *  Initial capacity will be the size of the String plus 16.
170    *  @param str the <code>String</code> to make a <code>StringBuffer</code> out of.
171    */
StringBuffer(String str)172   public StringBuffer (String str)
173   {
174     if (str == null)
175       str = "null";
176     count = str.length();
177     // JLS: The initial capacity of the string buffer is 16 plus the
178     // length of the argument string.
179     value = new char[count + DEFAULT_CAPACITY];
180     str.getChars(0, count, value, 0);
181   }
182 
183   /** Convert this <code>StringBuffer</code> to a <code>String</code>.
184    *  @return the characters in this StringBuffer
185    */
186   // This is native because efficient implementation requires avoiding
187   // the Java protection mechanism.
toString()188   public native String toString ();
189 
190   // Index of next available character.  Note that this has
191   // permissions set this way so that String can get the value.
192   int count;
193 
194   // The buffer.  Note that this has permissions set this way so that
195   // String can get the value.
196   char[] value;
197 
198   private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1
199 }
200