Java – How to join and split byte arrays, byte[]
In this example, we will show you how to join and split byte arrays with ByteBuffer
and System.arraycopy
.
ByteBuffer
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
return ByteBuffer.allocate(byte1.length + byte2.length)
.put(byte1)
.put(byte2)
.array();
}
public static void splitByteArray(byte[] input) {
ByteBuffer bb = ByteBuffer.wrap(input);
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
bb.get(cipher, 0, cipher.length);
bb.get(nonce, 0, nonce.length);
bb.get(extra, 0, extra.length);
}
System.arraycopy
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
byte[] result = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, result, 0, byte1.length);
System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
return result;
}
public static void splitByteArray(byte[] input) {
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
System.arraycopy(input, 0, cipher, 0, cipher.length);
System.arraycopy(input, cipher.length, nonce, 0, nonce.length);
System.arraycopy(input, cipher.length + nonce.length, extra, 0, extra.length);
}
1. Join Byte Arrays
This Java example uses ByteBuffer
or System.arraycopy
to join or concatenate two byte arrays.
JoinByteArrayExample.java
package com.mkyong.nio;
import java.nio.ByteBuffer;
public class JoinByteArrayExample {
public static void main(String[] args) {
String str1 = "Hello World ";
String str2 = "Java";
byte[] bytes = joinByteArray(str1.getBytes(), str2.getBytes());
byte[] bytes2 = joinByteArray2(str1.getBytes(), str2.getBytes());
// byte[] to String
System.out.println("Result : " + new String(bytes));
System.out.println("Result (Hex) : " + convertBytesToHex(bytes));
System.out.println("Result2 : " + new String(bytes2));
System.out.println("Result2 (Hex): " + convertBytesToHex(bytes2));
}
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
return ByteBuffer.allocate(byte1.length + byte2.length)
.put(byte1)
.put(byte2)
.array();
}
public static byte[] joinByteArray2(byte[] byte1, byte[] byte2) {
byte[] result = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, result, 0, byte1.length);
System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
return result;
}
public static String convertBytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte temp : bytes) {
result.append(String.format("%02x", temp));
}
return result.toString();
}
}
Output
Terminal
Result : Hello World Java
Result (Hex) : 48656c6c6f20576f726c64204a617661
Result2 : Hello World Java
Result2 (Hex): 48656c6c6f20576f726c64204a617661
2. Split Byte Array
In Java, we can use ByteBuffer
or System.arraycopy
to split a single byte array into multiple byte arrays.
For example, this 000102030a0b0c0d1a1b1c1d2f2f
is a byte array (14 bytes) in hex representation, it is a combined of cipher (8 bytes) + nonce (4 bytes) + extra (2 bytes).
000102030a0b0c0d | 1a1b1c1d | 2f2f
cipher | nonce | extra
SplitByteArrayExample.java
package com.mkyong.nio;
import java.nio.ByteBuffer;
public class SplitByteArrayExample {
public static void main(String[] args) {
byte[] input = {0x00, 0x01, 0x02, 0x03, 0x0a, 0x0b, 0x0c, 0x0d, 0x1a, 0x1b, 0x1c, 0x1d, 0x2f, 0x2f};
if (input.length != 14) {
throw new IllegalArgumentException("input must be 14 bytes.");
}
ByteBuffer bb = ByteBuffer.wrap(input);
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
bb.get(cipher, 0, cipher.length);
bb.get(nonce, 0, nonce.length);
bb.get(extra, 0, extra.length);
System.out.println("Input (Hex) : " + convertBytesToHex(input));
System.out.println("\n--- ByteBuffer ---");
System.out.println("Cipher(Hex) : " + convertBytesToHex(cipher));
System.out.println("Nonce (Hex) : " + convertBytesToHex(nonce));
System.out.println("Nonce (Hex) : " + convertBytesToHex(extra));
byte[] cipher2 = new byte[8];
byte[] nonce2 = new byte[4];
byte[] extra2 = new byte[2];
System.arraycopy(input, 0, cipher2, 0, cipher2.length);
System.arraycopy(input, cipher2.length, nonce2, 0, nonce2.length);
System.arraycopy(input, cipher2.length + nonce2.length, extra2, 0, extra2.length);
System.out.println("\n--- System.arraycopy ---");
System.out.println("Cipher2 (Hex) : " + convertBytesToHex(cipher2));
System.out.println("Nonce2 (Hex) : " + convertBytesToHex(nonce2));
System.out.println("Nonce2 (Hex) : " + convertBytesToHex(extra2));
}
public static String convertBytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte temp : bytes) {
result.append(String.format("%02x", temp));
}
return result.toString();
}
}
Output
Terminal
Input (Hex) : 000102030a0b0c0d1a1b1c1d2f2f
--- ByteBuffer ---
Cipher(Hex) : 000102030a0b0c0d
Nonce (Hex) : 1a1b1c1d
Nonce (Hex) : 2f2f
--- System.arraycopy ---
Cipher2 (Hex) : 000102030a0b0c0d
Nonce2 (Hex) : 1a1b1c1d
Nonce2 (Hex) : 2f2f
Nice post!
you can simplify the ByteBuffer split by using
ByteBuffer::get(byte[])
bb.get(cipher, 0, cipher.length);
bb.get(nonce, 0, nonce.length);
bb.get(extra, 0, extra.length);
is equivalent to
bb.get(cipher);
bb.get(nonce);
bb.get(extra);
I see… thanks for your input.