Java Array Concatenate concatAll(byte[] first, byte[]... rest)

Here you can find the source of concatAll(byte[] first, byte[]... rest)

Description

concat All

License

Open Source License

Declaration

public static byte[] concatAll(byte[] first, byte[]... rest) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    public static byte[] concatAll(byte[] first, byte[]... rest) {
        int totalLength = (first == null) ? 0 : first.length;
        for (byte[] array : rest) {
            totalLength += (array == null) ? 0 : array.length;
        }/*from   w ww  .  j  av a 2  s  . c o  m*/

        byte[] result = (first == null) ? null : Arrays.copyOf(first, totalLength);
        int offset = (first == null) ? 0 : first.length;

        for (byte[] array : rest) {
            if (array != null) {
                System.arraycopy(array, 0, result, offset, array.length);
                offset += array.length;
            }
        }

        return result;
    }

    public static int[] concatAll(int[] first, int[]... rest) {
        int totalLength = (first == null) ? 0 : first.length;
        for (int[] array : rest) {
            totalLength += (array == null) ? 0 : array.length;
        }

        int[] result = (first == null) ? null : Arrays.copyOf(first, totalLength);
        int offset = (first == null) ? 0 : first.length;

        for (int[] array : rest) {
            if (array != null) {
                System.arraycopy(array, 0, result, offset, array.length);
                offset += array.length;
            }
        }

        return result;
    }

    public static long[] concatAll(long[] first, long[]... rest) {
        int totalLength = (first == null) ? 0 : first.length;
        for (long[] array : rest) {
            totalLength += (array == null) ? 0 : array.length;
        }

        long[] result = (first == null) ? null : Arrays.copyOf(first, totalLength);
        int offset = (first == null) ? 0 : first.length;

        for (long[] array : rest) {
            if (array != null) {
                System.arraycopy(array, 0, result, offset, array.length);
                offset += array.length;
            }
        }

        return result;
    }

    @SafeVarargs
    public static <T> T[] concatAll(T[] first, T[]... rest) {
        int totalLength = (first == null) ? 0 : first.length;
        for (T[] array : rest) {
            totalLength += (array == null) ? 0 : array.length;
        }

        T[] result = (first == null) ? null : Arrays.copyOf(first, totalLength);
        int offset = (first == null) ? null : first.length;

        for (T[] array : rest) {
            if (array != null) {
                System.arraycopy(array, 0, result, offset, array.length);
                offset += array.length;
            }
        }

        return result;
    }
}

Related

  1. concat(T[] left, T[] right)
  2. concat(T[] objects)
  3. concat(T[]... arrays)
  4. concat(T[]... tss)
  5. concatAll(byte[] a, byte[]... rest)
  6. concatAll(byte[]... args)
  7. concatAll(T[] first, T[]... rest)
  8. concatAll(T[] first, T[]... rest)
  9. concatAllArrays(byte[] data1, byte[]... remaining)