Path: blob/master/src/java.base/share/classes/java/nio/channels/WritableByteChannel.java
41159 views
/*1* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.io.IOException;28import java.nio.ByteBuffer;293031/**32* A channel that can write bytes.33*34* <p> Only one write operation upon a writable channel may be in progress at35* any given time. If one thread initiates a write operation upon a channel36* then any other thread that attempts to initiate another write operation will37* block until the first operation is complete. Whether or not other kinds of38* I/O operations may proceed concurrently with a write operation depends upon39* the type of the channel. </p>40*41*42* @author Mark Reinhold43* @author JSR-51 Expert Group44* @since 1.445*/4647public interface WritableByteChannel48extends Channel49{5051/**52* Writes a sequence of bytes to this channel from the given buffer.53*54* <p> An attempt is made to write up to <i>r</i> bytes to the channel,55* where <i>r</i> is the number of bytes remaining in the buffer, that is,56* {@code src.remaining()}, at the moment this method is invoked.57*58* <p> Suppose that a byte sequence of length <i>n</i> is written, where59* {@code 0} {@code <=} <i>n</i> {@code <=} <i>r</i>.60* This byte sequence will be transferred from the buffer starting at index61* <i>p</i>, where <i>p</i> is the buffer's position at the moment this62* method is invoked; the index of the last byte written will be63* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1}.64* Upon return the buffer's position will be equal to65* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.66*67* <p> Unless otherwise specified, a write operation will return only after68* writing all of the <i>r</i> requested bytes. Some types of channels,69* depending upon their state, may write only some of the bytes or possibly70* none at all. A socket channel in non-blocking mode, for example, cannot71* write any more bytes than are free in the socket's output buffer.72*73* <p> This method may be invoked at any time. If another thread has74* already initiated a write operation upon this channel, however, then an75* invocation of this method will block until the first operation is76* complete. </p>77*78* @param src79* The buffer from which bytes are to be retrieved80*81* @return The number of bytes written, possibly zero82*83* @throws NonWritableChannelException84* If this channel was not opened for writing85*86* @throws ClosedChannelException87* If this channel is closed88*89* @throws AsynchronousCloseException90* If another thread closes this channel91* while the write operation is in progress92*93* @throws ClosedByInterruptException94* If another thread interrupts the current thread95* while the write operation is in progress, thereby96* closing the channel and setting the current thread's97* interrupt status98*99* @throws IOException100* If some other I/O error occurs101*/102public int write(ByteBuffer src) throws IOException;103104}105106107