Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/xml/XMLStringBuffer.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 1999, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.jpp.xml;
23
24
import java.text.NumberFormat;
25
26
public final class XMLStringBuffer {
27
28
private int count;
29
private boolean shared;
30
char[] value;
31
32
public XMLStringBuffer() {
33
this(16);
34
}
35
36
public XMLStringBuffer(int capacity) {
37
count = 0;
38
shared = false;
39
value = new char[capacity];
40
}
41
42
public XMLStringBuffer(String string) {
43
count = string.length();
44
shared = false;
45
value = new char[count + 16];
46
string.getChars(0, count, value, 0);
47
}
48
49
public XMLStringBuffer append(int value) {
50
return append((long) value);
51
}
52
53
public XMLStringBuffer append(long value) {
54
return append(NumberFormat.getNumberInstance().format(value));
55
}
56
57
public XMLStringBuffer append(Object value) {
58
return append(String.valueOf(value));
59
}
60
61
public XMLStringBuffer append(boolean value) {
62
return append(String.valueOf(value));
63
}
64
65
public XMLStringBuffer append(char value) {
66
return append(String.valueOf(value));
67
}
68
69
public XMLStringBuffer append(String string) {
70
if (string == null) {
71
string = String.valueOf(string);
72
}
73
int adding = string.length();
74
int newSize = count + adding;
75
if (newSize > value.length) {
76
ensureCapacityImpl(newSize);
77
} else if (shared) {
78
char[] copyValue = new char[value.length];
79
System.arraycopy(value, 0, copyValue, 0, copyValue.length);
80
value = copyValue;
81
shared = false;
82
}
83
string.getChars(0, adding, value, count);
84
count = newSize;
85
return this;
86
}
87
88
public int capacity() {
89
return value.length;
90
}
91
92
public synchronized char charAt(int index) {
93
if (index < count) {
94
return value[index];
95
}
96
throw new IndexOutOfBoundsException();
97
}
98
99
public synchronized void ensureCapacity(int min) {
100
if (min > value.length) {
101
ensureCapacityImpl(min);
102
}
103
}
104
105
private void ensureCapacityImpl(int min) {
106
int twice = (value.length << 1) + 2;
107
char[] newData = new char[Math.max(min, twice)];
108
System.arraycopy(value, 0, newData, 0, count);
109
value = newData;
110
shared = false;
111
}
112
113
public synchronized void getChars(int start, int end, char[] buffer, int index) {
114
// NOTE last character not copied!
115
if (start < count && end <= count) {
116
System.arraycopy(value, start, buffer, index, end - start);
117
return;
118
}
119
throw new IndexOutOfBoundsException();
120
}
121
122
public synchronized XMLStringBuffer insert(int index, char[] chars) {
123
move(chars.length, index);
124
System.arraycopy(chars, 0, value, index, chars.length);
125
count += chars.length;
126
return this;
127
}
128
129
public synchronized XMLStringBuffer insert(int index, char ch) {
130
move(1, index);
131
value[index] = ch;
132
count++;
133
return this;
134
}
135
136
public XMLStringBuffer insert(int index, int value) {
137
return insert(index, (long) value);
138
}
139
140
public XMLStringBuffer insert(int index, long value) {
141
return insert(index, NumberFormat.getNumberInstance().format(value));
142
}
143
144
public XMLStringBuffer insert(int index, Object value) {
145
return insert(index, String.valueOf(value));
146
}
147
148
public synchronized XMLStringBuffer insert(int index, String string) {
149
if (string == null) {
150
string = String.valueOf(string);
151
}
152
int min = string.length();
153
move(min, index);
154
string.getChars(0, min, value, index);
155
count += min;
156
return this;
157
}
158
159
public XMLStringBuffer insert(int index, boolean value) {
160
return insert(index, String.valueOf(value));
161
}
162
163
public int length() {
164
return count;
165
}
166
167
private void move(int size, int index) {
168
if (0 <= index && index <= count) {
169
int newSize;
170
if (value.length - count >= size) {
171
if (!shared) {
172
System.arraycopy(value, index, value, index + size, count - index); // index == count case is no-op
173
return;
174
}
175
newSize = value.length;
176
} else {
177
int a = count + size;
178
int b = (value.length << 1) + 2;
179
newSize = Math.max(a, b);
180
}
181
char[] newData = new char[newSize];
182
System.arraycopy(value, 0, newData, 0, index);
183
System.arraycopy(value, index, newData, index + size, count - index); // index == count case is no-op
184
value = newData;
185
shared = false;
186
} else {
187
throw new IndexOutOfBoundsException();
188
}
189
}
190
191
public synchronized void setLength(int length) {
192
if (length > value.length) {
193
ensureCapacityImpl(length);
194
}
195
if (count > length) {
196
if (shared) {
197
char[] newData = new char[value.length];
198
System.arraycopy(value, 0, newData, 0, length);
199
value = newData;
200
shared = false;
201
} else {
202
// NOTE: delete & replace do not void characters orphaned at the end
203
try {
204
for (int i = length; i < count; i++) {
205
value[i] = 0;
206
}
207
} catch (IndexOutOfBoundsException e) {
208
throw new IndexOutOfBoundsException();
209
}
210
}
211
}
212
count = length;
213
}
214
215
public synchronized boolean endsWith(String suffix) {
216
int suffixLength = suffix.length();
217
218
if (suffixLength < 0 || count < suffixLength) {
219
return false;
220
}
221
222
int localIndex = count - suffixLength;
223
int stringIndex = 0;
224
225
while (suffixLength-- > 0) {
226
if (charAt(localIndex++) != suffix.charAt(stringIndex++)) {
227
return false;
228
}
229
}
230
231
return true;
232
}
233
234
@Override
235
public String toString() {
236
shared = true;
237
return new String(value, 0, count);
238
}
239
240
}
241
242