1. /*
  2. * @(#)ReadableByteChannel.java 1.15 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 read bytes.
  12. *
  13. * <p> Only one read operation upon a readable channel may be in progress at
  14. * any given time. If one thread initiates a read operation upon a channel
  15. * then any other thread that attempts to initiate another read 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 read 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.15, 03/01/23
  24. * @since 1.4
  25. */
  26. public interface ReadableByteChannel extends Channel {
  27. /**
  28. * Reads a sequence of bytes from this channel into the given buffer.
  29. *
  30. * <p> An attempt is made to read up to <i>r</i> bytes from the channel,
  31. * where <i>r</i> is the number of bytes remaining in the buffer, that is,
  32. * <tt>dst.remaining()</tt>, at the moment this method is invoked.
  33. *
  34. * <p> Suppose that a byte sequence of length <i>n</i> is read, where
  35. * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>.
  36. * This byte sequence will be transferred into the buffer so that the first
  37. * byte in the sequence is at index <i>p</i> and the last byte is at index
  38. * <i>p</i> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>,
  39. * where <i>p</i> is the buffer's position at the moment this method is
  40. * invoked. Upon return the buffer's position will be equal to
  41. * <i>p</i> <tt>+</tt> <i>n</i> its limit will not have changed.
  42. *
  43. * <p> A read operation might not fill the buffer, and in fact it might not
  44. * read any bytes at all. Whether or not it does so depends upon the
  45. * nature and state of the channel. A socket channel in non-blocking mode,
  46. * for example, cannot read any more bytes than are immediately available
  47. * from the socket's input buffer; similarly, a file channel cannot read
  48. * any more bytes than remain in the file. It is guaranteed, however, that
  49. * if a channel is in blocking mode and there is at least one byte
  50. * remaining in the buffer then this method will block until at least one
  51. * byte is read.
  52. *
  53. * <p> This method may be invoked at any time. If another thread has
  54. * already initiated a read operation upon this channel, however, then an
  55. * invocation of this method will block until the first operation is
  56. * complete. </p>
  57. *
  58. * @param dst
  59. * The buffer into which bytes are to be transferred
  60. *
  61. * @return The number of bytes read, possibly zero, or <tt>-1</tt> if the
  62. * channel has reached end-of-stream
  63. *
  64. * @throws NonReadableChannelException
  65. * If this channel was not opened for reading
  66. *
  67. * @throws ClosedChannelException
  68. * If this channel is closed
  69. *
  70. * @throws AsynchronousCloseException
  71. * If another thread closes this channel
  72. * while the read operation is in progress
  73. *
  74. * @throws ClosedByInterruptException
  75. * If another thread interrupts the current thread
  76. * while the read operation is in progress, thereby
  77. * closing the channel and setting the current thread's
  78. * interrupt status
  79. *
  80. * @throws IOException
  81. * If some other I/O error occurs
  82. */
  83. public int read(ByteBuffer dst) throws IOException;
  84. }