aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/StringBuffer.java
blob: 4127d54787f4953d785412e525be7bb0622dad62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// StringBuffer.java - Growable strings.

/* Copyright (C) 1998, 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.lang;
import java.io.Serializable;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date October 23, 1998.  
 */

/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 */

public final class StringBuffer implements Serializable
{
  public StringBuffer append (boolean bool)
    {
      return append (String.valueOf(bool));
    }

  public synchronized StringBuffer append (char ch)
    {
      ensureCapacity (count + 1);
      value[count++] = ch;
      return this;
    }

  public StringBuffer append (int inum)
    {
      return append (String.valueOf(inum));
    }

  public StringBuffer append (long lnum)
    {
      return append (String.valueOf(lnum));
    }

  public StringBuffer append (float fnum)
    {
      return append (String.valueOf(fnum));
    }

  public StringBuffer append (double dnum)
    {
      return append (String.valueOf(dnum));
    }

  public StringBuffer append (Object obj)
    {
      return append (String.valueOf(obj));
    }

  public synchronized StringBuffer append (String str)
    {
      if (str == null)
	str = "null";
      int len = str.length();
      ensureCapacity (count + len);
      str.getChars(0, len, value, count);
      count += len;
      return this;
    }

  public StringBuffer append (char[] data)
    {
      return append (data, 0, data.length);
    }

  public synchronized StringBuffer append (char[] data, int offset, int count)
    {
      ensureCapacity (this.count + count);
      System.arraycopy(data, offset, value, this.count, count);
      this.count += count;
      return this;
    } 

  public int capacity ()
    {
      return value.length;
    }

  public synchronized char charAt (int index)
    {
      if (index >= count)
	throw new StringIndexOutOfBoundsException (index);
      return value[index];
    }

  public synchronized void ensureCapacity (int minimumCapacity)
    {
      if (shared || minimumCapacity > value.length)
	{
	  minimumCapacity = Math.max(minimumCapacity, value.length*2+2);
	  char[] nb = new char[minimumCapacity];
	  System.arraycopy(value, 0, nb, 0, count);
	  value = nb;
	  shared = false;
	}
    }

  public synchronized void getChars (int srcOffset, int srcEnd,
				     char[] dst, int dstOffset)
    {
      if (srcOffset < 0 || srcOffset > srcEnd)
	throw new StringIndexOutOfBoundsException (srcOffset);
      int todo = srcEnd - srcOffset;
      if (srcEnd > count || dstOffset + todo > count)
	throw new StringIndexOutOfBoundsException (srcEnd);
      System.arraycopy(value, srcOffset, dst, dstOffset, todo);
    }

  public StringBuffer insert (int offset, boolean bool)
    {
      return insert (offset, bool ? "true" : "false");
    }

  public synchronized StringBuffer insert (int offset, char ch)
    {
      if (offset < 0 || offset > count)
	throw new StringIndexOutOfBoundsException (offset);
      ensureCapacity (count+1);
      System.arraycopy(value, offset, value, offset+1, count-offset);
      value[offset] = ch;
      count++;
      return this;
    }

  public StringBuffer insert (int offset, int inum)
    {
      return insert (offset, String.valueOf(inum));
    }

  public StringBuffer insert (int offset, long lnum)
    {
      return insert (offset, String.valueOf(lnum));
    }

  public StringBuffer insert (int offset, float fnum)
    {
      return insert (offset, String.valueOf(fnum));
    }

  public StringBuffer insert (int offset, double dnum)
    {
      return insert (offset, String.valueOf(dnum));
    }

  public StringBuffer insert (int offset, Object obj)
    {
      return insert (offset, String.valueOf(obj));
    }

  public synchronized StringBuffer insert (int offset, String str)
    {
      if (offset < 0 || offset > count)
	throw new StringIndexOutOfBoundsException (offset);
      // Note that using `null' is from JDK 1.2.
      if (str == null)
	str = "null";
      int len = str.length();
      ensureCapacity(count+len);
      System.arraycopy(value, offset, value, offset+len, count-offset);
      str.getChars(0, len, value, offset);
      count += len;
      return this;
    }

  public synchronized StringBuffer insert (int offset, char[] data)
    {
      if (offset < 0 || offset > count)
	throw new StringIndexOutOfBoundsException (offset);
      int len = data.length;
      ensureCapacity (count+len);
      System.arraycopy(value, offset, value, offset+len, count-offset);
      System.arraycopy(data, 0, value, offset, len);
      count += len;
      return this;
    }

  public int length ()
    {
      return count;
    }

  public synchronized StringBuffer reverse ()
    {
      for (int i = 0; i < count / 2; ++i)
	{
	  char c = value[i];
	  value[i] = value[count - i - 1];
	  value[count - i - 1] = c;
	}
      return this;
    }

  public synchronized void setCharAt (int index, char ch)
    {
      if (index < 0 || index >= count)
	throw new StringIndexOutOfBoundsException (index);
      // Call ensureCapacity to enforce copy-on-write.
      ensureCapacity (count);
      value[index] = ch;
    }

  public synchronized void setLength (int newLength)
    {
      if (newLength < 0)
	throw new StringIndexOutOfBoundsException (newLength);

      ensureCapacity (newLength);
      for (int i = count; i < newLength; ++i)
	value[i] = '\0';
      count = newLength;
    }

  public StringBuffer ()
    {
      this (16);
    }

  public StringBuffer (int capacity)
    {
      count = 0;
      value = new char[capacity];
      shared = false;
    }

  public StringBuffer (String str)
    {
      // Note: nowhere does it say that we should handle a null
      // argument here.  In fact, the JCL implies that we should not.
      // But this leads to an asymmetry: `null + ""' will fail, while
      // `"" + null' will work.
      if (str == null)
	str = "null";
      count = str.length();
      // JLS: The initial capacity of the string buffer is 16 plus the
      // length of the argument string.
      value = new char[count + 16];
      str.getChars(0, count, value, 0);
      shared = false;
    }

  public String toString ()
    {
      shared = true;
      return new String (this);
    }

  // The buffer.  Note that this has permissions set this way so that
  // String can get the value.
  char[] value;

  // Index of next available character.  Note that this has
  // permissions set this way so that String can get the value.
  int count;

  // True if we need to copy the buffer before writing to it again.
  // FIXME: JDK 1.2 doesn't specify this.  The new buffer-growing
  // semantics make this less useful in that case, too.
  private boolean shared;
}