Java ReadableByteChannel Read readAll(ReadableByteChannel ch, ByteBuffer dst)

Here you can find the source of readAll(ReadableByteChannel ch, ByteBuffer dst)

Description

Read channel until dst has remaining or eof.

License

Open Source License

Parameter

Parameter Description
ch a parameter
dst a parameter

Exception

Parameter Description
IOException an exception

Return

Returns number of bytes or -1 if no bytes were read and eof reached.

Declaration

public static final int readAll(ReadableByteChannel ch, ByteBuffer dst) throws IOException 

Method Source Code


//package com.java2s;
/*// w ww . j  av  a 2s  .c o  m
 * Copyright (C) 2015 Timo Vesalainen <timo.vesalainen@iki.fi>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.EOFException;
import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.ReadableByteChannel;

import java.nio.charset.Charset;

public class Main {
    /**
     * Read channel until dst has remaining or eof.
     * @param ch
     * @param dst
     * @return Returns number of bytes or -1 if no bytes were read and eof reached.
     * @throws IOException 
     */
    public static final int readAll(ReadableByteChannel ch, ByteBuffer dst) throws IOException {
        int count = 0;
        while (dst.hasRemaining()) {
            int rc = ch.read(dst);
            if (rc == -1) {
                if (count > 0) {
                    return count;
                }
                return -1;
            }
            count += rc;
        }
        return count;
    }

    /**
     * Read length bytes from channel and constructs a String using given charset.
     * Throws EOFException if couldn't read length bytes because of eof.
     * @param ch
     * @param length
     * @param charset
     * @return
     * @throws IOException 
     */
    public static final String read(ReadableByteChannel ch, int length, Charset charset) throws IOException {
        return new String(read(ch, length), charset);
    }

    /**
     * Read length bytes from channel. 
     * Throws EOFException if couldn't read length bytes because of eof.
     * @param ch
     * @param length
     * @return
     * @throws IOException 
     */
    public static final byte[] read(ReadableByteChannel ch, int length) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(length);
        readAll(ch, bb);
        if (bb.hasRemaining()) {
            throw new EOFException("couldn't read " + length + " bytes");
        }
        return bb.array();
    }

    /**
     * ScatteringChannel support
     * @param channel
     * @param dsts
     * @param offset
     * @param length
     * @return
     * @throws IOException 
     */
    public static final long read(ReadableByteChannel channel, ByteBuffer[] dsts, int offset, int length)
            throws IOException {
        long res = 0;
        for (int ii = 0; ii < length; ii++) {
            ByteBuffer bb = dsts[ii + offset];
            if (bb.hasRemaining()) {
                int rc = channel.read(bb);
                if (rc == -1) {
                    if (res == 0) {
                        return -1;
                    } else {
                        return res;
                    }
                }
                res += rc;
                if (bb.hasRemaining()) {
                    break;
                }
            }
        }
        return res;
    }

    /**
     * ScatteringChannel support
     * @param channel
     * @param dsts
     * @return
     * @throws IOException 
     */
    public static final long read(ReadableByteChannel channel, ByteBuffer[] dsts) throws IOException {
        return read(channel, dsts, 0, dsts.length);
    }
}

Related

  1. read(ReadableByteChannel channel, ByteBuffer buffer)
  2. read(ReadableByteChannel channel, ByteBuffer buffer)
  3. read(ReadableByteChannel channel, ByteBuffer[] dsts)
  4. read(ReadableByteChannel channel, ByteBuffer[] dsts, int offset, int length)
  5. read(ReadableByteChannel channel, int amount, ByteBuffer dest)
  6. readAllBytesOrNone(ReadableByteChannel channel, ByteBuffer buffer, int numBytes)
  7. readAnerisHeader(ReadableByteChannel from, ByteBuffer buffer)
  8. readBuffer(ReadableByteChannel chan, ByteBuffer buf)
  9. readCharArray(ReadableByteChannel channel, ByteBuffer buffer, char[] charArray)