Concatenate two or more Byte Arrays in Java
This post will discuss how to concatenate two or more byte arrays in Java.
1. Using ByteArrayOutputStream
The recommended solution to concatenate two or more byte arrays is using ByteArrayOutputStream
. The idea is to write bytes from each of the byte arrays to the output stream, and then call toByteArray()
to get the current contents of the output stream as a byte array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.ByteArrayOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { byte[] first = "Hello".getBytes(); byte[] second = "World".getBytes(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(first); outputStream.write(second); byte[] result = outputStream.toByteArray(); System.out.println(new String(result)); } } |
Output:
HelloWorld
To concatenate multiple byte arrays, you can write a custom utility method that takes varargs, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.ByteArrayOutputStream; import java.util.Arrays; import java.util.Objects; public class Main { public static byte[] concat(byte[]... arrays) { ByteArrayOutputStream out = new ByteArrayOutputStream(); if (arrays != null) { Arrays.stream(arrays).filter(Objects::nonNull) .forEach(array -> out.write(array, 0, array.length)); } return out.toByteArray(); } public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = ", ".getBytes(); byte[] third = "World".getBytes(); byte[] result = concat(first, second, third); System.out.println(new String(result)); } } |
Output:
Hello, World
2. Using System.arraycopy()
method
Another elegant solution is to make two calls to System.arraycopy(), which can efficiently copy contents of an array to the destination array. Here’s the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = "World".getBytes(); byte[] result = new byte[first.length + second.length]; System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length); System.out.println(new String(result)); } } |
Output:
HelloWorld
To concatenate multiple byte arrays, you can create a utility method that takes varargs, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Arrays; import java.util.Objects; public class Main { public static byte[] concat(byte[]... arrays) { int len = Arrays.stream(arrays).filter(Objects::nonNull) .mapToInt(s -> s.length).sum(); byte[] result = new byte[len]; int lengthSoFar = 0; if (arrays != null) { for (byte[] array: arrays) { if (array != null) { System.arraycopy(array, 0, result, lengthSoFar, array.length); lengthSoFar += array.length; } } } return result; } public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = ", ".getBytes(); byte[] third = "World".getBytes(); byte[] result = concat(first, second, third); System.out.println(new String(result)); } } |
Output:
Hello, World
3. Using Guava
Guava Bytes class provides several static utility methods pertaining to byte primitives. To concatenate multiple byte arrays, you can use the Bytes.concat()
method, which can take any number of arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.primitives.Bytes; public class Main { public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = ", ".getBytes(); byte[] third = "World".getBytes(); byte[] result = Bytes.concat(first, second, third); System.out.println(new String(result)); } } |
Output:
Hello, World
4. Using Apache Commons Lang Library
Apache Commons Lang also provides static utility methods pertaining to byte primitives. One such method is ArrayUtils.addAll()
, which can join two arrays and is overloaded to accept any type.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.ArrayUtils; public class Main { public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = "World".getBytes(); byte[] result = ArrayUtils.addAll(first, second); System.out.println(new String(result)); } } |
Output:
HelloWorld
5. Using ByteBuffer
The ByteBuffer
class in Java, as the name suggests, is a byte buffer. You can use its put()
method to transfer contiguous sequences of bytes from a byte array into the buffer, and the array()
method to return the byte array that backs this buffer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.nio.ByteBuffer; public class Main { public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = "World".getBytes(); ByteBuffer byteBuffer = ByteBuffer.allocate(first.length + second.length); byteBuffer.put(first); byteBuffer.put(second); byte[] result = byteBuffer.array(); System.out.println(new String(result)); } } |
Output:
HelloWorld
The put()
method returns the buffer upon which it is invoked. This allows successive method invocations to be chained:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.nio.ByteBuffer; public class Main { public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = "World".getBytes(); byte[] result = ByteBuffer.allocate(first.length + second.length) .put(first) .put(second) .array(); System.out.println(new String(result)); } } |
Output:
HelloWorld
To concatenate multiple byte arrays, you can create a static utility method that takes varargs, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Objects; public class Main { public static byte[] concat(byte[]... arrays) { int len = Arrays.stream(arrays).filter(Objects::nonNull) .mapToInt(s -> s.length).sum(); ByteBuffer byteBuffer = ByteBuffer.allocate(len); if (arrays != null) { Arrays.stream(arrays).filter(Objects::nonNull).forEach(byteBuffer::put); } return byteBuffer.array(); } public static void main(String[] args) { byte[] first = "Hello".getBytes(); byte[] second = ", ".getBytes(); byte[] third = "World".getBytes(); byte[] result = concat(first, second, third); System.out.println(new String(result)); } } |
Output:
Hello, World
That’s all about concatenate two or more byte arrays in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)