Java ByteBuffer Get getArray(ByteBuffer buffer)

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

Description

You should almost never use this.

License

Apache License

Declaration

public static byte[] getArray(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.ByteBuffer;

import java.util.Arrays;

public class Main {
    /**//from   w w w .ja  va  2  s.c  o  m
    * You should almost never use this.  Instead, use the write* methods to avoid copies.
    */
    public static byte[] getArray(ByteBuffer buffer) {
        int length = buffer.remaining();

        if (buffer.hasArray()) {
            int boff = buffer.arrayOffset() + buffer.position();
            if (boff == 0 && length == buffer.array().length)
                return buffer.array();
            else
                return Arrays.copyOfRange(buffer.array(), boff, boff + length);
        }
        // else, DirectByteBuffer.get() is the fastest route
        byte[] bytes = new byte[length];
        buffer.duplicate().get(bytes);

        return bytes;
    }
}

Related

  1. getActiveArray(ByteBuffer buffer)
  2. getAddress(ByteBuffer buf)
  3. getAddress(ByteBuffer buffer)
  4. getAddress(ByteBuffer buffer)
  5. getAddressFromDirectByteBuffer(ByteBuffer buffer)
  6. getAsByteBuffer()
  7. getAsBytes(List buffers)
  8. getAscii(ByteBuffer bytes)
  9. getAsciiString(ByteBuffer buffer)