Here you can find the source of readBytes(ByteBuffer buffer, int position, int length)
public static byte[] readBytes(ByteBuffer buffer, int position, int length)
//package com.java2s; /**/*from w ww.ja va2s . c o m*/ * Copyright (c) 2012, 2014 Sme.UP and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * * Contributors: * Mattia Rocchi - Initial API and implementation */ import java.nio.ByteBuffer; public class Main { public static byte[] readBytes(ByteBuffer buffer, int position, int length) { assert buffer != null; prepare(buffer, position, length); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); return bytes; } public static void prepare(ByteBuffer buffer, int position, int length) { assert buffer != null; if (position > 0) { // overflow if (position + length > buffer.capacity()) buffer.limit(buffer.capacity()); else buffer.limit(position + length); buffer.position(position); } else { buffer.position(0); if (length > buffer.capacity()) buffer.limit(buffer.capacity()); else buffer.limit(length); } } }