Here you can find the source of toArray(ByteBuffer buffer)
public static byte[] toArray(ByteBuffer buffer)
//package com.java2s; // See LICENSE.txt for license information import java.nio.ByteBuffer; public class Main { /** Returns the content of {@code buffer} as byte array. */ public static byte[] toArray(ByteBuffer buffer) { byte[] retVal = null; try {/*ww w.j a v a 2s . c o m*/ retVal = buffer.array(); } catch (Throwable t) { } if (retVal == null || retVal.length != buffer.limit()) { retVal = new byte[buffer.limit()]; int pos = buffer.position(); buffer.position(0); buffer.get(retVal); buffer.position(pos); } return retVal; } }