-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferTools.java
More file actions
284 lines (250 loc) · 8.79 KB
/
Copy pathBufferTools.java
File metadata and controls
284 lines (250 loc) · 8.79 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package Mp3File;
import java.io.UnsupportedEncodingException;
public final class BufferTools {
protected static final String defaultCharsetName = "ISO-8859-1";
private BufferTools() {}
public static String byteBufferToStringIgnoringEncodingIssues(byte[] bytes, int offset, int length) {
try {
return byteBufferToString(bytes, offset, length, defaultCharsetName);
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String byteBufferToString(byte[] bytes, int offset, int length) throws UnsupportedEncodingException {
return byteBufferToString(bytes, offset, length, defaultCharsetName);
}
public static String byteBufferToString(byte[] bytes, int offset, int length, String charsetName) throws UnsupportedEncodingException {
if (length < 1) return "";
return new String(bytes, offset, length, charsetName);
}
public static byte[] stringToByteBufferIgnoringEncodingIssues(String s, int offset, int length) {
try {
return stringToByteBuffer(s, offset, length);
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static byte[] stringToByteBuffer(String s, int offset, int length) throws UnsupportedEncodingException {
return stringToByteBuffer(s, offset, length, defaultCharsetName);
}
public static byte[] stringToByteBuffer(String s, int offset, int length, String charsetName) throws UnsupportedEncodingException {
String stringToCopy = s.substring(offset, offset + length);
return stringToCopy.getBytes(charsetName);
}
public static void stringIntoByteBuffer(String s, int offset, int length, byte[] bytes, int destOffset) throws UnsupportedEncodingException {
stringIntoByteBuffer(s, offset, length, bytes, destOffset, defaultCharsetName);
}
public static void stringIntoByteBuffer(String s, int offset, int length, byte[] bytes, int destOffset, String charsetName) throws UnsupportedEncodingException {
String stringToCopy = s.substring(offset, offset + length);
byte[] srcBytes = stringToCopy.getBytes(charsetName);
if (srcBytes.length > 0) {
System.arraycopy(srcBytes, 0, bytes, destOffset, srcBytes.length);
}
}
public static String trimStringRight(String s) {
int endPosition = s.length() - 1;
char endChar;
while (endPosition >= 0) {
endChar = s.charAt(endPosition);
if (endChar > 32) {
break;
}
endPosition--;
}
if (endPosition == s.length() - 1) return s;
else if (endPosition < 0) return "";
return s.substring(0, endPosition + 1);
}
public static String padStringRight(String s, int length, char padWith) {
if (s.length() >= length) return s;
StringBuilder stringBuffer = new StringBuilder(s);
while (stringBuffer.length() < length) {
stringBuffer.append(padWith);
}
return stringBuffer.toString();
}
public static boolean checkBit(byte b, int bitPosition) {
return ((b & (0x01 << bitPosition)) != 0);
}
public static byte setBit(byte b, int bitPosition, boolean value) {
byte newByte;
if (value) {
newByte = (byte) (b | ((byte) 0x01 << bitPosition));
} else {
newByte = (byte) (b & (~((byte) 0x01 << bitPosition)));
}
return newByte;
}
public static int shiftByte(byte c, int places) {
int i = c & 0xff;
if (places < 0) {
return i << -places;
} else if (places > 0) {
return i >> places;
}
return i;
}
public static int unpackInteger(byte b1, byte b2, byte b3, byte b4) {
int value = b4 & 0xff;
value += BufferTools.shiftByte(b3, -8);
value += BufferTools.shiftByte(b2, -16);
value += BufferTools.shiftByte(b1, -24);
return value;
}
public static byte[] packInteger(int i) {
byte[] bytes = new byte[4];
bytes[3] = (byte) (i & 0xff);
bytes[2] = (byte) ((i >> 8) & 0xff);
bytes[1] = (byte) ((i >> 16) & 0xff);
bytes[0] = (byte) ((i >> 24) & 0xff);
return bytes;
}
public static int unpackSynchsafeInteger(byte b1, byte b2, byte b3, byte b4) {
int value = ((byte) (b4 & 0x7f));
value += shiftByte((byte) (b3 & 0x7f), -7);
value += shiftByte((byte) (b2 & 0x7f), -14);
value += shiftByte((byte) (b1 & 0x7f), -21);
return value;
}
public static byte[] packSynchsafeInteger(int i) {
byte[] bytes = new byte[4];
packSynchsafeInteger(i, bytes, 0);
return bytes;
}
public static void packSynchsafeInteger(int i, byte[] bytes, int offset) {
bytes[offset + 3] = (byte) (i & 0x7f);
bytes[offset + 2] = (byte) ((i >> 7) & 0x7f);
bytes[offset + 1] = (byte) ((i >> 14) & 0x7f);
bytes[offset + 0] = (byte) ((i >> 21) & 0x7f);
}
public static byte[] copyBuffer(byte[] bytes, int offset, int length) {
byte[] copy = new byte[length];
if (length > 0) {
System.arraycopy(bytes, offset, copy, 0, length);
}
return copy;
}
public static void copyIntoByteBuffer(byte[] bytes, int offset, int length, byte[] destBuffer, int destOffset) {
if (length > 0) {
System.arraycopy(bytes, offset, destBuffer, destOffset, length);
}
}
public static int sizeUnsynchronisationWouldAdd(byte[] bytes) {
int count = 0;
for (int i = 0; i < bytes.length - 1; i++) {
if (bytes[i] == (byte) 0xff && ((bytes[i + 1] & (byte) 0xe0) == (byte) 0xe0 || bytes[i + 1] == 0)) {
count++;
}
}
if (bytes.length > 0 && bytes[bytes.length - 1] == (byte) 0xff) count++;
return count;
}
public static byte[] unsynchroniseBuffer(byte[] bytes) {
// unsynchronisation is replacing instances of:
// 11111111 111xxxxx with 11111111 00000000 111xxxxx and
// 11111111 00000000 with 11111111 00000000 00000000
int count = sizeUnsynchronisationWouldAdd(bytes);
if (count == 0) return bytes;
byte[] newBuffer = new byte[bytes.length + count];
int j = 0;
for (int i = 0; i < bytes.length - 1; i++) {
newBuffer[j++] = bytes[i];
if (bytes[i] == (byte) 0xff && ((bytes[i + 1] & (byte) 0xe0) == (byte) 0xe0 || bytes[i + 1] == 0)) {
newBuffer[j++] = 0;
}
}
newBuffer[j++] = bytes[bytes.length - 1];
if (bytes[bytes.length - 1] == (byte) 0xff) {
newBuffer[j++] = 0;
}
return newBuffer;
}
public static int sizeSynchronisationWouldSubtract(byte[] bytes) {
int count = 0;
for (int i = 0; i < bytes.length - 2; i++) {
if (bytes[i] == (byte) 0xff && bytes[i + 1] == 0 && ((bytes[i + 2] & (byte) 0xe0) == (byte) 0xe0 || bytes[i + 2] == 0)) {
count++;
}
}
if (bytes.length > 1 && bytes[bytes.length - 2] == (byte) 0xff && bytes[bytes.length - 1] == 0) count++;
return count;
}
public static byte[] synchroniseBuffer(byte[] bytes) {
// synchronisation is replacing instances of:
// 11111111 00000000 111xxxxx with 11111111 111xxxxx and
// 11111111 00000000 00000000 with 11111111 00000000
int count = sizeSynchronisationWouldSubtract(bytes);
if (count == 0) return bytes;
byte[] newBuffer = new byte[bytes.length - count];
int i = 0;
for (int j = 0; j < newBuffer.length - 1; j++) {
newBuffer[j] = bytes[i];
if (bytes[i] == (byte) 0xff && bytes[i + 1] == 0 && ((bytes[i + 2] & (byte) 0xe0) == (byte) 0xe0 || bytes[i + 2] == 0)) {
i++;
}
i++;
}
newBuffer[newBuffer.length - 1] = bytes[i];
return newBuffer;
}
public static String substitute(String s, String replaceThis, String withThis) {
if (replaceThis.length() < 1 || !s.contains(replaceThis)) {
return s;
}
StringBuilder newString = new StringBuilder();
int lastPosition = 0;
int position = 0;
while ((position = s.indexOf(replaceThis, position)) >= 0) {
if (position > lastPosition) {
newString.append(s.substring(lastPosition, position));
}
if (withThis != null) {
newString.append(withThis);
}
lastPosition = position + replaceThis.length();
position++;
}
if (lastPosition < s.length()) {
newString.append(s.substring(lastPosition));
}
return newString.toString();
}
public static String asciiOnly(String s) {
StringBuilder newString = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch < 32 || ch > 126) {
newString.append('?');
} else {
newString.append(ch);
}
}
return newString.toString();
}
public static int indexOfTerminator(byte[] bytes) {
return indexOfTerminator(bytes, 0);
}
public static int indexOfTerminator(byte[] bytes, int fromIndex) {
return indexOfTerminator(bytes, 0, 1);
}
public static int indexOfTerminator(byte[] bytes, int fromIndex, int terminatorLength) {
int marker = -1;
for (int i = fromIndex; i <= bytes.length - terminatorLength; i++) {
if ((i - fromIndex) % terminatorLength == 0) {
int matched;
for (matched = 0; matched < terminatorLength; matched++) {
if (bytes[i + matched] != 0) break;
}
if (matched == terminatorLength) {
marker = i;
break;
}
}
}
return marker;
}
public static int indexOfTerminatorForEncoding(byte[] bytes, int fromIndex, int encoding) {
int terminatorLength = (encoding == EncodedText.TEXT_ENCODING_UTF_16 || encoding == EncodedText.TEXT_ENCODING_UTF_16BE) ? 2 : 1;
return indexOfTerminator(bytes, fromIndex, terminatorLength);
}
}