Concatenate two or more Byte Arrays in java

By | July 4, 2020
byte[] concat(byte[] a, byte[] b, byte[]... n) {
    int size, pos;

    pos = a.length + b.length;
    size = pos;

    for (byte[] e : n) {
        size += e.length;
    }
    byte[] r = new byte[size];

    System.arraycopy(a, 0, r, 0, a.length);
    System.arraycopy(b, 0, r, a.length, b.length);

    for (byte[] e : n) {
        System.arraycopy(e, 0, r, pos, e.length);
        pos += e.length;
    }
    return r;
}

Leave a Reply

Your email address will not be published. Required fields are marked *