Concatenates two byte arrays. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Concatenates two byte arrays.

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv) throws Exception{
        byte[] a1 = new byte[]{34,35,36,37,37,37,67,68,69};
        byte[] a2 = new byte[]{34,35,36,37,37,37,67,68,69};
        System.out.println(java.util.Arrays.toString(concat(a1,a2)));
    }//from   w w  w. j av  a  2s. com
    /**
     * Concatenates two byte arrays.
     * @param a1
     * @param a2
     * @return concatenated array. the returned array has length = a1.length+a2.length
     */
    final static public byte[] concat(byte[] a1, byte[] a2) {
        byte[] ret = new byte[a1.length + a2.length];
        System.arraycopy(a1, 0, ret, 0, a1.length);
        System.arraycopy(a2, 0, ret, a1.length, a2.length);
        return ret;
    }
}

Related Tutorials