Java ByteBuffer Copy duplicate(ByteBuffer buffer)

Here you can find the source of duplicate(ByteBuffer buffer)

Description

Duplicate a byte buffer for storing for future use.

License

Open Source License

Parameter

Parameter Description
buffer the buffer to duplicate

Return

the newly allocated buffer

Declaration

public static ByteBuffer duplicate(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/*/*from   w  ww. j av a  2s .co m*/
 * The MIT License
 *
 * Copyright (c) 2016, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Duplicate a byte buffer for storing for future use.
     *
     * @param buffer the buffer to duplicate
     * @return the newly allocated buffer
     */
    public static ByteBuffer duplicate(ByteBuffer buffer) {
        ByteBuffer newBuffer = ByteBuffer.allocate(buffer.remaining() * 2);
        newBuffer.put(buffer);
        newBuffer.flip();
        return newBuffer;
    }

    /**
     * Transfer bytes from src to dst. If the source has more bytes than the destination then only as many bytes
     * as the destination has capacity to take will be transferred.
     *
     * @param src the source.
     * @param dst the destination.
     */
    public static void put(ByteBuffer src, ByteBuffer dst) {
        if (src.remaining() > dst.remaining()) {
            int limit = src.limit();
            try {
                src.limit(src.position() + dst.remaining());
                dst.put(src);
            } finally {
                src.limit(limit);
            }
        } else {
            dst.put(src);
        }
    }
}

Related

  1. copyWholeFrame(ByteBuffer srcFrame, ByteBuffer destFrame)
  2. deepCopy(ByteBuffer orig)
  3. deepCopyByteBufferToByteArray(ByteBuffer byteBuffer)
  4. deepCopyVisible(ByteBuffer orig)
  5. duplicate(ByteBuffer bb)
  6. duplicate(ByteBuffer[] buffers)
  7. duplicate(ByteBuffer[] byteBuffers)
  8. memcpy(ByteBuffer dstBuffer, int dstByteOffset, ByteBuffer srcBuffer, int srcByteOffset, int length)