Here you can find the source of writeToChannel(WritableByteChannel dst, ByteBuffer src)
Parameter | Description |
---|---|
dst | The java.nio.channels.WritableByteChannel to write to |
src | The java.nio.ByteBuffer to read from |
Parameter | Description |
---|---|
IOException | If some other I/O error occurs |
public static int writeToChannel(WritableByteChannel dst, ByteBuffer src) throws IOException
//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; } }