Java ReadableByteChannel Read fill(ReadableByteChannel in, ByteBuffer buffer)

Here you can find the source of fill(ReadableByteChannel in, ByteBuffer buffer)

Description

Fills the provided buffer it with bytes from the provided channel.

License

Open Source License

Parameter

Parameter Description
in the channel to read from
buffer the buffer to read into

Exception

Parameter Description
IOException if an IO error occurs

Return

the provided buffer

Declaration

public static ByteBuffer fill(ReadableByteChannel in, ByteBuffer buffer) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import java.nio.channels.ReadableByteChannel;

public class Main {
    /**//from  www .java2 s.c  o  m
     * Creates a new buffer and fills it with bytes from the
     * provided channel. The amount of data to read is specified
     * in the arguments.
     *
     * @param in the channel to read from
     * @param size the number of bytes to read into a new buffer
     * @return a new buffer containing the bytes read
     * @throws IOException if an IO error occurs
     */
    public static ByteBuffer fill(ReadableByteChannel in, int size) throws IOException {
        return fill(in, ByteBuffer.allocate(size));
    }

    /**
     * Fills the provided buffer it with bytes from the
     * provided channel. The amount of data to read is
     * dependant on the available space in the provided
     * buffer.
     *
     * @param in the channel to read from
     * @param buffer the buffer to read into
     * @return the provided buffer
     * @throws IOException if an IO error occurs
     */
    public static ByteBuffer fill(ReadableByteChannel in, ByteBuffer buffer) throws IOException {
        while (buffer.hasRemaining())
            if (in.read(buffer) == -1)
                throw new BufferUnderflowException();
        buffer.flip();
        return buffer;
    }
}

Related

  1. copyChannels(ReadableByteChannel in, WritableByteChannel out)
  2. copyChannels(ReadableByteChannel input, WritableByteChannel output, int bufferSize)
  3. getNextVIntAsLong(ByteBuffer buffer, ReadableByteChannel readChannel)
  4. infiniteReadableByteChannelFor(ByteBuffer... buffers)
  5. read(ReadableByteChannel channel, ByteBuffer buffer)
  6. read(ReadableByteChannel channel, ByteBuffer buffer)