Here you can find the source of toByteArray(ByteBuffer bb)
public static byte[] toByteArray(ByteBuffer bb)
//package com.java2s; /*/*from ww w . j ava 2 s . c o m*/ * Copyright (c) 2012-2014, Parallel Universe Software Co. All rights reserved. * * This program and the accompanying materials are dual-licensed under * either the terms of the Eclipse Public License v1.0 as published by * the Eclipse Foundation * * or (per the licensee's choosing) * * under the terms of the GNU Lesser General Public License version 3.0 * as published by the Free Software Foundation. */ import java.nio.ByteBuffer; public class Main { public static byte[] toByteArray(ByteBuffer bb) { if (bb.hasArray() && bb.arrayOffset() == 0 && bb.position() == 0) { return bb.array(); } else { byte[] arr = new byte[bb.remaining()]; int p = bb.position(); bb.get(arr); bb.position(p); return arr; } } }