Skip to content

Commit 639d91d

Browse files
committed
Add append() method supporting multiple byte arrays
fixes #26
1 parent 9e61b07 commit 639d91d

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
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+
## v1.0.0
4+
5+
* add `append()` method supporting multiple byte arrays #26
6+
37
## v0.8.0
48

59
* add radix encoding/parsing and fix radix tests #6, #20

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,14 @@
2121

2222
package at.favre.lib.bytes;
2323

24-
import java.io.ByteArrayInputStream;
25-
import java.io.DataInput;
26-
import java.io.File;
27-
import java.io.InputStream;
28-
import java.io.Serializable;
24+
import java.io.*;
2925
import java.math.BigInteger;
30-
import java.nio.ByteBuffer;
31-
import java.nio.ByteOrder;
32-
import java.nio.CharBuffer;
33-
import java.nio.IntBuffer;
34-
import java.nio.ReadOnlyBufferException;
26+
import java.nio.*;
3527
import java.nio.charset.Charset;
3628
import java.nio.charset.StandardCharsets;
3729
import java.security.SecureRandom;
3830
import java.text.Normalizer;
39-
import java.util.Arrays;
40-
import java.util.BitSet;
41-
import java.util.Collection;
42-
import java.util.Iterator;
43-
import java.util.List;
44-
import java.util.Objects;
45-
import java.util.Random;
46-
import java.util.UUID;
31+
import java.util.*;
4732

4833
/**
4934
* Bytes is wrapper class for an byte-array that allows a lot of convenience operations on it:
@@ -777,6 +762,18 @@ public Bytes append(long long8Bytes) {
777762
return append(Bytes.from(long8Bytes));
778763
}
779764

765+
/**
766+
* Creates a new instance with the current array appended to the provided data (ie. append at the end).
767+
* You may use this to append multiple byte arrays without the need for chaining the {@link #append(byte[])} call
768+
* and therefore generating intermediate copies of the byte array, making this approach use less memory.
769+
*
770+
* @param arrays to append
771+
* @return appended instance
772+
*/
773+
public Bytes append(byte[]... arrays) {
774+
return append(Bytes.from(arrays));
775+
}
776+
780777
/**
781778
* Creates a new instance with the current array appended to the provided data (ie. append at the end)
782779
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public void append() {
4747
assertArrayEquals(Util.concat(example_bytes_eight, example_bytes_sixteen), Bytes.from(example_bytes_eight).append(Bytes.from(example_bytes_sixteen)).array());
4848
}
4949

50+
@Test
51+
public void appendMultipleByteArrays() {
52+
assertArrayEquals(new byte[0], Bytes.from(new byte[0]).append(new byte[0], new byte[0]).array());
53+
assertArrayEquals(new byte[]{0x0, 0x01, 0x02, 0x03}, Bytes.from(new byte[]{0x0}).append(new byte[]{0x1}, new byte[]{0x2}, new byte[]{0x3}).array());
54+
assertArrayEquals(Util.concat(example_bytes_seven, example_bytes_one, example_bytes_sixteen), Bytes.from(example_bytes_seven).append(example_bytes_one, example_bytes_sixteen).array());
55+
assertArrayEquals(Util.concat(example_bytes_sixteen, example_bytes_sixteen, example_bytes_sixteen), Bytes.from(example_bytes_sixteen).append(example_bytes_sixteen, example_bytes_sixteen).array());
56+
assertArrayEquals(Util.concat(example_bytes_two, example_bytes_seven, example_bytes_twentyfour, example_bytes_eight), Bytes.from(example_bytes_two).append(example_bytes_seven, example_bytes_twentyfour, example_bytes_eight).array());
57+
assertArrayEquals(Util.concat(example_bytes_two, example_bytes_seven, example_bytes_twentyfour, example_bytes_one, example_bytes_sixteen), Bytes.from(example_bytes_two).append(example_bytes_seven, example_bytes_twentyfour).append(example_bytes_one, example_bytes_sixteen).array());
58+
}
59+
5060
@Test
5161
public void appendNullSafe() {
5262
assertArrayEquals(new byte[0], Bytes.from(new byte[0]).appendNullSafe(new byte[0]).array());

0 commit comments

Comments
 (0)