Here you can find the source of toArray(ByteBuffer bytebuffer)
public static byte[] toArray(ByteBuffer bytebuffer)
//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()); } } }