J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.nio.channels; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.nio.ByteBuffer; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * A channel that can write bytes. |
| 34 | * |
| 35 | * <p> Only one write operation upon a writable channel may be in progress at |
| 36 | * any given time. If one thread initiates a write operation upon a channel |
| 37 | * then any other thread that attempts to initiate another write operation will |
| 38 | * block until the first operation is complete. Whether or not other kinds of |
| 39 | * I/O operations may proceed concurrently with a write operation depends upon |
| 40 | * the type of the channel. </p> |
| 41 | * |
| 42 | * |
| 43 | * @author Mark Reinhold |
| 44 | * @author JSR-51 Expert Group |
| 45 | * @since 1.4 |
| 46 | */ |
| 47 | |
| 48 | public interface WritableByteChannel |
| 49 | extends Channel |
| 50 | { |
| 51 | |
| 52 | /** |
| 53 | * Writes a sequence of bytes to this channel from the given buffer. |
| 54 | * |
| 55 | * <p> An attempt is made to write up to <i>r</i> bytes to the channel, |
| 56 | * where <i>r</i> is the number of bytes remaining in the buffer, that is, |
| 57 | * <tt>src.remaining()</tt>, at the moment this method is invoked. |
| 58 | * |
| 59 | * <p> Suppose that a byte sequence of length <i>n</i> is written, where |
| 60 | * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>. |
| 61 | * This byte sequence will be transferred from the buffer starting at index |
| 62 | * <i>p</i>, where <i>p</i> is the buffer's position at the moment this |
| 63 | * method is invoked; the index of the last byte written will be |
| 64 | * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>. |
| 65 | * Upon return the buffer's position will be equal to |
| 66 | * <i>p</i> <tt>+</tt> <i>n</i>; its limit will not have changed. |
| 67 | * |
| 68 | * <p> Unless otherwise specified, a write operation will return only after |
| 69 | * writing all of the <i>r</i> requested bytes. Some types of channels, |
| 70 | * depending upon their state, may write only some of the bytes or possibly |
| 71 | * none at all. A socket channel in non-blocking mode, for example, cannot |
| 72 | * write any more bytes than are free in the socket's output buffer. |
| 73 | * |
| 74 | * <p> This method may be invoked at any time. If another thread has |
| 75 | * already initiated a write operation upon this channel, however, then an |
| 76 | * invocation of this method will block until the first operation is |
| 77 | * complete. </p> |
| 78 | * |
| 79 | * @param src |
| 80 | * The buffer from which bytes are to be retrieved |
| 81 | * |
| 82 | * @return The number of bytes written, possibly zero |
| 83 | * |
| 84 | * @throws NonWritableChannelException |
| 85 | * If this channel was not opened for writing |
| 86 | * |
| 87 | * @throws ClosedChannelException |
| 88 | * If this channel is closed |
| 89 | * |
| 90 | * @throws AsynchronousCloseException |
| 91 | * If another thread closes this channel |
| 92 | * while the write operation is in progress |
| 93 | * |
| 94 | * @throws ClosedByInterruptException |
| 95 | * If another thread interrupts the current thread |
| 96 | * while the write operation is in progress, thereby |
| 97 | * closing the channel and setting the current thread's |
| 98 | * interrupt status |
| 99 | * |
| 100 | * @throws IOException |
| 101 | * If some other I/O error occurs |
| 102 | */ |
| 103 | public int write(ByteBuffer src) throws IOException; |
| 104 | |
| 105 | } |