fill Buffer from ReadableByteChannel - Java java.nio.channels

Java examples for java.nio.channels:ReadableByteChannel

Description

fill Buffer from ReadableByteChannel

Demo Code


//package com.java2s;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

public class Main {
    public static boolean fillBuffer(ReadableByteChannel channel,
            ByteBuffer buf, boolean clear) throws IOException {
        if (clear)
            buf.clear();//from www  . j  a va  2s  . com

        while (true) {
            int cnt = channel.read(buf);
            if (cnt < 0)
                return false;
            if (buf.position() == buf.capacity())
                break;// fill to capacity
        }
        return true;
    }
}

Related Tutorials