在此示例中,我们将向您展示如何使用ByteBuffer
和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.连接字节数组
此Java示例使用ByteBuffer
或System.arraycopy
来连接或连接两个字节数组。
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();
}
}
输出量
Terminal
Result : Hello World Java
Result (Hex) : 48656c6c6f20576f726c64204a617661
Result2 : Hello World Java
Result2 (Hex): 48656c6c6f20576f726c64204a617661
2.分割字节数组
在Java中,我们可以使用ByteBuffer
或System.arraycopy
将单个字节数组拆分为多个字节数组。
例如,此000102030a0b0c0d1a1b1c1d2f2f
以十六进制表示000102030a0b0c0d1a1b1c1d2f2f
是一个字节数组(14个字节),它是密码(8个字节)+随机数(4个字节)+额外(2个字节)的组合。
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();
}
}
输出量
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
参考文献
翻译自: https://mkyong.com/java/java-how-to-join-and-split-byte-arrays-byte/