How to combine two byte arrays

Java

Java Problem Overview


I have two byte arrays and I am wondering how I would go about adding one to the other or combining them to form a new byte array.

Java Solutions


Solution 1 - Java

You're just trying to concatenate the two byte arrays?

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];
}

Or you could use 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);

Or you could just use a List to do the work:

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()]);

Or you could simply use ByteBuffer with the advantage of adding many arrays.

byte[] allByteArray = new byte[one.length + two.length + three.length];

ByteBuffer buff = ByteBuffer.wrap(allByteArray);
buff.put(one);
buff.put(two);
buff.put(three);

byte[] combined = buff.array();

Solution 2 - Java

You can do this by using Apace common lang package (org.apache.commons.lang.ArrayUtils class ). You need to do the following

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

Solution 3 - Java

I think it is best approach,

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;
}

Solution 4 - Java

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

The problem with your code here is that the variable i that is being used to index the arrays is going past both the byteSalt array and the byteData array. So, Make sure that byteData is dimensioned to be at least the maximum length of the passwordSalt string plus 32. What will correct it is replacing the following line:

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

with:

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

Solution 5 - Java

The simplest method (inline, assuming a and b are two given arrays):

byte[] c = (new String(a, cch) + new String(b, cch)).getBytes(cch);

This, of course, works with more than two summands and uses a concatenation charset, defined somewhere in your code:

static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1;

Or, in more simple form, without this charset:

byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1");

But you need to suppress UnsupportedEncodingException which is unlikely to be thrown.


The fastest method:

public static byte[] concat(byte[] a, byte[] b) {
    int lenA = a.length;
    int lenB = b.length;
    byte[] c = Arrays.copyOf(a, lenA + lenB);
    System.arraycopy(b, 0, c, lenA, lenB);
    return c;
}

Solution 6 - Java

Assuming your byteData array is biger than 32 + byteSalt.length()...you're going to it's length, not byteSalt.length. You're trying to copy from beyond the array end.

Solution 7 - Java

I've used this code which works quite well just do appendData and either pass a single byte with an array, or two arrays to combine them :

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();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnovicePrgrmrView Question on Stackoverflow
Solution 1 - JavapickypgView Answer on Stackoverflow
Solution 2 - JavaCJProView Answer on Stackoverflow
Solution 3 - JavacagdasView Answer on Stackoverflow
Solution 4 - JavaBob BryanView Answer on Stackoverflow
Solution 5 - JavaJohn McClaneView Answer on Stackoverflow
Solution 6 - JavaMichael KView Answer on Stackoverflow
Solution 7 - JavaJeremie DView Answer on Stackoverflow