Skip to content

Commit 650087c

Browse files
committed
Add appending with strings
1 parent 296de11 commit 650087c

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
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.6
4+
5+
* add appending with strings
6+
37
## v0.4.5
48

59
* add nullSafe wrapper

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ For **appending** byte arrays or primitive integer types to current instances.
185185
Bytes result = Bytes.wrap(array1).append(array2);
186186
Bytes result = Bytes.wrap(array1).append(1341);
187187
Bytes result = Bytes.wrap(array1).append((byte) 3);
188+
Bytes result = Bytes.wrap(array1).append("some string");
188189
```
189190

190191
**Bitwise operations**: XOR, OR, AND, NOT as well as left and right shifts and switching bits:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>at.favre.lib</groupId>
88
<artifactId>bytes</artifactId>
9-
<version>0.4.5</version>
9+
<version>0.4.6</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Bytes Utility Library</name>

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,17 @@ public Bytes append(byte[] secondArray) {
593593
return transform(new BytesTransformer.ConcatTransformer(secondArray));
594594
}
595595

596+
/**
597+
* Creates a new instance with the current array appended to the provided utf-8 encoded representation of this string
598+
*
599+
* @param string string used to get utf-8 bytes from
600+
* @return appended instance
601+
*/
602+
public Bytes append(CharSequence string) {
603+
Objects.requireNonNull(string);
604+
return transform(new BytesTransformer.ConcatTransformer(string.toString().getBytes(StandardCharsets.UTF_8)));
605+
}
606+
596607
/**
597608
* Bitwise XOR operation on the whole internal byte array.
598609
* See the considerations about possible in-place operation in {@link #transform(BytesTransformer)}.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public void appendPrimitives() throws Exception {
5555
assertArrayEquals(Util.concat(example_bytes_eight, ByteBuffer.allocate(8).putLong(0x6762173671L).array()), Bytes.from(example_bytes_eight).append(0x6762173671L).array());
5656
}
5757

58+
@Test
59+
public void appendString() throws Exception {
60+
assertArrayEquals(new byte[]{0, 0, 48}, Bytes.from(new byte[2]).append("0").array());
61+
assertArrayEquals(new byte[]{48}, Bytes.from(new byte[0]).append("0").array());
62+
assertArrayEquals(new byte[]{71, 117, 116}, Bytes.from(new byte[0]).append("Gut").array());
63+
assertArrayEquals(new byte[]{71, -30, -99, -92}, Bytes.from(new byte[0]).append("G❤").array());
64+
}
65+
5866
@Test
5967
public void appendMulti() throws Exception {
6068
Bytes b = Bytes.random(2);

0 commit comments

Comments
 (0)