Java File Read via ByteBuffer readNByte(ReadableByteChannel channel, int n)

Here you can find the source of readNByte(ReadableByteChannel channel, int n)

Description

read N Byte

License

BSD License

Declaration

public static byte[] readNByte(ReadableByteChannel channel, int n) throws IOException 

Method Source Code

//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;
    }
}

Related

  1. readLong(InputStream input)
  2. readLong(InputStream is)
  3. readLong(InputStream is, ByteOrder bo)
  4. readLong(RandomAccessFile raFile)
  5. readLTriad(byte[] triad)
  6. readNIOFile(String filePath)
  7. readShort(byte[] bytes, int start)
  8. readShort(DataInput input)
  9. readShortBuffer(DataInputStream input)