Here you can find the source of splitAndPad(byte[] byteArray, int blocksize)
Parameter | Description |
---|---|
byteArray | the array. |
blocksize | the block size. |
public static List<byte[]> splitAndPad(byte[] byteArray, int blocksize)
//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; } }