Skip to content

Commit e7825ad

Browse files
committed
Add nullsafe wrap method
1 parent 32d70c8 commit e7825ad

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## v0.4.5
4+
5+
* add nullSafe wrapper
6+
37
## v0.4.4
48

59
* add feature for gathering parts of the array as primitives (e.g. intAt(int position))

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ Bytes.wrap(myArray).copy() ~ Bytes.from(myArray)
108108

109109
#### More Constructors
110110

111+
For a **null-safe version**, which uses the empty array in case of a null byte array:
112+
113+
```java
114+
Bytes.wrapNullSafe(null);
115+
```
116+
111117
**Concatenating** of multiple byte arrays or bytes:
112118

113119
```java

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ public static Bytes wrap(Bytes bytes) {
9797
return new Bytes(bytes.internalArray(), bytes.byteOrder);
9898
}
9999

100+
/**
101+
* Creates a new instance with given byte array.
102+
* <p>
103+
* The new instance will be backed by the given byte array;
104+
* that is, modifications to the bytes will cause the array to be modified
105+
* and vice versa.
106+
* <p>
107+
* If given array is null, a zero length byte array will be created and used instead.
108+
*
109+
* @param array to use directly or zero length byte array
110+
* @return new instance
111+
*/
112+
public static Bytes wrapNullSafe(byte[] array) {
113+
return wrap(array != null ? array : new byte[0], ByteOrder.BIG_ENDIAN);
114+
}
115+
100116
/**
101117
* Creates a new instance with given byte array.
102118
* <p>
@@ -1000,7 +1016,7 @@ public int unsignedByteAt(int index) {
10001016
*/
10011017
public char charAt(int index) {
10021018
Util.checkIndexBounds(length(), index, 2, "char");
1003-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getChar();
1019+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getChar();
10041020
}
10051021

10061022
/**
@@ -1013,7 +1029,7 @@ public char charAt(int index) {
10131029
*/
10141030
public short shortAt(int index) {
10151031
Util.checkIndexBounds(length(), index, 2, "short");
1016-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getShort();
1032+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getShort();
10171033
}
10181034

10191035
/**
@@ -1026,7 +1042,7 @@ public short shortAt(int index) {
10261042
*/
10271043
public int intAt(int index) {
10281044
Util.checkIndexBounds(length(), index, 4, "int");
1029-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getInt();
1045+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getInt();
10301046
}
10311047

10321048
/**
@@ -1039,7 +1055,7 @@ public int intAt(int index) {
10391055
*/
10401056
public long longAt(int index) {
10411057
Util.checkIndexBounds(length(), index, 8, "long");
1042-
return ((ByteBuffer) ByteBuffer.wrap(internalArray()).order(byteOrder).position(index)).getLong();
1058+
return ByteBuffer.wrap(internalArray()).order(byteOrder).position(index).getLong();
10431059
}
10441060

10451061
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public MutableBytes secureWipe() {
120120
*/
121121
public MutableBytes secureWipe(SecureRandom random) {
122122
Objects.requireNonNull(random, "must non-null random");
123-
random.nextBytes(internalArray());
123+
if (length() > 0) {
124+
random.nextBytes(internalArray());
125+
}
124126
return this;
125127
}
126128

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public void wrapTest() throws Exception {
5757

5858
Bytes b2 = Bytes.wrap(b);
5959
assertSame(b.array(), b2.array());
60+
61+
Bytes bNullSafe = Bytes.wrapNullSafe(example_bytes_seven);
62+
assertSame(example_bytes_seven, bNullSafe.array());
63+
64+
Bytes bNullSafe1 = Bytes.wrapNullSafe(null);
65+
assertEquals(0, bNullSafe1.length());
66+
}
67+
68+
@Test(expected = NullPointerException.class)
69+
public void wrapTestNullExpected() throws Exception {
70+
Bytes.wrap((byte[]) null);
71+
}
72+
73+
@Test
74+
public void wrapTestNullSafe() throws Exception {
75+
Bytes.wrapNullSafe(null);
6076
}
6177

6278
@Test

0 commit comments

Comments
 (0)