Skip to content

Commit 54bc437

Browse files
committed
ISO_8859_1 is faster encoding and decoding than US_ASCII
1 parent 262df37 commit 54bc437

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/main/java/scottf/ByteArrayBuilder.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import java.nio.charset.Charset;
66
import java.util.Arrays;
77

8+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
89
import static java.nio.charset.StandardCharsets.US_ASCII;
910

1011
public 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

src/test/java/scottf/ByteArrayBuilderTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Random;
1313

14+
import static java.nio.charset.StandardCharsets.ISO_8859_1;
1415
import static org.junit.jupiter.api.Assertions.*;
1516

1617
public class ByteArrayBuilderTests {
@@ -30,7 +31,7 @@ public static long bytesToLong(byte[] bytes) {
3031
public void byte_array_builder_works() {
3132
ByteArrayBuilder bab = new ByteArrayBuilder();
3233
String testString = "abcdefghij";
33-
_test(PRAND, bab, Collections.singletonList(testString), StandardCharsets.US_ASCII);
34+
_test(PRAND, bab, Collections.singletonList(testString), ISO_8859_1);
3435
}
3536

3637
@Test
@@ -46,7 +47,7 @@ public void equalsBytes() {
4647
public void copyTo() {
4748
ByteArrayBuilder bab = new ByteArrayBuilder();
4849
bab.append("0123456789");
49-
byte[] target = "AAAAAAAAAAAAAAAAAAAA".getBytes(StandardCharsets.US_ASCII);
50+
byte[] target = "AAAAAAAAAAAAAAAAAAAA".getBytes(ISO_8859_1);
5051
assertEquals(10, bab.copyTo(target, 0));
5152
assertEquals(10, bab.copyTo(target, 10));
5253
assertEquals("01234567890123456789", new String(target));
@@ -61,19 +62,19 @@ public void constructorCoverage() {
6162
bab = new ByteArrayBuilder(-1, StandardCharsets.UTF_8);
6263
assertEquals(ByteArrayBuilder.DEFAULT_OTHER_ALLOCATION, bab.internalArray().length);
6364

64-
bab = new ByteArrayBuilder(-1, -1, StandardCharsets.US_ASCII);
65+
bab = new ByteArrayBuilder(-1, -1, ISO_8859_1);
6566
assertEquals(ByteArrayBuilder.DEFAULT_ASCII_ALLOCATION, bab.internalArray().length);
6667

6768
bab = new ByteArrayBuilder(-1, -1, StandardCharsets.UTF_8);
6869
assertEquals(ByteArrayBuilder.DEFAULT_OTHER_ALLOCATION, bab.internalArray().length);
6970

70-
bab = new ByteArrayBuilder(-1, 100, StandardCharsets.US_ASCII);
71+
bab = new ByteArrayBuilder(-1, 100, ISO_8859_1);
7172
assertEquals(ByteArrayBuilder.DEFAULT_ASCII_ALLOCATION, bab.internalArray().length);
7273

7374
bab = new ByteArrayBuilder(-1, 100, StandardCharsets.UTF_8);
7475
assertEquals(ByteArrayBuilder.DEFAULT_OTHER_ALLOCATION, bab.internalArray().length);
7576

76-
bab = new ByteArrayBuilder(100, -1, StandardCharsets.US_ASCII);
77+
bab = new ByteArrayBuilder(100, -1, ISO_8859_1);
7778
assertEquals(100, bab.internalArray().length);
7879

7980
bab = new ByteArrayBuilder(100, -1, StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)