Java Array Element Get getArrays(byte[] inputArray, int arraySize, boolean zeroPad)

Here you can find the source of getArrays(byte[] inputArray, int arraySize, boolean zeroPad)

Description

Convert a large byte array into multiple smaller byte arrays, with the output size determined by the caller

License

Open Source License

Parameter

Parameter Description
inputArray An array of bytes
arraySize The size of each array object returned in the Enumeration
zeroPad Add a padding of zeros if the last array returned is shorter than arraySize

Return

Enumeration An Enumeration of byte arrays of the size specified by the caller

Declaration

public static byte[][] getArrays(byte[] inputArray, int arraySize, boolean zeroPad) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    /**//from ww w  . j av a 2s .co  m
     * Convert a large byte array into multiple smaller byte arrays, with the
     * output size determined by the caller
     * 
     * @param inputArray
     *            An array of bytes
     * @param arraySize
     *            The size of each array object returned in the Enumeration
     * @param zeroPad
     *            Add a padding of zeros if the last array returned is shorter
     *            than arraySize
        
     * @return Enumeration An Enumeration of byte arrays of the size specified
     *         by the caller */
    public static byte[][] getArrays(byte[] inputArray, int arraySize, boolean zeroPad) {
        byte[][] tdba = new byte[(int) Math.ceil(inputArray.length / (double) arraySize)][arraySize];
        int start = 0;
        for (int i = 0; i < tdba.length; i++) {
            if (start + arraySize > inputArray.length) {
                byte[] lastArray;
                if (zeroPad) {
                    lastArray = new byte[arraySize];
                    Arrays.fill(lastArray, (byte) 0x00);
                } else {
                    lastArray = new byte[inputArray.length - start];
                }
                System.arraycopy(inputArray, start, lastArray, 0, inputArray.length - start);
                tdba[i] = lastArray;
            } else {
                System.arraycopy(inputArray, start, tdba[i], 0, arraySize);
            }
            start += arraySize;
        }
        return tdba;
    }
}

Related

  1. getArgs(final String[] args, final String[] defaultValue, final String... flags)
  2. getArgs(String[] args, String... singleArgs)
  3. getArgsOption(String[] args, String option)
  4. getArrayNoNull(Object[] array)
  5. getArrayPreview(final Object[] array, final int previewSize)
  6. getArraySubset(T[] array, int start, int end)
  7. getFrequentElement(int[] bcp)
  8. getFrequentElement(int[] bcp)
  9. getItems(T[] items, int[] indices)