Java ByteBuffer Concatenate concatenateBytebuffers(ArrayList buffers)

Here you can find the source of concatenateBytebuffers(ArrayList buffers)

Description

concatenate nio-ByteBuffers

License

Open Source License

Parameter

Parameter Description
buffers ArrayList<ByteBuffer>

Return

ByteBuffer

Declaration

public static ByteBuffer concatenateBytebuffers(ArrayList<ByteBuffer> buffers) 

Method Source Code

//package com.java2s;
/**/*w  w w  . j a  v a2 s .  c  om*/
 * Copyright (C) 2008 ITS of ETH Zurich, Switzerland, Sarah Windler Burri
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 * 
 * See the GNU Lesser General Public License for more details. You should have
 * received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

import java.util.ArrayList;

public class Main {
    /**
     * concatenate nio-ByteBuffers
     * 
     * @param buffers
     *          ArrayList<ByteBuffer>
     * @return ByteBuffer
     */
    public static ByteBuffer concatenateBytebuffers(ArrayList<ByteBuffer> buffers) {
        int n = 0;
        for (ByteBuffer b : buffers)
            n += b.remaining();

        ByteBuffer buf = (n > 0 && buffers.get(0).isDirect()) ? ByteBuffer.allocateDirect(n)
                : ByteBuffer.allocate(n);
        if (n > 0)
            buf.order(buffers.get(0).order());

        for (ByteBuffer b : buffers)
            buf.put(b.duplicate());

        buf.flip();
        return buf;
    }
}

Related

  1. concat(ByteBuffer outPart1, ByteBuffer outPart2)
  2. concat(ByteBuffer[] buf1, ByteBuffer[] buf2)
  3. concat(ByteBuffer[] buffers, ByteBuffer buffer)
  4. concat(ByteBuffer[] buffers, int overAllCapacity)
  5. concat(List bbs)