Java ByteBuffer Copy copyByteBuffer(final ByteBuffer pBuffer)

Here you can find the source of copyByteBuffer(final ByteBuffer pBuffer)

Description

Clone a byte buffer.

License

Open Source License

Parameter

Parameter Description
pBuffer The buffer.

Return

The new object.

Declaration

public static final ByteBuffer copyByteBuffer(final ByteBuffer pBuffer) 

Method Source Code

//package com.java2s;
/**/*w  ww .  j  a  va2 s  .  com*/
 * (C) Copyright 2007, Deft Labs.
 *
 * 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 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;

public class Main {
    /**
     * Clone a byte buffer. This doesn't work on direct buffers. The mark isn't
     * passed to the new buffer. The size of the new buffer is based on the
     * limit and not the capacity.
     * @param pBuffer The buffer.
     * @return The new object.
     */
    public static final ByteBuffer copyByteBuffer(final ByteBuffer pBuffer) {
        if (pBuffer.isDirect()) {
            throw new IllegalArgumentException("direct buffers not supported");
        }

        final ByteBuffer buffer = ByteBuffer.allocate(pBuffer.limit());
        buffer.order(pBuffer.order());
        buffer.put(pBuffer.array());
        buffer.position(pBuffer.position());
        return buffer;
    }
}

Related

  1. copyBuffer(ByteBuffer dest, int pos1, ByteBuffer src, int len)
  2. copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)
  3. copyByteBuffer(ByteBuffer buf)
  4. copyByteBuffer(ByteBuffer src)
  5. copyByteBuffer(ByteBuffer srcBuf, int srcStep, ByteBuffer dstBuf, int dstStep)
  6. copyByteBuffer(java.nio.ByteBuffer data)
  7. copyBytes(ByteBuffer bytes)
  8. copyBytes(ByteBuffer src, ByteBuffer dst, int length)
  9. copyBytesFrom(ByteBuffer bb)