|
1
|
|
/*
|
|
2
|
|
* JBind
|
|
3
|
|
*
|
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
|
5
|
|
*
|
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
|
8
|
|
* 'Apache Software License V 1.1'.
|
|
9
|
|
*/
|
|
10
|
|
package org.jbind.util.other;
|
|
11
|
|
|
|
12
|
|
/**
|
|
13
|
|
* An unsynchornized copy of the java.lang.StrBuffer class.
|
|
14
|
|
*/
|
|
15
|
|
public class StrBuffer implements java.io.Serializable {
|
|
16
|
|
|
|
17
|
|
private char value[];
|
|
18
|
|
|
|
19
|
|
private int count;
|
|
20
|
|
|
|
21
|
|
private boolean shared;
|
|
22
|
|
|
|
23
|
|
static final long serialVersionUID = 3388685877147921107L;
|
|
24
|
|
|
|
25
|
2536
|
public StrBuffer() {
|
|
26
|
2536
|
this(16);
|
|
27
|
|
}
|
|
28
|
|
|
|
29
|
145710
|
public StrBuffer(int length) {
|
|
30
|
145710
|
value = new char[length];
|
|
31
|
145710
|
shared = false;
|
|
32
|
|
}
|
|
33
|
|
|
|
34
|
16160
|
public StrBuffer(String str) {
|
|
35
|
16160
|
this(str.length() + 16);
|
|
36
|
16160
|
append(str);
|
|
37
|
|
}
|
|
38
|
|
|
|
39
|
48663
|
public int length() {
|
|
40
|
48663
|
return count;
|
|
41
|
|
}
|
|
42
|
|
|
|
43
|
0
|
public int capacity() {
|
|
44
|
0
|
return value.length;
|
|
45
|
|
}
|
|
46
|
|
|
|
47
|
0
|
private final void copy() {
|
|
48
|
0
|
char newValue[] = new char[value.length];
|
|
49
|
0
|
System.arraycopy(value, 0, newValue, 0, count);
|
|
50
|
0
|
value = newValue;
|
|
51
|
0
|
shared = false;
|
|
52
|
|
}
|
|
53
|
|
|
|
54
|
0
|
public void ensureCapacity(int minimumCapacity) {
|
|
55
|
0
|
if (minimumCapacity > value.length) {
|
|
56
|
0
|
expandCapacity(minimumCapacity);
|
|
57
|
|
}
|
|
58
|
|
}
|
|
59
|
|
|
|
60
|
4336
|
private void expandCapacity(int minimumCapacity) {
|
|
61
|
4336
|
int newCapacity = (value.length + 1) * 2;
|
|
62
|
4336
|
if (newCapacity < 0) {
|
|
63
|
0
|
newCapacity = Integer.MAX_VALUE;
|
|
64
|
4336
|
} else if (minimumCapacity > newCapacity) {
|
|
65
|
765
|
newCapacity = minimumCapacity;
|
|
66
|
|
}
|
|
67
|
|
|
|
68
|
4336
|
char newValue[] = new char[newCapacity];
|
|
69
|
4336
|
System.arraycopy(value, 0, newValue, 0, count);
|
|
70
|
4336
|
value = newValue;
|
|
71
|
4336
|
shared = false;
|
|
72
|
|
}
|
|
73
|
|
|
|
74
|
4795
|
public void setLength(int newLength) {
|
|
75
|
4795
|
if (newLength < 0) {
|
|
76
|
0
|
throw new StringIndexOutOfBoundsException(newLength);
|
|
77
|
|
}
|
|
78
|
|
|
|
79
|
4795
|
if (newLength > value.length) {
|
|
80
|
0
|
expandCapacity(newLength);
|
|
81
|
|
}
|
|
82
|
|
|
|
83
|
4795
|
if (count < newLength) {
|
|
84
|
0
|
if (shared) {
|
|
85
|
0
|
copy();
|
|
86
|
|
}
|
|
87
|
0
|
for (; count < newLength; count++) {
|
|
88
|
0
|
value[count] = '\0';
|
|
89
|
|
}
|
|
90
|
|
} else {
|
|
91
|
4795
|
count = newLength;
|
|
92
|
4795
|
if (shared) {
|
|
93
|
0
|
if (newLength > 0) {
|
|
94
|
0
|
copy();
|
|
95
|
|
} else {
|
|
96
|
|
// If newLength is zero, assume the StrBuffer is being
|
|
97
|
|
// stripped for reuse; Make new buffer of default size
|
|
98
|
0
|
value = new char[16];
|
|
99
|
0
|
shared = false;
|
|
100
|
|
}
|
|
101
|
|
}
|
|
102
|
|
}
|
|
103
|
|
}
|
|
104
|
|
|
|
105
|
17150
|
public char charAt(int index) {
|
|
106
|
17150
|
if ((index < 0) || (index >= count)) {
|
|
107
|
0
|
throw new StringIndexOutOfBoundsException(index);
|
|
108
|
|
}
|
|
109
|
17150
|
return value[index];
|
|
110
|
|
}
|
|
111
|
|
|
|
112
|
0
|
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
|
|
113
|
0
|
if (srcBegin < 0) {
|
|
114
|
0
|
throw new StringIndexOutOfBoundsException(srcBegin);
|
|
115
|
|
}
|
|
116
|
0
|
if ((srcEnd < 0) || (srcEnd > count)) {
|
|
117
|
0
|
throw new StringIndexOutOfBoundsException(srcEnd);
|
|
118
|
|
}
|
|
119
|
0
|
if (srcBegin > srcEnd) {
|
|
120
|
0
|
throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
|
|
121
|
|
}
|
|
122
|
0
|
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
|
|
123
|
|
}
|
|
124
|
|
|
|
125
|
15691
|
public void setCharAt(int index, char ch) {
|
|
126
|
15691
|
if ((index < 0) || (index >= count)) {
|
|
127
|
0
|
throw new StringIndexOutOfBoundsException(index);
|
|
128
|
|
}
|
|
129
|
15691
|
if (shared) {
|
|
130
|
0
|
copy();
|
|
131
|
|
}
|
|
132
|
15691
|
value[index] = ch;
|
|
133
|
|
}
|
|
134
|
|
|
|
135
|
34888
|
public StrBuffer append(Object obj) {
|
|
136
|
34888
|
return append(String.valueOf(obj));
|
|
137
|
|
}
|
|
138
|
|
|
|
139
|
328065
|
public StrBuffer append(String str) {
|
|
140
|
328065
|
if (str == null) {
|
|
141
|
0
|
str = String.valueOf(str);
|
|
142
|
|
}
|
|
143
|
|
|
|
144
|
328065
|
int len = str.length();
|
|
145
|
328065
|
int newcount = count + len;
|
|
146
|
328065
|
if (newcount > value.length) {
|
|
147
|
3462
|
expandCapacity(newcount);
|
|
148
|
|
}
|
|
149
|
328065
|
str.getChars(0, len, value, count);
|
|
150
|
328065
|
count = newcount;
|
|
151
|
328065
|
return this;
|
|
152
|
|
}
|
|
153
|
|
|
|
154
|
0
|
public StrBuffer append(char str[]) {
|
|
155
|
0
|
int len = str.length;
|
|
156
|
0
|
int newcount = count + len;
|
|
157
|
0
|
if (newcount > value.length) {
|
|
158
|
0
|
expandCapacity(newcount);
|
|
159
|
|
}
|
|
160
|
0
|
System.arraycopy(str, 0, value, count, len);
|
|
161
|
0
|
count = newcount;
|
|
162
|
0
|
return this;
|
|
163
|
|
}
|
|
164
|
|
|
|
165
|
0
|
public StrBuffer append(char str[], int offset, int len) {
|
|
166
|
0
|
int newcount = count + len;
|
|
167
|
0
|
if (newcount > value.length) {
|
|
168
|
0
|
expandCapacity(newcount);
|
|
169
|
|
}
|
|
170
|
0
|
System.arraycopy(str, offset, value, count, len);
|
|
171
|
0
|
count = newcount;
|
|
172
|
0
|
return this;
|
|
173
|
|
}
|
|
174
|
|
|
|
175
|
0
|
public StrBuffer append(boolean b) {
|
|
176
|
0
|
return append(String.valueOf(b));
|
|
177
|
|
}
|
|
178
|
|
|
|
179
|
184615
|
public StrBuffer append(char c) {
|
|
180
|
184615
|
int newcount = count + 1;
|
|
181
|
184615
|
if (newcount > value.length) {
|
|
182
|
874
|
expandCapacity(newcount);
|
|
183
|
|
}
|
|
184
|
184615
|
value[count++] = c;
|
|
185
|
184615
|
return this;
|
|
186
|
|
}
|
|
187
|
|
|
|
188
|
922
|
public StrBuffer append(int i) {
|
|
189
|
922
|
return append(String.valueOf(i));
|
|
190
|
|
}
|
|
191
|
|
|
|
192
|
0
|
public StrBuffer append(long l) {
|
|
193
|
0
|
return append(String.valueOf(l));
|
|
194
|
|
}
|
|
195
|
|
|
|
196
|
0
|
public StrBuffer append(float f) {
|
|
197
|
0
|
return append(String.valueOf(f));
|
|
198
|
|
}
|
|
199
|
|
|
|
200
|
0
|
public StrBuffer append(double d) {
|
|
201
|
0
|
return append(String.valueOf(d));
|
|
202
|
|
}
|
|
203
|
|
|
|
204
|
0
|
public StrBuffer delete(int start, int end) {
|
|
205
|
0
|
if (start < 0) {
|
|
206
|
0
|
throw new StringIndexOutOfBoundsException(start);
|
|
207
|
|
}
|
|
208
|
0
|
if (end > count) {
|
|
209
|
0
|
end = count;
|
|
210
|
|
}
|
|
211
|
0
|
if (start > end) {
|
|
212
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
213
|
|
}
|
|
214
|
|
|
|
215
|
0
|
int len = end - start;
|
|
216
|
0
|
if (len > 0) {
|
|
217
|
0
|
if (shared) {
|
|
218
|
0
|
copy();
|
|
219
|
|
}
|
|
220
|
0
|
System.arraycopy(value, start + len, value, start, count - end);
|
|
221
|
0
|
count -= len;
|
|
222
|
|
}
|
|
223
|
0
|
return this;
|
|
224
|
|
}
|
|
225
|
|
|
|
226
|
79
|
public StrBuffer deleteCharAt(int index) {
|
|
227
|
79
|
if ((index < 0) || (index >= count)) {
|
|
228
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
229
|
|
}
|
|
230
|
79
|
if (shared) {
|
|
231
|
0
|
copy();
|
|
232
|
|
}
|
|
233
|
79
|
System.arraycopy(value, index + 1, value, index, count - index - 1);
|
|
234
|
79
|
count--;
|
|
235
|
79
|
return this;
|
|
236
|
|
}
|
|
237
|
|
|
|
238
|
51
|
public StrBuffer replace(int start, int end, String str) {
|
|
239
|
51
|
if (start < 0) {
|
|
240
|
0
|
throw new StringIndexOutOfBoundsException(start);
|
|
241
|
|
}
|
|
242
|
51
|
if (end > count) {
|
|
243
|
0
|
end = count;
|
|
244
|
|
}
|
|
245
|
51
|
if (start > end) {
|
|
246
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
247
|
|
}
|
|
248
|
|
|
|
249
|
51
|
int len = str.length();
|
|
250
|
51
|
int newCount = count + len - (end - start);
|
|
251
|
51
|
if (newCount > value.length) {
|
|
252
|
0
|
expandCapacity(newCount);
|
|
253
|
51
|
} else if (shared) {
|
|
254
|
0
|
copy();
|
|
255
|
|
}
|
|
256
|
|
|
|
257
|
51
|
System.arraycopy(value, end, value, start + len, count - end);
|
|
258
|
51
|
str.getChars(0, len, value, start);
|
|
259
|
51
|
count = newCount;
|
|
260
|
51
|
return this;
|
|
261
|
|
}
|
|
262
|
|
|
|
263
|
0
|
public String substring(int start) {
|
|
264
|
0
|
return substring(start, count);
|
|
265
|
|
}
|
|
266
|
|
|
|
267
|
0
|
public String substring(int start, int end) {
|
|
268
|
0
|
if (start < 0) {
|
|
269
|
0
|
throw new StringIndexOutOfBoundsException(start);
|
|
270
|
|
}
|
|
271
|
0
|
if (end > count) {
|
|
272
|
0
|
throw new StringIndexOutOfBoundsException(end);
|
|
273
|
|
}
|
|
274
|
0
|
if (start > end) {
|
|
275
|
0
|
throw new StringIndexOutOfBoundsException(end - start);
|
|
276
|
|
}
|
|
277
|
0
|
return new String(value, start, end - start);
|
|
278
|
|
}
|
|
279
|
|
|
|
280
|
0
|
public StrBuffer insert(int index, char str[], int offset, int len) {
|
|
281
|
0
|
if ((index < 0) || (index > count)) {
|
|
282
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
283
|
|
}
|
|
284
|
0
|
if ((offset < 0) || (offset + len < 0) || (offset + len > str.length)) {
|
|
285
|
0
|
throw new StringIndexOutOfBoundsException(offset);
|
|
286
|
|
}
|
|
287
|
0
|
if (len < 0) {
|
|
288
|
0
|
throw new StringIndexOutOfBoundsException(len);
|
|
289
|
|
}
|
|
290
|
0
|
int newCount = count + len;
|
|
291
|
0
|
if (newCount > value.length) {
|
|
292
|
0
|
expandCapacity(newCount);
|
|
293
|
0
|
} else if (shared) {
|
|
294
|
0
|
copy();
|
|
295
|
|
}
|
|
296
|
0
|
System.arraycopy(value, index, value, index + len, count - index);
|
|
297
|
0
|
System.arraycopy(str, offset, value, index, len);
|
|
298
|
0
|
count = newCount;
|
|
299
|
0
|
return this;
|
|
300
|
|
}
|
|
301
|
|
|
|
302
|
0
|
public StrBuffer insert(int offset, Object obj) {
|
|
303
|
0
|
return insert(offset, String.valueOf(obj));
|
|
304
|
|
}
|
|
305
|
|
|
|
306
|
0
|
public StrBuffer insert(int offset, String str) {
|
|
307
|
0
|
if ((offset < 0) || (offset > count)) {
|
|
308
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
309
|
|
}
|
|
310
|
|
|
|
311
|
0
|
if (str == null) {
|
|
312
|
0
|
str = String.valueOf(str);
|
|
313
|
|
}
|
|
314
|
0
|
int len = str.length();
|
|
315
|
0
|
int newcount = count + len;
|
|
316
|
0
|
if (newcount > value.length) {
|
|
317
|
0
|
expandCapacity(newcount);
|
|
318
|
0
|
} else if (shared) {
|
|
319
|
0
|
copy();
|
|
320
|
|
}
|
|
321
|
0
|
System.arraycopy(value, offset, value, offset + len, count - offset);
|
|
322
|
0
|
str.getChars(0, len, value, offset);
|
|
323
|
0
|
count = newcount;
|
|
324
|
0
|
return this;
|
|
325
|
|
}
|
|
326
|
|
|
|
327
|
0
|
public StrBuffer insert(int offset, char str[]) {
|
|
328
|
0
|
if ((offset < 0) || (offset > count)) {
|
|
329
|
0
|
throw new StringIndexOutOfBoundsException();
|
|
330
|
|
}
|
|
331
|
0
|
int len = str.length;
|
|
332
|
0
|
int newcount = count + len;
|
|
333
|
0
|
if (newcount > value.length) {
|
|
334
|
0
|
expandCapacity(newcount);
|
|
335
|
0
|
} else if (shared) {
|
|
336
|
0
|
copy();
|
|
337
|
|
}
|
|
338
|
0
|
System.arraycopy(value, offset, value, offset + len, count - offset);
|
|
339
|
0
|
System.arraycopy(str, 0, value, offset, len);
|
|
340
|
0
|
count = newcount;
|
|
341
|
0
|
return this;
|
|
342
|
|
}
|
|
343
|
|
|
|
344
|
0
|
public StrBuffer insert(int offset, boolean b) {
|
|
345
|
0
|
return insert(offset, String.valueOf(b));
|
|
346
|
|
}
|
|
347
|
|
|
|
348
|
0
|
public StrBuffer insert(int offset, char c) {
|
|
349
|
0
|
int newcount = count + 1;
|
|
350
|
0
|
if (newcount > value.length) {
|
|
351
|
0
|
expandCapacity(newcount);
|
|
352
|
0
|
} else if (shared) {
|
|
353
|
0
|
copy();
|
|
354
|
|
}
|
|
355
|
0
|
System.arraycopy(value, offset, value, offset + 1, count - offset);
|
|
356
|
0
|
value[offset] = c;
|
|
357
|
0
|
count = newcount;
|
|
358
|
0
|
return this;
|
|
359
|
|
}
|
|
360
|
|
|
|
361
|
0
|
public StrBuffer insert(int offset, int i) {
|
|
362
|
0
|
return insert(offset, String.valueOf(i));
|
|
363
|
|
}
|
|
364
|
|
|
|
365
|
0
|
public StrBuffer insert(int offset, long l) {
|
|
366
|
0
|
return insert(offset, String.valueOf(l));
|
|
367
|
|
}
|
|
368
|
|
|
|
369
|
0
|
public StrBuffer insert(int offset, float f) {
|
|
370
|
0
|
return insert(offset, String.valueOf(f));
|
|
371
|
|
}
|
|
372
|
|
|
|
373
|
0
|
public StrBuffer insert(int offset, double d) {
|
|
374
|
0
|
return insert(offset, String.valueOf(d));
|
|
375
|
|
}
|
|
376
|
|
|
|
377
|
0
|
public StrBuffer reverse() {
|
|
378
|
0
|
if (shared) {
|
|
379
|
0
|
copy();
|
|
380
|
|
}
|
|
381
|
0
|
int n = count - 1;
|
|
382
|
0
|
for (int j = (n - 1) >> 1; j >= 0; --j) {
|
|
383
|
0
|
char temp = value[j];
|
|
384
|
0
|
value[j] = value[n - j];
|
|
385
|
0
|
value[n - j] = temp;
|
|
386
|
|
}
|
|
387
|
0
|
return this;
|
|
388
|
|
}
|
|
389
|
|
|
|
390
|
194561
|
public String toString() {
|
|
391
|
194561
|
return new String(value, 0, count);
|
|
392
|
|
}
|
|
393
|
|
|
|
394
|
0
|
final void setShared() {
|
|
395
|
0
|
shared = true;
|
|
396
|
|
}
|
|
397
|
0
|
final char[] getValue() {
|
|
398
|
0
|
return value;
|
|
399
|
|
}
|
|
400
|
|
|
|
401
|
0
|
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
|
|
402
|
0
|
s.defaultReadObject();
|
|
403
|
0
|
value = (char[])value.clone();
|
|
404
|
0
|
shared = false;
|
|
405
|
|
}
|
|
406
|
|
}
|
|
407
|
|
|