File: java.lang.StringBuffer

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (261 lines) | stat: -rw-r--r-- 5,256 bytes parent folder | download | duplicates (3)
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
// java.lang.StringBuffer -- an implementation of the Java Language Spec 20.13
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.lang;

public class StringBuffer {

	// CONSTRUCTORS
	public StringBuffer()
	{
		this(16);
	}

	public StringBuffer(int len) throws NegativeArraySizeException
	{
		data=new char[len];
		curlen=0;
		shared=false;
	}

	public StringBuffer(String s)
	{
		curlen=s.length();
		data=new char[curlen+16];
		s.getChars(0, curlen, data, 0);
		shared=false;
	}

	// PRIVATE
	private char[] data;
	private int curlen;
	private boolean shared;

	// For use only by String!
	// The contents of the returned array should NOT be modified.
	char[] getPrivateData()
	{
		shared=true;
		return data;
	}
	int getPrivateStart()
	{
		return 0;
	}

	// PUBLIC INTERFACE
	public String toString()
	{
		return new String(this);
	}

	public int length()
	{
		return curlen;
	}

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

	public synchronized void ensureCapacity(int cap)
	{
		if ((cap<=0 || data.length>=cap) && !shared) return;
		int newlen=data.length;
		if (cap>data.length) newlen=newlen*2+2;
		if (cap>newlen) newlen=cap;
		char[] newdata=new char[newlen];
		for (int i=0; i<curlen; i++) {
			newdata[i]=data[i];
		}
		data=newdata;
	}

	public synchronized void setLength(int newlen)
			throws IndexOutOfBoundsException
	{
		if (newlen<0) throw new IndexOutOfBoundsException();
		ensureCapacity(newlen);
		for (int i=curlen; i<newlen; i++) {
			data[i]=(char) 0;
		}
		curlen=newlen;
	}

	public synchronized char charAt(int index)
			throws IndexOutOfBoundsException
	{
		if (index<0 || index>=curlen)
			throw new IndexOutOfBoundsException();
		return data[index];
	}

	public synchronized void setCharAt(int index, char ch)
			throws IndexOutOfBoundsException
	{
		if (index<0 || index>=curlen)
			throw new IndexOutOfBoundsException();
		ensureCapacity(curlen);
		data[index]=ch;
	}

	public synchronized void getChars(int sb, int se, char[] d, int db)
			throws NullPointerException, IndexOutOfBoundsException
	{
		if (db+se-sb>d.length || sb<0 || sb>se || se>curlen)
			throw new IndexOutOfBoundsException();
		while (sb<se) {
			d[db++]=data[sb++];
		}
	}

	public StringBuffer append(Object o)
	{
		return insert(curlen, o);
	}

	public StringBuffer append(String s)
	{
		return insert(curlen, s);
	}

	public StringBuffer append(char[] cs) throws NullPointerException
	{
		return insert(curlen, cs);
	}

	public StringBuffer append(char[] cs, int off, int len)
			throws NullPointerException, IndexOutOfBoundsException
	{
		return insert(curlen, cs, off, len);
	}

	public StringBuffer append(boolean b)
	{
		return insert(curlen, b);
	}

	public StringBuffer append(char c)
	{
		return insert(curlen, c);
	}

	public StringBuffer append(int i)
	{
		return insert(curlen, i);
	}

	public StringBuffer append(long l)
	{
		return insert(curlen, l);
	}

//	public StringBuffer append(float f)
//	{
//		return insert(curlen, f);
//	}

//	public StringBuffer append(double d)
//	{
//		return insert(curlen, d);
//	}

	private void insertSpace(int off, int space)
	{
		ensureCapacity(curlen+space);
		for (int i=curlen-1; i>=off; i--) {
			data[space+i]=data[i];
		}
		curlen+=space;
	}

	public StringBuffer insert(int off, Object o)
			throws IndexOutOfBoundsException
	{
		insert(off, String.valueOf(o));
		return this;
	}

	public synchronized StringBuffer insert(int off, String s)
			throws NullPointerException, IndexOutOfBoundsException
	{
		if (off<0 || off>curlen)
			throw new IndexOutOfBoundsException();
		if (s==null) s="null";
		insertSpace(off, s.length());
		for (int i=0; i<s.length(); i++) {
			data[off+i]=s.charAt(i);
		}
		return this;
	}

	public StringBuffer insert(int off, char[] cs)
			throws NullPointerException
	{
		return insert(off, cs, 0, cs.length);
	}

	// Oddly enough, the following method isn't in the standard API...
	private synchronized StringBuffer insert(int off, char[] cs, int coff, int len)
			throws NullPointerException, IndexOutOfBoundsException
	{
		if (off<0 || coff<0 || len<0
		    || off+len>curlen || coff+len>cs.length)
			throw new IndexOutOfBoundsException();
		insertSpace(off, len);
		for (int i=0; i<len; i++) {
			data[off+i]=cs[coff+i];
		}
		return this;
	}

	public StringBuffer insert(int off, boolean b)
	{
		return insert(off, String.valueOf(b));
	}

	public synchronized StringBuffer insert(int off, char c)
	{
		insertSpace(off, 1);
		data[off]=c;
		return this;
	}

	public StringBuffer insert(int off, int i)
	{
		// FIXME: make this more efficient
		return insert(off, String.valueOf(i));
	}

	public StringBuffer insert(int off, long l)
	{
		// FIXME: make this more efficient
		return insert(off, String.valueOf(l));
	}

//	public StringBuffer insert(int off, float f)
//	{
//		// FIXME: make this more efficient
//		return insert(off, String.valueOf(f));
//	}

//	public StringBuffer insert(int off, double d)
//	{
//		// FIXME: make this more efficient
//		return insert(off, String.valueOf(d));
//	}

	public synchronized StringBuffer reverse()
	{
		ensureCapacity(curlen);
		int low=0, high=curlen-1;
		while (low<high) {
			char temp=data[high];
			data[high]=data[low];
			data[low]=temp;
		}
		return this;
	}

}