Java Array Split splitAndPad(byte[] byteArray, int blocksize)

Here you can find the source of splitAndPad(byte[] byteArray, int blocksize)

Description

Splits the given array into blocks of given size and adds padding to the last one, if necessary.

License

Open Source License

Parameter

Parameter Description
byteArray the array.
blocksize the block size.

Return

a list of blocks of given size.

Declaration

public static List<byte[]> splitAndPad(byte[] byteArray, int blocksize) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    /**/* ww  w.j ava2 s .  c o m*/
     * Splits the given array into blocks of given size and adds padding to the
     * last one, if necessary.
     * 
     * @param byteArray
     *            the array.
     * @param blocksize
     *            the block size.
     * @return a list of blocks of given size.
     */
    public static List<byte[]> splitAndPad(byte[] byteArray, int blocksize) {
        List<byte[]> blocks = new ArrayList<byte[]>();
        int numBlocks = (int) Math.ceil(byteArray.length / (double) blocksize);

        for (int i = 0; i < numBlocks; i++) {

            byte[] block = new byte[blocksize];
            Arrays.fill(block, (byte) 0x00);
            if (i + 1 == numBlocks) {
                // the last block
                int remainingBytes = byteArray.length - (i * blocksize);
                System.arraycopy(byteArray, i * blocksize, block, 0, remainingBytes);
            } else {
                System.arraycopy(byteArray, i * blocksize, block, 0, blocksize);
            }
            blocks.add(block);
        }

        return blocks;
    }
}

Related

  1. split(byte[] source, int c)
  2. split(Collection collection, C[] array, int pageSize)
  3. split(final int[] array)
  4. split(int splitBefore, byte[] source)
  5. split(String string, String delim, String[] a)
  6. splitArray(byte[] src, int size)
  7. splitArray(int[] array, int limit)
  8. splitArray(T[] array, int capacity)
  9. splitArray(T[] array, int max)