Java ByteBuffer Copy copyOf(ByteBuffer payload)

Here you can find the source of copyOf(ByteBuffer payload)

Description

Make a copy of a byte buffer.

License

Open Source License

Parameter

Parameter Description
payload the payload to copy

Return

a new byte array of the payload contents

Declaration

public static ByteBuffer copyOf(ByteBuffer payload) 

Method Source Code


//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Main {
    /**/* w  w  w .  j  av  a 2s  .com*/
     * Make a copy of a byte buffer.
     * <p>
     * This is important in some tests, as the underlying byte buffer contained in a Frame can be modified through
     * masking and make it difficult to compare the results in the fuzzer.
     *
     * @param payload the payload to copy
     * @return a new byte array of the payload contents
     */
    public static ByteBuffer copyOf(ByteBuffer payload) {
        if (payload == null)
            return null;

        ByteBuffer copy = ByteBuffer.allocate(payload.remaining());
        copy.put(payload.slice());
        copy.flip();
        return copy;
    }

    /**
     * Make a copy of a byte buffer.
     * <p>
     * This is important in some tests, as the underlying byte buffer contained in a Frame can be modified through
     * masking and make it difficult to compare the results in the fuzzer.
     *
     * @param payload the payload to copy
     * @return a new byte array of the payload contents
     */
    public static ByteBuffer copyOf(byte[] payload) {
        return ByteBuffer.wrap(Arrays.copyOf(payload, payload.length));
    }
}

Related

  1. copyContentsToArray(ByteBuffer src)
  2. copyFileByMappedByteBuffer(String srcFileName, String dstFileName)
  3. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  4. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  5. copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)
  6. copyRemaining(ByteBuffer src, ByteBuffer dst)
  7. copyTo(ByteBuffer original, ByteBuffer copy)
  8. copyToArray(@Nonnull ByteBuffer data)
  9. copyToHeapBuffer(ByteBuffer src, ByteBuffer dest)