Java ByteBuffer Split split(ByteBuffer src, int unitSize)

Here you can find the source of split(ByteBuffer src, int unitSize)

Description

split

License

Apache License

Declaration

public static ByteBuffer[] split(ByteBuffer src, int unitSize) 

Method Source Code

//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;
    }
}

Related

  1. split(ByteBuffer buffer, byte tag)
  2. split(ByteBuffer buffer, int position)
  3. splitLongToBuffers(ByteBuffer buffer, ByteBuffer buffer1, long iValue)
  4. splitShortToBuffers(ByteBuffer buffer, ByteBuffer buffer1, short iValue)