Here you can find the source of split(ByteBuffer src, int unitSize)
public static ByteBuffer[] split(ByteBuffer src, int unitSize)
//package com.java2s; //License from project: Apache License import java.nio.ByteBuffer; public class Main { public static ByteBuffer[] split(ByteBuffer src, int unitSize) { int limit = src.limit(); if (unitSize >= limit) { return null;//new ByteBuffer[] { src }; }/*from w w w .j a v a 2s . c om*/ // return null; int size = (int) (Math.ceil((double) src.limit() / (double) unitSize)); ByteBuffer[] ret = new ByteBuffer[size]; int srcIndex = 0; for (int i = 0; i < size; i++) { int bufferSize = unitSize; if (i == size - 1) { bufferSize = src.limit() % unitSize; } byte[] dest = new byte[bufferSize]; System.arraycopy(src.array(), srcIndex, dest, 0, dest.length); srcIndex = srcIndex + bufferSize; ret[i] = ByteBuffer.wrap(dest); ret[i].position(0); ret[i].limit(ret[i].capacity()); } return ret; } }