Concatenates two byte arrays. - Java java.lang

Java examples for java.lang:byte Array

Description

Concatenates two byte arrays.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] a = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(concatenate(a, b)));
    }/*from w w  w  . j  a v  a 2  s  .c  o  m*/

    /**
     * Concatenates two byte arrays.
     * 
     * @param a
     *            the first array.
     * @param b
     *            the second array.
     * @return the concatenated array.
     */
    public static byte[] concatenate(byte[] a, byte[] b) {
        int lengthA = a.length;
        int lengthB = b.length;

        byte[] concat = new byte[lengthA + lengthB];

        System.arraycopy(a, 0, concat, 0, lengthA);
        System.arraycopy(b, 0, concat, lengthA, lengthB);

        return concat;
    }
}

Related Tutorials