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) { if (buffer.hasArray()) return buffer.array(); byte[] result = new byte[buffer.capacity()]; buffer.rewind();//from w w w. j a va 2s. c om buffer.get(result); return result; } public static <T> T get(T[] a, int idx, T defaultValue) { return idx > 0 && idx < a.length ? a[idx] : defaultValue; } }