Here you can find the source of toArray(final byte[] byteArray, final int sizeLimit)
Parameter | Description |
---|---|
byteArray | The array to segment. |
sizeLimit | The maximum size of each byte buffer. |
public static ByteBuffer[] toArray(final byte[] byteArray, final int sizeLimit)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; public class Main { /**// w w w . j a v a 2 s .co m * Splits the specified byte array into an array of {@link ByteBuffer}s, * each with the specified maximum size. This is useful, for example, * when trying to limit sizes to a network MTU. * * @param byteArray The array to segment. * @param sizeLimit The maximum size of each byte buffer. * @return The new array of {@link ByteBuffer}s. */ public static ByteBuffer[] toArray(final byte[] byteArray, final int sizeLimit) { final int numBufs = (int) Math.ceil((double) byteArray.length / (double) sizeLimit); final ByteBuffer[] bufs = new ByteBuffer[numBufs]; int byteIndex = 0; for (int i = 0; i < numBufs; i++) { final int numBytes; final int remaining = byteArray.length - byteIndex; if (remaining < sizeLimit) { numBytes = remaining; } else { numBytes = sizeLimit; } bufs[i] = ByteBuffer.wrap(byteArray, byteIndex, numBytes); byteIndex += sizeLimit; } return bufs; } }