2011-04-16 72 views
50

我有两个字节数组,我想知道如何去添加一个到另一个或组合它们以形成一个新的字节数组。如何结合两个字节数组

+0

如果你添加它们,那么你将如何处理溢出? – maerics 2011-04-16 00:05:30

+0

你是什么意思溢出? – novicePrgrmr 2011-04-16 00:06:43

+0

你在哪里做任何补充? – 2011-04-16 00:07:08

回答

89

你只是想连接两个字节数组?

byte[] one = getBytesForOne(); 
byte[] two = getBytesForTwo(); 
byte[] combined = new byte[one.length + two.length]; 

for (int i = 0; i < combined.length; ++i) 
{ 
    combined[i] = i < one.length ? one[i] : two[i - one.length]; 
} 

或者你可以使用System.arraycopy:

byte[] one = getBytesForOne(); 
byte[] two = getBytesForTwo(); 
byte[] combined = new byte[one.length + two.length]; 

System.arraycopy(one,0,combined,0   ,one.length); 
System.arraycopy(two,0,combined,one.length,two.length); 

或者你可以只用一个列表做的工作:

byte[] one = getBytesForOne(); 
byte[] two = getBytesForTwo(); 

List<Byte> list = new ArrayList<Byte>(Arrays.<Byte>asList(one)); 
list.addAll(Arrays.<Byte>asList(two)); 

byte[] combined = list.toArray(new byte[list.size()]); 
+6

或者您可以使用System.arraycopy() – 2011-04-16 00:42:34

+1

良好的调用。我从来没有见过'System.arraycopy();' – pickypg 2011-04-16 00:48:18

+0

并且我个人总是使用System.arraycopy,通常使用包装器方法来处理常见的情况......实际上我早已写了一个'ArrayUtil.resize'方法。 – 2011-04-16 00:50:07

2

假设您的byteData阵列比32 + byteSalt.length()更大......您将选择它的长度,而不是byteSalt.length。您正在尝试从数组末尾进行复制。

2
String temp = passwordSalt; 
byte[] byteSalt = temp.getBytes(); 
int start = 32; 
for (int i = 0; i < byteData.length; i ++) 
{ 
    byteData[start + i] = byteSalt[i]; 
} 

与您的代码这里的问题是用于索引数组的变量i将通过byteSalt数组和byteData数组。因此,确保byteData的尺寸是至少passwordSalt字符串的最大长度加上32.什么会纠正它替换以下行:

for (int i = 0; i < byteData.length; i ++) 

有:

for (int i = 0; i < byteSalt.length; i ++) 
17

你可以使用Apace common lang包(org.apache.commons.lang.ArrayUtils类)执行此操作。你需要做的

byte[] concatBytes = ArrayUtils.addAll(one,two); 
+0

它快吗。我需要一个非常快速的方法。没有循环的开销。 – 2014-10-01 05:26:45

+1

您对开销的恐惧让我觉得,当您执行此循环时,您的UI会受到冻结的困扰。也许你应该考虑一个'AsyncTask'(android),这是阻止循环中断你的UI线程的唯一方法。 – 1owk3y 2015-07-27 13:14:16

1

我用这个代码工作的很好只是appendData,要么通过一个字节一个数组,或者两个数组把它们结合在一起的情况如下:

protected byte[] appendData(byte firstObject,byte[] secondObject){ 
    byte[] byteArray= {firstObject}; 
    return appendData(byteArray,secondObject); 
} 

protected byte[] appendData(byte[] firstObject,byte secondByte){ 
    byte[] byteArray= {secondByte}; 
    return appendData(firstObject,byteArray); 
} 

protected byte[] appendData(byte[] firstObject,byte[] secondObject){ 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    try { 
     if (firstObject!=null && firstObject.length!=0) 
      outputStream.write(firstObject); 
     if (secondObject!=null && secondObject.length!=0) 
      outputStream.write(secondObject); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return outputStream.toByteArray(); 
} 
2

我认为这是最好的办法,

public static byte[] addAll(final byte[] array1, byte[] array2) { 
    byte[] joinedArray = Arrays.copyOf(array1, array1.length + array2.length); 
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); 
    return joinedArray; 
}