2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / runtime / StringBuffer.java
blobea2ff3e5aa02eae9d25dc3e9b5798d1811ec3ff7
1 // This is a simplified copy of java.lang.StringBuffer with
2 // `synchronized' removed.
4 /* StringBuffer.java -- Growable strings
5 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
7 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
41 package gnu.gcj.runtime;
43 public final class StringBuffer
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)
52 public StringBuffer append (boolean bool)
54 return append (bool ? "true" : "false");
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>.
61 public StringBuffer append (char ch)
63 ensureCapacity_unsynchronized (count + 1);
64 value[count++] = ch;
65 return this;
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)
75 public native StringBuffer append (int inum);
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)
84 public StringBuffer append (long lnum)
86 return append (Long.toString (lnum));
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)
96 public StringBuffer append (float fnum)
98 return append (Float.toString (fnum));
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)
108 public StringBuffer append (double dnum)
110 return append (Double.toString (dnum));
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)
120 public StringBuffer append (Object obj)
122 return append (String.valueOf(obj));
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>.
129 public StringBuffer append (String str)
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;
140 private void ensureCapacity_unsynchronized (int minimumCapacity)
142 if (minimumCapacity > value.length)
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;
151 /** Create a new StringBuffer with default capacity 16.
152 * @see JLS 20.13.1
154 public StringBuffer ()
156 this (DEFAULT_CAPACITY);
159 /** Create an empty <code>StringBuffer</code> with the specified initial capacity.
160 * @param capacity the initial capacity.
162 public StringBuffer (int capacity)
164 count = 0;
165 value = new char[capacity];
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.
172 public StringBuffer (String str)
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);
183 /** Convert this <code>StringBuffer</code> to a <code>String</code>.
184 * @return the characters in this StringBuffer
186 // This is native because efficient implementation requires avoiding
187 // the Java protection mechanism.
188 public native String toString ();
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;
194 // The buffer. Note that this has permissions set this way so that
195 // String can get the value.
196 char[] value;
198 private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1