什么是最简单的方式来连接两个byte
阵列?
说,
byte a[];
byte b[];
如何连接两个byte
阵列,并将其存储在另一个byte
阵列?
最简单的:
byte[] c = new byte[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
如果合并包含PDF两个字节数组,这个逻辑将无法正常工作。我们需要使用第三方工具,如PDFBOX Apache的:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
mergePdf.addSource(new ByteArrayInputStream(a));
mergePdf.addSource(new ByteArrayInputStream(b));
mergePdf.setDestinationStream(byteArrayOutputStream);
mergePdf.mergeDocuments();
c = byteArrayOutputStream.toByteArray();
如果你不想使用数组大小一塌糊涂,只需使用字符串连接的法宝:
byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1");
或者在你的代码定义的地方
// concatenation charset
static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1;
和使用
byte[] c = (new String(a, cch) + new String(b, cch)).getBytes(cch);
这,当然,还与使用+
加法操作两个以上的字符串连接。
无论"l1"
和ISO_8859_1
表明,编码每个字符作为单个字节西方拉丁1字符集。由于没有多字节转换是字符串中所执行的字符将具有作为字节的值相同(除了它们将一直被解释成正的值,如char
是无符号的)。至少为Oracle提供的运行时,任何字节将因此被正确地“解码”,然后“编码”一次。
要注意的是字符串做扩展字节数组体贴,需要额外的内存。字符串也可以实习,并因此不容易被删除。字符串也是不变的,所以在他们里面的值不能被破坏。因此,您应该不串联敏感阵列这种方式也不应该用这种方法对于较大的字节数组。让你在做什么,也将被要求作为阵列串联的这种方法是不是一个通用的解决方案的明确指示。
这是我的方式做到这一点!
public static byte[] concatByteArrays(byte[]... inputs) {
int i = inputs.length - 1, len = 0;
for (; i >= 0; i--) {
len += inputs[i].length;
}
byte[] r = new byte[len];
for (i = inputs.length - 1; i >= 0; i--) {
System.arraycopy(inputs[i], 0, r, len -= inputs[i].length, inputs[i].length);
}
return r;
}
特征:
...
)至与任何数量的字节[]的被调用。System.arraycopy()
,保证高速运转。int
和i
变量分配少len
变量。记住:
更好的方式来做到这一点,是复制@Jonathan code。这个问题来自于本地变量数组,因为Java当这种数据类型传递给另一个函数创建新的变量。
最优雅的方式做,这是一个ByteArrayOutputStream
。
byte a[];
byte b[];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
outputStream.write( a );
outputStream.write( b );
byte c[] = outputStream.toByteArray( );
下面是使用Guava的com.google.common.primitives.Bytes
一个很好的解决方案:
byte[] c = Bytes.concat(a, b);
关于这种方法的最大好处是,它有一个可变参数的签名:
public static byte[] concat(byte[]... arrays)
这意味着你可以连接在一个单一的方法调用阵列的任意数量。
另一种可能性是使用java.nio.ByteBuffer
。
就像是
ByteBuffer bb = ByteBuffer.allocate(a.length + b.length + c.length);
bb.put(a);
bb.put(b);
bb.put(c);
byte[] result = bb.array();
// or using method chaining:
byte[] result = ByteBuffer
.allocate(a.length + b.length + c.length)
.put(a).put(b).put(c)
.array();
需要注意的是阵列必须被适当大小的,开始时,因此需要分配线路(如array()
简单地返回背衬阵列,而不采取偏移,位置或限制考虑在内)。
另一种方法是使用效用函数(你能做出这样的通用工具类的静态方法,如果您喜欢):
byte[] concat(byte[]...arrays)
{
// Determine the length of the result array
int totalLength = 0;
for (int i = 0; i < arrays.length; i++)
{
totalLength += arrays[i].length;
}
// create the result array
byte[] result = new byte[totalLength];
// copy the source arrays into the result array
int currentIndex = 0;
for (int i = 0; i < arrays.length; i++)
{
System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
currentIndex += arrays[i].length;
}
return result;
}
调用像这样:
byte[] a;
byte[] b;
byte[] result = concat(a, b);
它也将工作为级联3,4,5个阵列等
这样做,这样让你快速arraycopy代码的优势,这也很容易阅读和维护。
byte[] result = new byte[a.length + b.length];
// copy a to result
System.arraycopy(a, 0, result, 0, a.length);
// copy b to result
System.arraycopy(b, 0, result, a.length, b.length);
您可以使用清洁代码的第三方库,如Apache Commons Lang中,并用它喜欢:
byte[] bytes = ArrayUtils.addAll(a, b);
如果你喜欢ByteBuffer
喜欢@kalefranz,总是有来连接两个byte[]
(甚至更多)在同一行,像这样的posibility:
byte[] c = ByteBuffer.allocate(a.length+b.length).put(a).put(b).array();
对于两个或多个阵列中,可以使用这种简单的和干净的工具方法:
/**
* Append the given byte arrays to one big array
*
* @param arrays The arrays to append
* @return The complete array containing the appended data
*/
public static final byte[] append(final byte[]... arrays) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
if (arrays != null) {
for (final byte[] array : arrays) {
if (array != null) {
out.write(array, 0, array.length);
}
}
}
return out.toByteArray();
}