Skip to content

Commit f19863d

Browse files
committed
Add null-safe from() constructor
1 parent 7527102 commit f19863d

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v0.5.0
44
* better resource handling for compression
5+
* add nullSafe from() constructor
56

67
## v0.4.6
78

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ For a **null-safe version**, which uses the empty array in case of a null byte a
113113

114114
```java
115115
Bytes.wrapNullSafe(null);
116+
Bytes.fromNullSafe(null);
116117
```
117118

118119
**Concatenating** of multiple byte arrays or bytes:

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static Bytes wrap(Bytes bytes) {
111111
* @return new instance
112112
*/
113113
public static Bytes wrapNullSafe(byte[] array) {
114-
return wrap(array != null ? array : new byte[0], ByteOrder.BIG_ENDIAN);
114+
return wrap(array != null ? array : new byte[0]);
115115
}
116116

117117
/**
@@ -156,6 +156,19 @@ public static Bytes from(byte[] byteArrayToCopy) {
156156
return wrap(Arrays.copyOf(byteArrayToCopy, byteArrayToCopy.length));
157157
}
158158

159+
/**
160+
* Creates a new instance from given collections of single bytes.
161+
* This will create a copy of given bytes and will not directly use given bytes or byte array.
162+
* <p>
163+
* If given array is null, a zero length byte array will be created and used instead.
164+
*
165+
* @param byteArrayToCopy will not be used directly, but a copy; may be null
166+
* @return new instance
167+
*/
168+
public static Bytes fromNullSafe(byte[] byteArrayToCopy) {
169+
return from(byteArrayToCopy != null ? byteArrayToCopy : new byte[0]);
170+
}
171+
159172
/**
160173
* Creates a new instance from a slice of given array
161174
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public MutableBytes secureWipe() {
119119
* @return this instance
120120
*/
121121
public MutableBytes secureWipe(SecureRandom random) {
122-
Objects.requireNonNull(random, "must non-null random");
122+
Objects.requireNonNull(random, "must be non-null random");
123123
if (length() > 0) {
124124
random.nextBytes(internalArray());
125125
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ public void fromVariousBytes() {
347347
assertArrayEquals(new byte[]{1, 2, 3}, Bytes.from((byte) 1, (byte) 2, (byte) 3).array());
348348
assertArrayEquals(new byte[2], Bytes.from((byte) 0, (byte) 0).array());
349349
assertArrayNotEquals(new byte[2], Bytes.from((byte) 1, (byte) 0).array());
350+
351+
assertArrayEquals(new byte[0], Bytes.fromNullSafe(null).array());
352+
assertArrayEquals(example_bytes_two, Bytes.fromNullSafe(example_bytes_two).array());
353+
assertArrayEquals(example_bytes_sixteen, Bytes.fromNullSafe(example_bytes_sixteen).array());
350354
}
351355

352356
@Test

0 commit comments

Comments
 (0)