Java ByteBuffer to Byte Array getBytes(ByteBuffer buf, int length)

Here you can find the source of getBytes(ByteBuffer buf, int length)

Description

This is a relative bulk get method for ByteBuffer that returns the array of bytes gotten.

License

Open Source License

Parameter

Parameter Description
buf The ByteBuffer to get bytes from.
length The number of bytes to be copied.

Exception

Parameter Description
BufferUnderflowException if there are fewer than<code>length</code> bytes remaining in the buffer.

Return

Returns an array of the bytes gotten.

Declaration

public static byte[] getBytes(ByteBuffer buf, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    /**/*www. j  a v a 2  s .c om*/
     * This is a relative bulk get method for {@link ByteBuffer} that returns
     * the array of bytes gotten.  It is a convenience method so the array
     * doesn't have to be declared beforehand.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param length The number of bytes to be copied.
     * @return Returns an array of the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes remaining in the buffer.
     */
    public static byte[] getBytes(ByteBuffer buf, int length) {
        final byte[] dst = new byte[length];
        buf.get(dst, 0, length);
        return dst;
    }

    /**
     * This is an absolute bulk get method for {@link ByteBuffer} because it
     * doesn't have one (but should!).  Being absolute, the
     * {@link ByteBuffer}'s position doesn't change.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param offset The absolute offset within the {@link ByteBuffer} of the
     * first byte to be read; must be non-negative.
     * @param length The number of bytes to be copied.
     * @return Returns an array of the bytes gotten.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes in the buffer starting at the given offset.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit.
     */
    public static byte[] getBytes(ByteBuffer buf, int offset, int length) {
        final byte[] dst = new byte[length];
        getBytes(buf, offset, dst, 0, length);
        return dst;
    }

    /**
     * This is an absolute bulk get method for {@link ByteBuffer} because it
     * doesn't have one (but should!).  Being absolute, the
     * {@link ByteBuffer}'s position doesn't change.
     *
     * @param buf The {@link ByteBuffer} to get bytes from.
     * @param bufOffset The absolute offset within the {@link ByteBuffer} of
     * the first byte to be read; must be non-negative.
     * @param dst The array into which bytes are to be written.
     * @param dstOffset The offset within the destination array to start
     * writing bytes.
     * @param length The number of bytes to be copied.
     * @throws BufferUnderflowException if there are fewer than
     * <code>length</code> bytes in the buffer starting at the given offset.
     * @throws IllegalArgumentException if the offset is either negative or
     * beyond the buffer's limit.
     */
    public static void getBytes(ByteBuffer buf, int bufOffset, byte[] dst, int dstOffset, int length) {
        final int origPos = buf.position();
        buf.position(bufOffset);
        buf.get(dst, dstOffset, length);
        buf.position(origPos);
    }
}

Related

  1. getByteArrayFromByteBuffer(ByteBuffer content)
  2. getBytes(ByteBuffer bb)
  3. getBytes(ByteBuffer buf)
  4. getBytes(ByteBuffer buf)
  5. getBytes(ByteBuffer buf, byte[] arr)
  6. getBytes(ByteBuffer buffer, int len)
  7. getBytes(ByteBuffer byteBuffer, int length)
  8. getBytes(ByteBuffer bytes)
  9. getBytes(ByteBuffer floatCalculator, float number)