Here you can find the source of divideByteArrayToChunks(byte data[], int chunkSize)
Breaks down a byte[]
to several sub-arrays specified by the chunkSize
.
Parameter | Description |
---|---|
data | The input <code>byte[]</code> to be divided into pieces. |
chunkSize | The length of each chunk. |
public static byte[][] divideByteArrayToChunks(byte data[], int chunkSize)
//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; } }