Java Byte Array Combine combine(byte[]... bytes)

Here you can find the source of combine(byte[]... bytes)

Description

Merge multiple Byte Arrays into one

License

Open Source License

Parameter

Parameter Description
bytes Array to be merged together

Return

Merged Arrays

Declaration

public static byte[] combine(byte[]... bytes) 

Method Source Code

//package com.java2s;
/*//from   w  ww .j  av  a  2  s. c  o  m
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Merge multiple Byte Arrays into one
     *
     * @param bytes Array to be merged together
     * @return Merged Arrays
     */
    public static byte[] combine(byte[]... bytes) {

        if (bytes == null) {
            return null;
        } else if (bytes.length == 1) {
            return bytes[0];
        }

        int combinedLength = 0;
        for (byte[] _bytes : bytes) {
            if (_bytes != null) {
                combinedLength += _bytes.length;
            }
        }

        byte[] combined = new byte[combinedLength];
        int shift = 0;
        for (byte[] aByte : bytes) {
            if (aByte != null) {
                System.arraycopy(aByte, 0, combined, shift, aByte.length);
                shift += aByte.length;
            }
        }
        return combined;
    }
}

Related

  1. combine(byte[] a, byte[] b)
  2. combine(byte[] arr1, byte[] arr2)
  3. combine(byte[] first, byte[] second)
  4. combine(byte[] one, byte[] two, byte[] three, byte[] four)
  5. combine(byte[] one, byte[] two, byte[] three, byte[] four)
  6. combine(byte[][] arr)
  7. combine(final byte[] array1, final byte[] array2)
  8. combineArrays(byte[]... a)
  9. combineInsertB2sizeAsByte(byte[] b1, byte[] b2)