Java ReadableByteChannel Read read(ReadableByteChannel channel, ByteBuffer[] dsts, int offset, int length)

Here you can find the source of read(ReadableByteChannel channel, ByteBuffer[] dsts, int offset, int length)

Description

ScatteringChannel support

License

Open Source License

Parameter

Parameter Description
channel a parameter
dsts a parameter
offset a parameter
length a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static final long read(ReadableByteChannel channel, ByteBuffer[] dsts, int offset, int length)
        throws IOException 

Method Source Code


//package com.java2s;
/*/* ww w  .  j  av  a 2 s  .c  om*/
 * 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 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);
    }

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

Related

  1. getNextVIntAsLong(ByteBuffer buffer, ReadableByteChannel readChannel)
  2. infiniteReadableByteChannelFor(ByteBuffer... buffers)
  3. read(ReadableByteChannel channel, ByteBuffer buffer)
  4. read(ReadableByteChannel channel, ByteBuffer buffer)
  5. read(ReadableByteChannel channel, ByteBuffer[] dsts)
  6. read(ReadableByteChannel channel, int amount, ByteBuffer dest)
  7. readAll(ReadableByteChannel ch, ByteBuffer dst)
  8. readAllBytesOrNone(ReadableByteChannel channel, ByteBuffer buffer, int numBytes)
  9. readAnerisHeader(ReadableByteChannel from, ByteBuffer buffer)