Java ByteBuffer Write writeToChannel(WritableByteChannel dst, ByteBuffer src)

Here you can find the source of writeToChannel(WritableByteChannel dst, ByteBuffer src)

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
dst The java.nio.channels.WritableByteChannel to write to
src The java.nio.ByteBuffer to read from

Exception

Parameter Description
IOException If some other I/O error occurs

Return

The number of bytes read, possibly zero

Declaration

public static int writeToChannel(WritableByteChannel dst, ByteBuffer src) throws IOException 

Method Source Code


//package com.java2s;
/*//from   www .j  av  a 2 s  . com
 * 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.WritableByteChannel;

public class Main {
    /**
     * Reads available data from the stream until the stream is empty or the {@link java.nio.ByteBuffer} is full
     *
     * @param dst The {@link java.nio.channels.WritableByteChannel} to write to
     * @param src The {@link java.nio.ByteBuffer} to read from
     *
     * @return The number of bytes read, possibly zero
     *
     * @throws java.nio.channels.NonWritableChannelException If this channel was not opened for writing
     * @throws java.nio.channels.ClosedChannelException      If this channel is closed
     * @throws java.nio.channels.AsynchronousCloseException  If another thread closes this channel
     *                                                       while the write operation is in progress
     * @throws java.nio.channels.ClosedByInterruptException  If another thread interrupts the current thread
     *                                                       while the write 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 writeToChannel(WritableByteChannel dst, ByteBuffer src) throws IOException {
        int totalBytesWritten = 0;

        int bytesWritten;
        do {
            bytesWritten = dst.write(src);
            totalBytesWritten += bytesWritten;
        } while (bytesWritten > 0);

        return totalBytesWritten;
    }
}

Related

  1. writeStringData(ByteBuffer buff, String s, int len)
  2. writeStringToBuffer(ByteBuffer buffer, String message)
  3. writeTo(ByteBuffer buffer, File file)
  4. writeToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer)
  5. writeToChannel(WritableByteChannel chan, ByteBuffer buffer)
  6. writeToFile(String name, ByteBuffer bb)
  7. writeTs(ByteBuffer is, long ts)
  8. writetUnsignedInt(ByteBuffer buffer, long value)
  9. writeUB2(ByteBuffer buffer, int i)