1. /*
  2. * @(#)WritableByteChannel.java 1.14 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels;
  8. import java.io.IOException;
  9. import java.nio.ByteBuffer;
  10. /**
  11. * A channel that can write bytes.
  12. *
  13. * <p> Only one write operation upon a writable channel may be in progress at
  14. * any given time. If one thread initiates a write operation upon a channel
  15. * then any other thread that attempts to initiate another write operation will
  16. * block until the first operation is complete. Whether or not other kinds of
  17. * I/O operations may proceed concurrently with a write operation depends upon
  18. * the type of the channel. </p>
  19. *
  20. *
  21. * @author Mark Reinhold
  22. * @author JSR-51 Expert Group
  23. * @version 1.14, 03/01/23
  24. * @since 1.4
  25. */
  26. public interface WritableByteChannel
  27. extends Channel
  28. {
  29. /**
  30. * Writes a sequence of bytes to this channel from the given buffer.
  31. *
  32. * <p> An attempt is made to write up to <i>r</i> bytes to the channel,
  33. * where <i>r</i> is the number of bytes remaining in the buffer, that is,
  34. * <tt>dst.remaining()</tt>, at the moment this method is invoked.
  35. *
  36. * <p> Suppose that a byte sequence of length <i>n</i> is written, where
  37. * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>.
  38. * This byte sequence will be transferred from the buffer starting at index
  39. * <i>p</i>, where <i>p</i> is the buffer's position at the moment this
  40. * method is invoked; the index of the last byte written will be
  41. * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>.
  42. * Upon return the buffer's position will be equal to
  43. * <i>p</i> <tt>+</tt> <i>n</i> its limit will not have changed.
  44. *
  45. * <p> Unless otherwise specified, a write operation will return only after
  46. * writing all of the <i>r</i> requested bytes. Some types of channels,
  47. * depending upon their state, may write only some of the bytes or possibly
  48. * none at all. A socket channel in non-blocking mode, for example, cannot
  49. * write any more bytes than are free in the socket's output buffer.
  50. *
  51. * <p> This method may be invoked at any time. If another thread has
  52. * already initiated a write operation upon this channel, however, then an
  53. * invocation of this method will block until the first operation is
  54. * complete. </p>
  55. *
  56. * @param src
  57. * The buffer from which bytes are to be retrieved
  58. *
  59. * @return The number of bytes written, possibly zero
  60. *
  61. * @throws NonWritableChannelException
  62. * If this channel was not opened for writing
  63. *
  64. * @throws ClosedChannelException
  65. * If this channel is closed
  66. *
  67. * @throws AsynchronousCloseException
  68. * If another thread closes this channel
  69. * while the write operation is in progress
  70. *
  71. * @throws ClosedByInterruptException
  72. * If another thread interrupts the current thread
  73. * while the write operation is in progress, thereby
  74. * closing the channel and setting the current thread's
  75. * interrupt status
  76. *
  77. * @throws IOException
  78. * If some other I/O error occurs
  79. */
  80. public int write(ByteBuffer src) throws IOException;
  81. }