Java ByteBuffer from Byte Array readBytes(ByteBuffer buffer)

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

Description

Read the given byte buffer into a byte array

License

Apache License

Declaration

public static byte[] readBytes(ByteBuffer buffer) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    /**/* w w  w  .ja  v  a 2 s  . com*/
     * Read the given byte buffer into a byte array
     */
    public static byte[] readBytes(ByteBuffer buffer) {
        return readBytes(buffer, 0, buffer.limit());
    }

    /**
     * Read a byte array from the given offset and size in the buffer
     */
    public static byte[] readBytes(ByteBuffer buffer, int offset, int size) {
        byte[] dest = new byte[size];
        if (buffer.hasArray()) {
            System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size);
        } else {
            buffer.mark();
            buffer.get(dest);
            buffer.reset();
        }
        return dest;
    }
}

Related

  1. readByteArray(ByteBuffer logBuf)
  2. readByteAsInt(ByteBuffer buffer)
  3. readBytes(ByteBuffer bb, int length)
  4. readBytes(ByteBuffer bb, int length)
  5. readBytes(ByteBuffer buf, int length)
  6. readBytes(ByteBuffer buffer, int length)
  7. readBytes(ByteBuffer buffer, int position, int length)
  8. readBytes(final ByteBuffer bb, final int length)
  9. toByteBuffer(byte[] array)