Java ByteBuffer to Byte Array toArray(final ByteBuffer buffer)

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

Description

Converts a byte buffer into a byte array.

License

Open Source License

Parameter

Parameter Description
buffer Byte buffer to convert.

Return

Byte array corresponding to bytes of buffer from current position to limit.

Declaration

public static byte[] toArray(final ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/* See LICENSE for licensing and NOTICE for copyright. */

import java.nio.ByteBuffer;

public class Main {
    /**/*from ww w  . ja  v a  2s.  co  m*/
     * Converts a byte buffer into a byte array.
     *
     * @param  buffer  Byte buffer to convert.
     *
     * @return  Byte array corresponding to bytes of buffer from current position
     *          to limit.
     */
    public static byte[] toArray(final ByteBuffer buffer) {
        if (buffer.limit() == buffer.capacity()) {
            return buffer.array();
        }

        final byte[] array = new byte[buffer.limit()];
        buffer.position(0);
        buffer.get(array);
        return array;
    }
}

Related

  1. toArray(ByteBuffer buffer)
  2. toArray(ByteBuffer buffer)
  3. toArray(ByteBuffer buffer)
  4. toArray(ByteBuffer bytebuffer)
  5. toArray(final ByteBuffer b)
  6. toByteArray(ByteBuffer bb)
  7. toByteArray(ByteBuffer buf)
  8. toByteArray(ByteBuffer buffer)
  9. toByteArray(ByteBuffer buffer)