Java ByteBuffer to Byte Array toArray(ByteBuffer bytebuffer)

Here you can find the source of toArray(ByteBuffer bytebuffer)

Description

to Array

License

Apache License

Declaration

public static byte[] toArray(ByteBuffer bytebuffer) 

Method Source Code

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

import java.nio.ByteBuffer;

import java.util.Arrays;

public class Main {

    public static byte[] toArray(ByteBuffer bytebuffer) {
        if (false == bytebuffer.hasArray()) {
            int oldPosition = bytebuffer.position();
            bytebuffer.position(0);//  w  w  w .j a v a2s . co  m
            int size = bytebuffer.limit();
            byte[] buffers = new byte[size];
            bytebuffer.get(buffers);
            bytebuffer.position(oldPosition);
            return buffers;
        } else {
            return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
        }
    }
}

Related

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