Java List Merge merge(List bs)

Here you can find the source of merge(List bs)

Description

merge

License

Apache License

Declaration

public static byte[] merge(List<byte[]> bs) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {

    public static byte[] merge(List<byte[]> bs) {
        int len = 0;
        for (int i = 0; i < bs.size(); i++) {
            len += bs.get(i).length;/* w w w . j  a va2 s  .co  m*/
        }
        byte[] merged = new byte[len];
        int x = 0;
        for (int i = 0; i < bs.size(); i++) {
            byte[] b = bs.get(i);
            int blen = b.length;
            // copy(byte[] src, int srcPos, byte[] dest, int destPos, int length)
            copy(b, 0, merged, x, blen);
            x += blen;
        }

        return merged;
    }

    public static byte[] merge(byte[]... bs) {
        int len = 0;
        for (int i = 0; i < bs.length; i++) {
            len += bs[i].length;
        }
        byte[] merged = new byte[len];
        int x = 0;
        for (int i = 0; i < bs.length; i++) {
            byte[] b = bs[i];
            int blen = b.length;
            // copy(byte[] src, int srcPos, byte[] dest, int destPos, int length)
            copy(b, 0, merged, x, blen);
            x += blen;
        }

        return merged;
    }

    /**
     * copy bytes from src to dest
     * 
     * @param src
     * @param srcPos
     * @param dest
     * @param destPos
     * @param length
     */
    public static void copy(byte[] src, int srcPos, byte[] dest, int destPos, int length) {
        System.arraycopy(src, srcPos, dest, destPos, length);
    }

    /**
     * copy bytes from src to dest
     * 
     * @param src
     * @param dest
     * @param destPos
     */
    public static void copy(byte[] src, byte[] dest, int destPos) {
        System.arraycopy(src, 0, dest, destPos, src.length);
    }

    /**
     * dest[destPos] = src;
     * 
     * @param src
     * @param dest
     * @param destPos
     */
    public static void copy(byte src, byte[] dest, int destPos) {
        dest[destPos] = src;
    }
}

Related

  1. merge(final List list, final String seperator)
  2. merge(final List a, final List b)
  3. merge(final List aList, final List bList)
  4. merge(final String[] input, final String[] list)
  5. merge(List original, List merger)
  6. merge(List left, List right)
  7. merge(List list1, List list2)
  8. merge(List> list)
  9. merge(List l1, List l2)