1 // StringBuffer.java - Growable strings.
3 /* Copyright (C) 1998, 1999, 2000 Red Hat
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java.io.Serializable;
15 * @author Tom Tromey <tromey@cygnus.com>
16 * @date October 23, 1998.
19 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
22 public final class StringBuffer implements Serializable
24 public StringBuffer append (boolean bool)
26 return append (String.valueOf(bool));
29 public synchronized StringBuffer append (char ch)
31 ensureCapacity (count + 1);
36 public StringBuffer append (int inum)
38 return append (String.valueOf(inum));
41 public StringBuffer append (long lnum)
43 return append (String.valueOf(lnum));
46 public StringBuffer append (float fnum)
48 return append (String.valueOf(fnum));
51 public StringBuffer append (double dnum)
53 return append (String.valueOf(dnum));
56 public StringBuffer append (Object obj)
58 return append (String.valueOf(obj));
61 public synchronized StringBuffer append (String str)
65 int len = str.length();
66 ensureCapacity (count + len);
67 str.getChars(0, len, value, count);
72 public StringBuffer append (char[] data)
74 return append (data, 0, data.length);
77 public synchronized StringBuffer append (char[] data, int offset, int count)
79 ensureCapacity (this.count + count);
80 System.arraycopy(data, offset, value, this.count, count);
85 public int capacity ()
90 public synchronized char charAt (int index)
93 throw new StringIndexOutOfBoundsException (index);
97 public synchronized void ensureCapacity (int minimumCapacity)
99 if (shared || minimumCapacity > value.length)
101 // We don't want to make a larger vector when `shared' is
102 // set. If we do, then setLength becomes very inefficient
103 // when repeatedly reusing a StringBuffer in a loop.
104 int max = (minimumCapacity > value.length
107 minimumCapacity = Math.max(minimumCapacity, max);
108 char[] nb = new char[minimumCapacity];
109 System.arraycopy(value, 0, nb, 0, count);
115 public synchronized void getChars (int srcOffset, int srcEnd,
116 char[] dst, int dstOffset)
118 if (srcOffset < 0 || srcOffset > srcEnd)
119 throw new StringIndexOutOfBoundsException (srcOffset);
120 int todo = srcEnd - srcOffset;
121 if (srcEnd > count || dstOffset + todo > count)
122 throw new StringIndexOutOfBoundsException (srcEnd);
123 System.arraycopy(value, srcOffset, dst, dstOffset, todo);
126 public StringBuffer insert (int offset, boolean bool)
128 return insert (offset, bool ? "true" : "false");
131 public synchronized StringBuffer insert (int offset, char ch)
133 if (offset < 0 || offset > count)
134 throw new StringIndexOutOfBoundsException (offset);
135 ensureCapacity (count+1);
136 System.arraycopy(value, offset, value, offset+1, count-offset);
142 public StringBuffer insert (int offset, int inum)
144 return insert (offset, String.valueOf(inum));
147 public StringBuffer insert (int offset, long lnum)
149 return insert (offset, String.valueOf(lnum));
152 public StringBuffer insert (int offset, float fnum)
154 return insert (offset, String.valueOf(fnum));
157 public StringBuffer insert (int offset, double dnum)
159 return insert (offset, String.valueOf(dnum));
162 public StringBuffer insert (int offset, Object obj)
164 return insert (offset, String.valueOf(obj));
167 public synchronized StringBuffer insert (int offset, String str)
169 if (offset < 0 || offset > count)
170 throw new StringIndexOutOfBoundsException (offset);
171 // Note that using `null' is from JDK 1.2.
174 int len = str.length();
175 ensureCapacity(count+len);
176 System.arraycopy(value, offset, value, offset+len, count-offset);
177 str.getChars(0, len, value, offset);
182 public synchronized StringBuffer insert (int offset, char[] data)
184 if (offset < 0 || offset > count)
185 throw new StringIndexOutOfBoundsException (offset);
186 int len = data.length;
187 ensureCapacity (count+len);
188 System.arraycopy(value, offset, value, offset+len, count-offset);
189 System.arraycopy(data, 0, value, offset, len);
199 public synchronized StringBuffer reverse ()
201 for (int i = 0; i < count / 2; ++i)
204 value[i] = value[count - i - 1];
205 value[count - i - 1] = c;
210 public synchronized void setCharAt (int index, char ch)
212 if (index < 0 || index >= count)
213 throw new StringIndexOutOfBoundsException (index);
214 // Call ensureCapacity to enforce copy-on-write.
215 ensureCapacity (count);
219 public synchronized void setLength (int newLength)
222 throw new StringIndexOutOfBoundsException (newLength);
224 ensureCapacity (newLength);
225 for (int i = count; i < newLength; ++i)
230 public StringBuffer ()
235 public StringBuffer (int capacity)
238 value = new char[capacity];
242 public StringBuffer (String str)
244 // The documentation is not clear, but experimentation with
245 // other implementations indicates that StringBuffer(null)
246 // should throw a NullPointerException.
247 count = str.length();
248 // JLS: The initial capacity of the string buffer is 16 plus the
249 // length of the argument string.
250 value = new char[count + 16];
251 str.getChars(0, count, value, 0);
255 public String toString ()
258 return new String (this);
261 // The buffer. Note that this has permissions set this way so that
262 // String can get the value.
265 // Index of next available character. Note that this has
266 // permissions set this way so that String can get the value.
269 // True if we need to copy the buffer before writing to it again.
270 // FIXME: JDK 1.2 doesn't specify this. The new buffer-growing
271 // semantics make this less useful in that case, too.
272 private boolean shared;