Skip to content

Commit 5eb7dca

Browse files
committed
Add boolean constructor
1 parent 743a6ab commit 5eb7dca

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v0.4.6
44

55
* add appending with strings
6+
* add boolean constructor
67

78
## v0.4.5
89

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ public static Bytes from(byte firstByte, byte... moreBytes) {
238238
return wrap(Util.concatVararg(firstByte, moreBytes));
239239
}
240240

241+
/**
242+
* Creates a new instance from given boolean.
243+
* This will create a new single array element array instance using the convention that false is zero.
244+
* E.g. Creates array <code>new byte[] {1}</code> if booleanValue is true and <code>new byte[] {0}</code> if
245+
* booleanValue is false.
246+
*
247+
* @param booleanValue to convert (false is zero, true is one)
248+
* @return new instance
249+
*/
250+
public static Bytes from(boolean booleanValue) {
251+
return wrap(new byte[]{booleanValue ? (byte) 1 : 0});
252+
}
253+
241254
/**
242255
* Creates a new instance from given unsigned 2 byte char.
243256
*

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ private void checkBigInteger(byte[] array) {
128128
assertEquals(bigInteger, Bytes.from(bigInteger).toBigInteger());
129129
}
130130

131+
@Test
132+
public void fromBoolean() throws Exception {
133+
assertArrayEquals(new byte[]{(byte) 1}, Bytes.from(true).array());
134+
assertArrayEquals(new byte[]{(byte) 0}, Bytes.from(false).array());
135+
}
136+
131137
@Test
132138
public void fromByte() throws Exception {
133139
byte test = 0x4E;

0 commit comments

Comments
 (0)