Skip to content

Commit 0f61a58

Browse files
committed
Use EMPTY constant instance for empty byte array to safe memory
fix #17
1 parent f901085 commit 0f61a58

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* add `from(Inputstream stream, int maxlength)` limiting stream reading constructor #13
88
* add indexOf() with `fromIndex` parameter #14
99
* add support for base64 url safe encoding #15
10+
* use EMPTY constant instance for empty byte array to safe memory #16
1011

1112
## v0.6.0
1213

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
@SuppressWarnings("WeakerAccess")
6161
public class Bytes implements Comparable<Bytes>, Serializable, Iterable<Byte> {
6262

63+
private final static Bytes EMPTY = Bytes.wrap(new byte[0]);
64+
6365
/* FACTORY ***************************************************************************************************/
6466

6567
/**
@@ -90,10 +92,10 @@ public static Bytes allocate(int length, byte defaultValue) {
9092
/**
9193
* Creates an Byte instance with an internal empty byte array. Same as calling {@link #allocate(int)} with 0.
9294
*
93-
* @return new instance
95+
* @return the empty instance (always the same reference
9496
*/
9597
public static Bytes empty() {
96-
return allocate(0);
98+
return EMPTY;
9799
}
98100

99101
/**
@@ -120,7 +122,7 @@ public static Bytes wrap(Bytes bytes) {
120122
* @return new instance
121123
*/
122124
public static Bytes wrapNullSafe(byte[] array) {
123-
return wrap(array != null ? array : new byte[0]);
125+
return array != null ? wrap(array) : empty();
124126
}
125127

126128
/**
@@ -175,7 +177,7 @@ public static Bytes from(byte[] byteArrayToCopy) {
175177
* @return new instance
176178
*/
177179
public static Bytes fromNullSafe(byte[] byteArrayToCopy) {
178-
return from(byteArrayToCopy != null ? byteArrayToCopy : new byte[0]);
180+
return byteArrayToCopy != null ? from(byteArrayToCopy) : empty();
179181
}
180182

181183
/**

src/test/java/at/favre/lib/bytes/BytesConstructorTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626
import org.junit.Test;
2727
import org.junit.rules.TemporaryFolder;
2828

29-
import java.io.ByteArrayInputStream;
30-
import java.io.DataInput;
31-
import java.io.DataInputStream;
32-
import java.io.File;
33-
import java.io.FileOutputStream;
29+
import java.io.*;
3430
import java.math.BigInteger;
3531
import java.nio.ByteBuffer;
32+
import java.nio.ByteOrder;
3633
import java.nio.charset.Charset;
3734
import java.nio.charset.StandardCharsets;
3835
import java.text.Normalizer;
@@ -82,6 +79,11 @@ public void empty() {
8279
assertEquals(0, Bytes.empty().length());
8380
assertEquals(Bytes.allocate(0), Bytes.empty());
8481
assertArrayEquals(new byte[0], Bytes.empty().array());
82+
assertSame(Bytes.empty(), Bytes.empty());
83+
84+
Bytes empty = Bytes.empty();
85+
empty = empty.byteOrder(ByteOrder.LITTLE_ENDIAN);
86+
assertNotSame(Bytes.empty(), empty);
8587
}
8688

8789
@Test

0 commit comments

Comments
 (0)