Here you can find the source of getBytes(ByteBuffer buf)
public static byte[] getBytes(ByteBuffer buf)
//package com.java2s; /**//w w w . j a va 2 s. c o m Copyright (C) SYSTAP, LLC 2006-2007. All rights reserved. Contact: SYSTAP, LLC 4501 Tower Road Greensboro, NC 27410 licenses@bigdata.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.nio.ByteBuffer; public class Main { /** * Return the data in the buffer. When possible, the backing array is * returned. Otherwise, a new byte[] is allocated, the data are copied into * the array, and the new array is returned. */ public static byte[] getBytes(ByteBuffer buf) { if (buf.hasArray() && buf.arrayOffset() == 0 && buf.position() == 0 && buf.limit() == buf.capacity()) { /* * Return the backing array. */ return buf.array(); } /* * Copy the expected data into a byte[] using a read-only view on the * buffer so that we do not mess with its position, mark, or limit. */ final byte[] a; { buf = buf.asReadOnlyBuffer(); final int len = buf.remaining(); a = new byte[len]; buf.get(a); } return a; } }