Java ByteBuffer to Byte Array toArray(ByteBuffer buffer)

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

Description

Get the byte array out of a ByteBuffer.

License

Open Source License

Parameter

Parameter Description
buffer the buffer to get the array from

Return

the byte buffer array

Declaration


public static byte[] toArray(ByteBuffer buffer) 

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 ava2 s  . c  om
     * Get the byte array out of a ByteBuffer.
     *
     * @param buffer the buffer to get the array from
     * @return the byte buffer array
     */

    public static byte[] toArray(ByteBuffer buffer) {
        if (buffer.hasArray()) {
            byte[] array = buffer.array();
            int from = buffer.arrayOffset() + buffer.position();
            return Arrays.copyOfRange(array, from, from + buffer.remaining());
        } else {
            byte[] to = new byte[buffer.remaining()];
            buffer.slice().get(to);
            return to;
        }
    }
}

Related

  1. readBytesNoLength(ByteBuffer logBuf, int size)
  2. readBytesWithShortLength(ByteBuffer bb)
  3. toArray(ByteBuffer buffer)
  4. toArray(ByteBuffer buffer)
  5. toArray(ByteBuffer buffer)
  6. toArray(ByteBuffer buffer)
  7. toArray(ByteBuffer buffer)
  8. toArray(ByteBuffer buffer)
  9. toArray(ByteBuffer bytebuffer)