Concatenate byte arrays in Kotlin
This article explores different ways to concatenate byte arrays in Kotlin.
1. Using + operator
The standard solution to concatenate byte arrays in Kotlin is using the + operator. It returns an array containing all elements of the two arrays. For example,
|
1 2 3 4 5 6 7 |
fun main() { val first = "Apple".toByteArray() val second = "Google".toByteArray() val result: ByteArray = first + second println(String(result)) } |
We can also use the plus() function, which returns an array containing all elements of the original array and followed by all elements of the specified array. This is effectively the same as using the + operator.
|
1 2 3 4 5 6 7 |
fun main() { val first = "Apple".toByteArray() val second = "Google".toByteArray() val result: ByteArray = first.plus(second) println(String(result)) } |
2. Using ByteArrayOutputStream
The idea here is to write bytes from each of the byte arrays to the output stream and call the toByteArray() function to get the output stream’s contents as a byte array. This can be implemented as follows in Kotlin using vararg.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.ByteArrayOutputStream fun concat(vararg arrays: ByteArray): ByteArray { val out = ByteArrayOutputStream() arrays.forEach { out.write(it) } return out.toByteArray() } fun main() { val first = "Apple".toByteArray() val second = ", ".toByteArray() val third = "Google".toByteArray() val result = concat(first, second, third) println(String(result)) } |
Output:
Apple, Google
3. Using ByteBuffer
Another solution is to use the ByteBuffer#put() function to transfer bytes from each of the byte arrays into a buffer and call the array() function to get the byte array from the buffer.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.nio.ByteBuffer fun main() { val first = "Apple".toByteArray() val second = "Google".toByteArray() val result = ByteBuffer.allocate(first.size + second.size) .put(first) .put(second) .array() println(String(result)) } |
Output:
AppleGoogle
To concatenate the arbitrary number of byte arrays, create a utility function that takes varargs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.nio.ByteBuffer fun concat(vararg arrays: ByteArray): ByteArray { val n = arrays.map {it.size}.sum() val byteBuffer = ByteBuffer.allocate(n) arrays.forEach { byteBuffer.put(it) } return byteBuffer.array() } fun main() { val first = "Apple".toByteArray() val second = ", ".toByteArray() val third = "Google".toByteArray() val result = concat(first, second, third) println(String(result)) } |
Output:
Apple, Google
4. Using System.arraycopy() function
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 15 16 17 18 19 20 |
fun concat(vararg arrays: ByteArray): ByteArray { val len = arrays.map { it.size }.sum() val result = ByteArray(len) var lengthSoFar = 0 for (array in arrays) { System.arraycopy(array, 0, result, lengthSoFar, array.size) lengthSoFar += array.size } return result } fun main() { val first = "Apple".toByteArray() val second = ", ".toByteArray() val third = "Google".toByteArray() val result = concat(first, second, third) println(String(result)) } |
Output:
Apple, Google
That’s all about concatenating byte arrays in Kotlin.
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 :)