Java ByteBuffer Create newByteArrayFromByteBuffer(ByteBuffer buf)

Here you can find the source of newByteArrayFromByteBuffer(ByteBuffer buf)

Description

Creates a byte array from the given ByteBuffer, the position property of input ByteBuffer remains unchanged.

License

Apache License

Parameter

Parameter Description
buf source ByteBuffer

Return

a newly created byte array

Declaration

public static byte[] newByteArrayFromByteBuffer(ByteBuffer buf) 

Method Source Code

//package com.java2s;
/*/*from w  w w.j  a  v a 2  s  .com*/
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the ?License??). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Creates a byte array from the given ByteBuffer, the position property of input
     * {@link ByteBuffer} remains unchanged.
     *
     * @param buf source ByteBuffer
     * @return a newly created byte array
     */
    public static byte[] newByteArrayFromByteBuffer(ByteBuffer buf) {
        final int length = buf.remaining();
        byte[] bytes = new byte[length];
        // transfer bytes from this buffer into the given destination array
        buf.duplicate().get(bytes, 0, length);
        return bytes;
    }
}

Related

  1. createByteBufferOnHeap(int size)
  2. createCopy(ByteBuffer buffer)
  3. createDirectByteBuffer(byte[] data)
  4. createIconsFromBuffers(List byteBuffers)
  5. createInputStream(final ByteBuffer buf)
  6. newByteBuffer(int bufferSize, boolean direct)
  7. newByteBuffer(int len)
  8. newByteBuffer(int nbElements)
  9. newByteBuffer(int theSize)