55import java .nio .charset .Charset ;
66import java .util .Arrays ;
77
8+ import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
89import static java .nio .charset .StandardCharsets .US_ASCII ;
910
1011public class ByteArrayBuilder {
1112 public static final int DEFAULT_ASCII_ALLOCATION = 32 ;
1213 public static final int DEFAULT_OTHER_ALLOCATION = 64 ;
13- public static final byte [] NULL = "null" .getBytes (US_ASCII );
14+ public static final byte [] NULL = "null" .getBytes (ISO_8859_1 );
1415
1516 private final Charset defaultCharset ;
1617 private ByteBuffer buffer ;
@@ -19,44 +20,43 @@ public class ByteArrayBuilder {
1920 /**
2021 * Construct the ByteArrayBuilder with
2122 * the initial size and allocation size of {@value #DEFAULT_ASCII_ALLOCATION}
22- * and the character set {@link java.nio.charset.StandardCharsets#US_ASCII}
23+ * and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1}
24+ * since ISO_8859_1 is faster when encoding and decoding than US_ASCII
2325 */
2426 public ByteArrayBuilder () {
25- this (DEFAULT_ASCII_ALLOCATION , DEFAULT_ASCII_ALLOCATION , US_ASCII );
27+ this (DEFAULT_ASCII_ALLOCATION , DEFAULT_ASCII_ALLOCATION , ISO_8859_1 );
2628 }
2729
2830 /**
2931 * Construct the ByteArrayBuilder with the supplied initial size,
3032 * allocation size of {@value #DEFAULT_ASCII_ALLOCATION}
31- * and the character set {@link java.nio.charset.StandardCharsets#US_ASCII }
32- *
33+ * and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1 }
34+ * since ISO_8859_1 is faster when encoding and decoding than US_ASCII
3335 * @param initialSize the initial size
3436 */
3537 public ByteArrayBuilder (int initialSize ) {
36- this (initialSize , DEFAULT_ASCII_ALLOCATION , US_ASCII );
38+ this (initialSize , DEFAULT_ASCII_ALLOCATION , ISO_8859_1 );
3739 }
3840
3941 /**
40- * Construct the ByteArrayBuilder with an existing byte array using it's
42+ * Construct the ByteArrayBuilder with an existing byte array using its
4143 * length as the initial length, the allocation size the larger of
4244 * the length and {@value #DEFAULT_ASCII_ALLOCATION},
43- * and the character set {@link java.nio.charset.StandardCharsets#US_ASCII }
44- *
45+ * and the character set {@link java.nio.charset.StandardCharsets#ISO_8859_1 }
46+ * since ISO_8859_1 is faster when encoding and decoding than US_ASCII
4547 * Then initializes the buffer with the supplied bytes
46- *
4748 * @param bytes the bytes
4849 */
4950 public ByteArrayBuilder (byte [] bytes ) {
5051 allocationSize = Math .max (DEFAULT_ASCII_ALLOCATION , bytes .length );
5152 this .buffer = ByteBuffer .allocate (bytes .length );
52- this .defaultCharset = US_ASCII ;
53+ this .defaultCharset = ISO_8859_1 ;
5354 buffer .put (bytes , 0 , bytes .length );
5455 }
5556
5657 /**
5758 * Construct the ByteArrayBuilder with the supplied character set
5859 * with the default initial size and allocation size determined by that character set
59- *
6060 * @param defaultCharset the default character set
6161 */
6262 public ByteArrayBuilder (Charset defaultCharset ) {
@@ -66,7 +66,6 @@ public ByteArrayBuilder(Charset defaultCharset) {
6666 /**
6767 * Construct the ByteArrayBuilder with the supplied initial size and character set
6868 * with the allocation size determined by that character set.
69- *
7069 * @param initialSize the initial size
7170 * @param defaultCharset the default character set
7271 */
@@ -78,15 +77,15 @@ public ByteArrayBuilder(int initialSize, Charset defaultCharset) {
7877 /**
7978 * Construct the ByteArrayBuilder with the supplied initial size,
8079 * allocation size and character set
81- *
8280 * @param initialSize the initial size
8381 * @param allocationSize the allocationSize size
8482 * @param defaultCharset the default character set
8583 */
8684 public ByteArrayBuilder (int initialSize , int allocationSize , Charset defaultCharset ) {
87- this .allocationSize = allocationSize > 0 ? allocationSize : (defaultCharset == US_ASCII ? DEFAULT_ASCII_ALLOCATION : DEFAULT_OTHER_ALLOCATION );
88- int bytesNeeeded = initialSize > 0 ? initialSize : (defaultCharset == US_ASCII ? DEFAULT_ASCII_ALLOCATION : DEFAULT_OTHER_ALLOCATION );
89- this .buffer = ByteBuffer .allocate (bytesNeeeded );
85+ int alSize = defaultCharset == ISO_8859_1 || defaultCharset == US_ASCII ? DEFAULT_ASCII_ALLOCATION : DEFAULT_OTHER_ALLOCATION ;
86+ this .allocationSize = allocationSize > 0 ? allocationSize : alSize ;
87+ int bytesNeeded = initialSize > 0 ? initialSize : alSize ;
88+ this .buffer = ByteBuffer .allocate (bytesNeeded );
9089 this .defaultCharset = defaultCharset ;
9190 }
9291
@@ -179,7 +178,7 @@ public ByteArrayBuilder ensureCapacity(int bytesNeeded) {
179178 }
180179
181180 /**
182- * Clear the buffer, resetting it's length
181+ * Clear the buffer, resetting its length
183182 *
184183 * @return this (fluent)
185184 */
@@ -207,7 +206,7 @@ public ByteArrayBuilder setAllocationSize(int allocationSize) {
207206 * @return this (fluent)
208207 */
209208 public ByteArrayBuilder append (int i ) {
210- append (Integer .toString (i ).getBytes (US_ASCII )); // a number is always ascii
209+ append (Integer .toString (i ).getBytes (ISO_8859_1 )); // a number is always ascii
211210 return this ;
212211 }
213212
0 commit comments