Java ByteBuffer Concatenate concat(ByteBuffer[] buffers, int overAllCapacity)

Here you can find the source of concat(ByteBuffer[] buffers, int overAllCapacity)

Description

Merge all byte buffers together

License

Apache License

Parameter

Parameter Description
buffers the bytebuffers to merge
overAllCapacity the capacity of the merged bytebuffer

Return

the merged byte buffer

Declaration

public static ByteBuffer concat(ByteBuffer[] buffers, int overAllCapacity) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

public class Main {
    /**//www .j  a v a2s .co m
     * Merge all byte buffers together
     * @param buffers the bytebuffers to merge
     * @param overAllCapacity the capacity of the
     *                        merged bytebuffer
     * @return the merged byte buffer
     *
     */
    public static ByteBuffer concat(ByteBuffer[] buffers, int overAllCapacity) {
        ByteBuffer all = ByteBuffer.allocateDirect(overAllCapacity);
        for (int i = 0; i < buffers.length; i++) {
            ByteBuffer curr = buffers[i].slice();
            all.put(curr);
        }

        all.rewind();
        return all;
    }

    /**
     * Merge all bytebuffers together
     * @param buffers the bytebuffers to merge
     * @return the merged bytebuffer
     */
    public static ByteBuffer concat(ByteBuffer[] buffers) {
        int overAllCapacity = 0;
        for (int i = 0; i < buffers.length; i++)
            overAllCapacity += buffers[i].limit() - buffers[i].position();
        //padding
        overAllCapacity += buffers[0].limit() - buffers[0].position();
        ByteBuffer all = ByteBuffer.allocateDirect(overAllCapacity);
        for (int i = 0; i < buffers.length; i++) {
            ByteBuffer curr = buffers[i];
            all.put(curr);
        }

        all.flip();
        return all;
    }
}

Related

  1. concat(byte[] pre, ByteBuffer bbuf)
  2. concat(ByteBuffer b1, ByteBuffer b2)
  3. concat(ByteBuffer outPart1, ByteBuffer outPart2)
  4. concat(ByteBuffer[] buf1, ByteBuffer[] buf2)
  5. concat(ByteBuffer[] buffers, ByteBuffer buffer)
  6. concat(List bbs)
  7. concatenateBytebuffers(ArrayList buffers)