Java ByteBuffer Copy copy(ByteBuffer buffer)

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

Description

Copies the contents in the specified ByteBuffer , but not any of its attributes (e.g.

License

Open Source License

Parameter

Parameter Description
buffer The byte buffer.

Return

The copied byte buffer.

Declaration

public static ByteBuffer copy(ByteBuffer buffer) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    /**//from   w  w w .j ava2 s .c  om
     * Copies the contents in the specified {@link ByteBuffer}, but <strong>not</strong> any of its attributes (e.g.
     * mark, read-only). The capacity and limit of the new buffer will be the limit of the specified one, the position
     * of the new buffer will be 0, and the mark will be undefined. The specified buffer will be flipped after writing.
     * <p>
     * This method uses {@link ByteBuffer#put(ByteBuffer)} and so will write from the specified byte buffers current
     * position.
     *
     * @param buffer The byte buffer.
     * @return The copied byte buffer.
     */
    public static ByteBuffer copy(ByteBuffer buffer) {
        ByteBuffer copy = ByteBuffer.allocate(buffer.limit());
        copy.put(buffer).flip();
        buffer.flip();
        return copy;
    }
}

Related

  1. copy(ByteBuffer bb, boolean forceDirect)
  2. copy(ByteBuffer buf)
  3. copy(ByteBuffer buffer)
  4. copy(ByteBuffer buffer)
  5. copy(ByteBuffer from, ByteBuffer to)
  6. copy(ByteBuffer origin, int start, int end)