Java Array Divide divideByteArrayToChunks(byte data[], int chunkSize)

Here you can find the source of divideByteArrayToChunks(byte data[], int chunkSize)

Description

Breaks down a byte[] to several sub-arrays specified by the chunkSize.

License

Open Source License

Parameter

Parameter Description
data The input <code>byte[]</code> to be divided into pieces.
chunkSize The length of each chunk.

Return

An array of smaller arrays.

Declaration

public static byte[][] divideByteArrayToChunks(byte data[], int chunkSize) 

Method Source Code

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

public class Main {
    /**/*w ww .j  a v a 2s . c o  m*/
     * <p>
     * Breaks down a <code>byte[]</code> to several sub-arrays specified by 
     * the <code>chunkSize</code>.
     * </p>
     * <p>
     * <strong>Note:</strong> when using this in relation to an <code>
     * Preference</code> object in order to store a large <code>byte[]</code>
     * take a look at {@link Preferences.MAX_VALUE_LENGTH}. Important is that 
     * the array-length is limited to <code>MAX_VALUE_LENGTH * 3 / 4</code>.
     * </p>
     * @param data The input <code>byte[]</code> to be divided into pieces.
     * @param chunkSize The length of each chunk.
     * @return An array of smaller arrays.
     */
    public static byte[][] divideByteArrayToChunks(byte data[], int chunkSize) {
        int numChunks = (data.length + chunkSize - 1) / chunkSize;
        byte chunks[][] = new byte[numChunks][];

        for (int i = 0; i < numChunks; ++i) {
            int startByte = i * chunkSize;
            int endByte = startByte + chunkSize;
            if (endByte > data.length)
                endByte = data.length;
            int length = endByte - startByte;
            chunks[i] = new byte[length];

            System.arraycopy(data, startByte, chunks[i], 0, length);
        }
        return chunks;
    }
}

Related

  1. divideAndConquer(int[] array, int num)
  2. divideArray(float[] array, float num)
  3. divideArray(T[] arr, Integer... cuts)
  4. divideArrayElements(final int[] array, final int divisor)
  5. divideBytearrays(byte[] ret, int headerLength, byte[] bArr)
  6. divideElements(double[] v1, double[] v2, double[] rs)
  7. divideElements(final int j, final int numRows, final double[] u, final int startU, final double realA, final double imagA)
  8. divideElements(final int j, final int numRows, final float[] u, final float u_0)
  9. divideElements4arg(final int j, final int numRows, final double[] u, final double u_0)