java combine to byte[] code example

Example 1: java combine to byte[]

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

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

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

Example 2: java combine to byte[]

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

Example 3: java combine to byte[]

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

Example 4: java combine to byte[]

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

Tags:

Misc Example