Java Array Split splitByteArray(byte[] array, int each)

Here you can find the source of splitByteArray(byte[] array, int each)

Description

Split the byte array into pieces of the given size, the last piece may not have the same size of the others.

License

Open Source License

Parameter

Parameter Description
array the array to split
each the size of each piece

Return

the 2D array with the pieces

Declaration

public static byte[][] splitByteArray(byte[] array, int each) 

Method Source Code

//package com.java2s;

import java.util.Arrays;

public class Main {
    /**//from   w w  w.j  a  v a 2s . c om
     * Split the byte array into pieces of the given size, the last piece may not have the same size of the others.
     *
     * @param array the array to split
     * @param each the size of each piece
     * @return the 2D array with the pieces
     */
    public static byte[][] splitByteArray(byte[] array, int each) {

        int slice = (int) Math.floor(array.length / each);
        byte[][] slices = new byte[slice + 1][];

        int read = 0;

        for (int j1 = 0; j1 < slices.length; j1++) {

            int to = read + each;

            if (to > array.length) {
                to = array.length;
            }

            slices[j1] = Arrays.copyOfRange(array, read, to);
            read += each;
        }

        return slices;
    }
}

Related

  1. splitArray(int[] array, int limit)
  2. splitArray(T[] array, int capacity)
  3. splitArray(T[] array, int max)
  4. splitArray(T[] elementArray, int maxSubArraySize)
  5. splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
  6. splitByteArray(byte[] inByteArray, int length)
  7. splitData(String[] sData, int numMaps)
  8. splitOrderByFirst(byte[] pattern, byte[] src, int offset)
  9. splitSentences(String[] sentence, int limitLength, String reg)