Here you can find the source of toByteArray(ByteBuffer buffer)
public static byte[] toByteArray(ByteBuffer buffer)
//package com.java2s; import java.nio.ByteBuffer; public class Main { public static byte[] toByteArray(ByteBuffer buffer) { return toByteArray(buffer, 0, buffer.capacity()); }/*from w w w .j a va 2 s.c o m*/ public static byte[] toByteArray(ByteBuffer buffer, int off, int len) { if (buffer.hasArray()) if (off == 0 && len == buffer.capacity()) return buffer.array(); byte[] result = new byte[len]; buffer.clear(); buffer.position(off); buffer.get(result); return result; } }