Code example for ByteBuffer

Methods: positionremainingarrayarrayOffsethasArray

     * 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();
                return Arrays.copyOfRange(buffer.array(), boff, boff + length);
        // else, DirectByteBuffer.get() is the fastest route 
        byte[] bytes = new byte[length];
        return bytes;
Connect your IDE to all the code out there  Get Codota for Java