Here you can find the source of readNByte(ReadableByteChannel channel, int n)
public static byte[] readNByte(ReadableByteChannel channel, int n) throws IOException
//package com.java2s; /**/*from w w w. j a v a 2s . c o m*/ * This class is part of JCodec ( www.jcodec.org ) This software is distributed * under FreeBSD License * * @author The JCodec project * */ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; public class Main { public static byte[] readNByte(ReadableByteChannel channel, int n) throws IOException { byte[] result = new byte[n]; channel.read(ByteBuffer.wrap(result)); return result; } public static final ByteBuffer read(ByteBuffer buffer, int count) { ByteBuffer slice = buffer.duplicate(); int limit = buffer.position() + count; slice.limit(limit); buffer.position(limit); return slice; } public static ByteBuffer duplicate(ByteBuffer bb) { ByteBuffer out = ByteBuffer.allocate(bb.remaining()); out.put(bb.duplicate()); out.flip(); return out; } }