File tree Expand file tree Collapse file tree 4 files changed +19
-1
lines changed
main/java/at/favre/lib/bytes
test/java/at/favre/lib/bytes Expand file tree Collapse file tree 4 files changed +19
-1
lines changed Original file line number Diff line number Diff line change 55 * add `encodeCharsetToBytes()` feature #7
66 * add new `from(char[] charArray, Charset charset)` constructor with improved logic #8
77 * add constructor/converter from/to UUID #9
8+ * add `empty()` constructor, creating empty byte array
89
910## v0.5.0
1011
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ Initializing **empty arrays** of arbitrary length:
139139``` java
140140Bytes . allocate(16 );
141141Bytes . allocate(4 , (byte ) 1 ); // fill with 0x01
142+ Bytes . empty(); // creates zero length byte array
142143```
143144
144145Creating ** random** byte arrays for e.g. testing:
Original file line number Diff line number Diff line change @@ -92,12 +92,21 @@ public static Bytes allocate(int length) {
9292 */
9393 public static Bytes allocate (int length , byte defaultValue ) {
9494 byte [] array = new byte [length ];
95- if (defaultValue != 0 ) {
95+ if (defaultValue != 0 && length > 0 ) {
9696 Arrays .fill (array , defaultValue );
9797 }
9898 return wrap (array );
9999 }
100100
101+ /**
102+ * Creates an Byte instance with an internal empty byte array. Same as calling {@link #allocate(int)} with 0.
103+ *
104+ * @return new instance
105+ */
106+ public static Bytes empty () {
107+ return allocate (0 );
108+ }
109+
101110 /**
102111 * Creates a new reference backed by the same byte array.
103112 * Inherits all attributes (readonly, etc.)
Original file line number Diff line number Diff line change @@ -76,6 +76,13 @@ public void wrapTestNullSafe() {
7676 Bytes .wrapNullSafe (null );
7777 }
7878
79+ @ Test
80+ public void empty () {
81+ assertEquals (0 , Bytes .empty ().length ());
82+ assertEquals (Bytes .allocate (0 ), Bytes .empty ());
83+ assertArrayEquals (new byte [0 ], Bytes .empty ().array ());
84+ }
85+
7986 @ Test
8087 public void allocate () {
8188 assertEquals (0 , Bytes .allocate (0 ).length ());
You can’t perform that action at this time.
0 commit comments