Java ReadableByteChannel Read readToBuffer(ReadableByteChannel src, ByteBuffer dst)

Here you can find the source of readToBuffer(ReadableByteChannel src, ByteBuffer dst)

Description

Reads available data from the stream until the stream is empty or the java.nio.ByteBuffer is full

License

Open Source License

Parameter

Parameter Description
src The java.nio.channels.ReadableByteChannel to read from
dst The java.nio.ByteBuffer to write to

Exception

Parameter Description
IOException If some other I/O error occurs

Return

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Declaration

public static int readToBuffer(ReadableByteChannel src, ByteBuffer dst) throws IOException 

Method Source Code


//package com.java2s;
/*/* ww w  .j  a  v  a  2  s. c  om*/
 * SkryUtils - Free open source general purpose utilities
 * Copyright (C) 2014 Peter Skrypalle
 *
 * 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.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;

public class Main {
    /**
     * Reads available data from the stream until the stream is empty or the {@link java.nio.ByteBuffer} is full
     *
     * @param src The {@link java.nio.channels.ReadableByteChannel} to read from
     * @param dst The {@link java.nio.ByteBuffer} to write to
     *
     * @return The number of bytes read, possibly zero, or <tt>-1</tt> if the channel has reached end-of-stream
     *
     * @throws java.nio.channels.NonReadableChannelException If this channel was not opened for reading
     * @throws java.nio.channels.ClosedChannelException      If this channel is closed
     * @throws java.nio.channels.AsynchronousCloseException  If another thread closes this channel
     *                                                       while the read operation is in progress
     * @throws java.nio.channels.ClosedByInterruptException  If another thread interrupts the current thread
     *                                                       while the read operation is in progress, thereby
     *                                                       closing the channel and setting the current thread's
     *                                                       interrupt status
     * @throws IOException                                   If some other I/O error occurs
     */
    public static int readToBuffer(ReadableByteChannel src, ByteBuffer dst) throws IOException {
        int totalBytesRead = 0;

        int bytesRead;
        do {
            bytesRead = src.read(dst);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        return totalBytesRead;
    }
}

Related

  1. readFully(ReadableByteChannel channel, ByteBuffer b)
  2. readFully(ReadableByteChannel channel, ByteBuffer buffer)
  3. readLengthAndString(ReadableByteChannel channel, ByteBuffer buffer)
  4. readMap(ReadableByteChannel channel, ByteBuffer buffer)
  5. readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed)
  6. retryRead(ReadableByteChannel channel, ByteBuffer buffer)
  7. transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)
  8. transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink)
  9. writeEntryToTemp(File tempDir, ByteBuffer buffer, ReadableByteChannel channel)