-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayBuilder.java
More file actions
348 lines (317 loc) · 10.8 KB
/
Copy pathByteArrayBuilder.java
File metadata and controls
348 lines (317 loc) · 10.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package scottf;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.US_ASCII;
public class ByteArrayBuilder {
public static final int DEFAULT_ASCII_ALLOCATION = 32;
public static final int DEFAULT_OTHER_ALLOCATION = 64;
public static final byte[] NULL = "null".getBytes(ISO_8859_1);
private final Charset defaultCharset;
private ByteBuffer buffer;
private int allocationSize;
/**
* Construct the ByteArrayBuilder with
* the initial size and allocation size of {@value #DEFAULT_ASCII_ALLOCATION}
* and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1}
* since ISO_8859_1 is faster when encoding and decoding than US_ASCII
*/
public ByteArrayBuilder() {
this(DEFAULT_ASCII_ALLOCATION, DEFAULT_ASCII_ALLOCATION, ISO_8859_1);
}
/**
* Construct the ByteArrayBuilder with the supplied initial size,
* allocation size of {@value #DEFAULT_ASCII_ALLOCATION}
* and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1}
* since ISO_8859_1 is faster when encoding and decoding than US_ASCII
* @param initialSize the initial size
*/
public ByteArrayBuilder(int initialSize) {
this(initialSize, DEFAULT_ASCII_ALLOCATION, ISO_8859_1);
}
/**
* Construct the ByteArrayBuilder with an existing byte array using its
* length as the initial length, the allocation size the larger of
* the length and {@value #DEFAULT_ASCII_ALLOCATION},
* and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1}
* since ISO_8859_1 is faster when encoding and decoding than US_ASCII
* Then initializes the buffer with the supplied bytes
* @param bytes the bytes
*/
public ByteArrayBuilder(byte[] bytes) {
allocationSize = Math.max(DEFAULT_ASCII_ALLOCATION, bytes.length);
this.buffer = ByteBuffer.allocate(bytes.length);
this.defaultCharset = ISO_8859_1;
buffer.put(bytes, 0, bytes.length);
}
/**
* Construct the ByteArrayBuilder with the supplied character set
* with the default initial size and allocation size determined by that character set
* @param defaultCharset the default character set
*/
public ByteArrayBuilder(Charset defaultCharset) {
this(-1, -1, defaultCharset);
}
/**
* Construct the ByteArrayBuilder with the supplied initial size and character set
* with the allocation size determined by that character set.
* @param initialSize the initial size
* @param defaultCharset the default character set
*/
public ByteArrayBuilder(int initialSize, Charset defaultCharset) {
this(initialSize, -1, defaultCharset);
}
/**
* Construct the ByteArrayBuilder with the supplied initial size,
* allocation size and character set
* @param initialSize the initial size
* @param allocationSize the allocationSize size
* @param defaultCharset the default character set
*/
public ByteArrayBuilder(int initialSize, int allocationSize, Charset defaultCharset) {
int alSize = defaultCharset == ISO_8859_1 || defaultCharset == US_ASCII ? DEFAULT_ASCII_ALLOCATION : DEFAULT_OTHER_ALLOCATION;
this.allocationSize = allocationSize > 0 ? allocationSize : alSize;
int bytesNeeded = initialSize > 0 ? initialSize : alSize;
this.buffer = ByteBuffer.allocate(bytesNeeded);
this.defaultCharset = defaultCharset;
}
/**
* Get the length of the data in the buffer
*
* @return the length of the data
*/
public int length() {
return buffer.position();
}
/**
* Determine if a byte array contains the same bytes as this buffer
*
* @param bytes the bytes
* @return true if the supplied value equals what is in the buffer
*/
public boolean equals(byte[] bytes) {
if (bytes == null || buffer.position() != bytes.length) {
return false;
}
byte[] hb = buffer.array();
for (int x = 0; x < bytes.length; x++) {
if (hb[x] != bytes[x]) {
return false;
}
}
return true;
}
/**
* Copy the contents of the buffer to the byte array starting at the destination
* positions supplied. Assumes that the {@link #length} method has been called
* and the destination byte array has enough space allocated
*
* @param dest the destination byte array
* @param destPos the starting position in the destination byte array
* @return the number of bytes copied
*/
public int copyTo(byte[] dest, int destPos) {
int len = length();
byte[] hb = buffer.array();
System.arraycopy(hb, 0, dest, destPos, len);
return len;
}
/**
* Copy the value in the buffer to a new byte array
*
* @return the copy of the bytes
*/
public byte[] toByteArray() {
return Arrays.copyOf(buffer.array(), buffer.position());
}
/**
* Access the internal byte array of this buffer. Intended for read only
* with knowledge of {@link #length}
*
* @return a direct handle to the internal byte array
*/
public byte[] internalArray() {
return buffer.array();
}
protected int computeAmountToAllocate(int currentPosition, int bytesNeeded) {
return ((currentPosition + bytesNeeded + allocationSize) / allocationSize) * allocationSize;
}
/**
* Ensures that the buffer can accept the number of bytes needed
* Useful if the size of multiple append operations is known ahead of time
* therefore reducing the number of possible allocations during those appends
*
* @param bytesNeeded the number of bytes needed
*
* @return this (fluent)
*/
public ByteArrayBuilder ensureCapacity(int bytesNeeded) {
int bytesAvailable = buffer.capacity() - buffer.position();
if (bytesAvailable < bytesNeeded) {
ByteBuffer newBuffer
= ByteBuffer.allocate(
computeAmountToAllocate(buffer.position(), bytesNeeded));
newBuffer.put(buffer.array(), 0, buffer.position());
buffer = newBuffer;
}
return this;
}
/**
* Clear the buffer, resetting its length
*
* @return this (fluent)
*/
public ByteArrayBuilder clear() {
buffer.clear();
return this;
}
/**
* Change the allocation size
*
* @param allocationSize the new allocation size
*
* @return this (fluent)
*/
public ByteArrayBuilder setAllocationSize(int allocationSize) {
this.allocationSize = allocationSize;
return this;
}
/**
* Append a String representation of the number.
*
* @param i the number
* @return this (fluent)
*/
public ByteArrayBuilder append(int i) {
append(Integer.toString(i).getBytes(ISO_8859_1)); // a number is always ascii
return this;
}
/**
* Append a String with the default charset.
* If the src is null, the word 'null' is appended.
*
* @param src
* The String from which bytes are to be read
* @return this (fluent)
*/
public ByteArrayBuilder append(String src) {
return append(src, defaultCharset);
}
/**
* Append a String with specified charset.
* If the src is null, the word 'null' is appended.
*
* @param src
* The String from which bytes are to be read
* @param charset the charset for encoding
* @return this (fluent)
*/
public ByteArrayBuilder append(String src, Charset charset) {
return src == null ? append(NULL, 0, 4) : append(src.getBytes(charset));
}
/**
* Append a CharBuffer with default charset.
* If the src is null, the word 'null' is appended.
*
* @param src
* The CharBuffer from which bytes are to be read
* @return this (fluent)
*/
public ByteArrayBuilder append(CharBuffer src) {
return append(src, defaultCharset);
}
/**
* Append a CharBuffer with specified charset.
* If the src is null, the word 'null' is appended.
*
* @param src
* The CharBuffer from which bytes are to be read
* @param charset the charset for encoding
* @return this (fluent)
*/
public ByteArrayBuilder append(CharBuffer src, Charset charset) {
if (src == null) {
append(NULL, 0, 4);
}
else {
append(src.toString().getBytes(charset));
}
return this;
}
/**
* Append a byte as is
*
* @param b the byte
* @return this (fluent)
*/
public ByteArrayBuilder append(byte b) {
ensureCapacity(1);
buffer.put(b);
return this;
}
/**
* Append a byte array
*
* @param src
* The array from which bytes are to be read
* @return this (fluent)
*/
public ByteArrayBuilder append(byte[] src) {
if (src.length > 0) {
ensureCapacity(src.length);
buffer.put(src, 0, src.length);
}
return this;
}
/**
* Append a byte array
*
* @param src
* The array from which bytes are to be read
* @param len
* The number of bytes to be read from the given array;
* must be non-negative and no larger than
* <code>array.length - offset</code>
* @return this (fluent)
*/
public ByteArrayBuilder append(byte[] src, int len) {
if (len > 0) {
ensureCapacity(len);
buffer.put(src, 0, len);
}
return this;
}
/**
* Append a byte array
*
* @param src
* The array from which bytes are to be read
* @param offset
* The offset within the array of the first byte to be read;
* must be non-negative and no larger than <code>array.length</code>
* @param len
* The number of bytes to be read from the given array;
* must be non-negative and no larger than
* <code>array.length - offset</code>
* @return this (fluent)
*/
public ByteArrayBuilder append(byte[] src, int offset, int len) {
if (len > 0) {
ensureCapacity(len);
buffer.put(src, offset, len);
}
return this;
}
public ByteArrayBuilder append(ByteArrayBuilder bab) {
if (bab != null && bab.length() > 0) {
append(bab.buffer.array(), 0, bab.length());
}
return this;
}
@Override
public String toString() {
return new String(buffer.array(), 0, buffer.position(), defaultCharset);
}
}