SocketChannel.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* SocketChannel.java --
  2. Copyright (C) 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio.channels;
  32. import java.nio.channels.spi.AbstractSelectableChannel;
  33. import java.nio.channels.spi.SelectorProvider;
  34. import java.nio.ByteBuffer;
  35. import java.io.IOException;
  36. import java.net.Socket;
  37. import java.net.SocketAddress;
  38. /**
  39. * @author Michael Koch
  40. * @since 1.4
  41. */
  42. abstract public class SocketChannel extends AbstractSelectableChannel
  43. implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
  44. {
  45. /**
  46. * Initializes this socket.
  47. */
  48. protected SocketChannel (SelectorProvider provider)
  49. {
  50. super (provider);
  51. }
  52. /**
  53. * Opens a socket channel.
  54. *
  55. * @exception IOException If an error occurs
  56. */
  57. public static SocketChannel open () throws IOException
  58. {
  59. return SelectorProvider.provider ().openSocketChannel ();
  60. }
  61. /**
  62. * Opens a channel and connects it to a remote address.
  63. *
  64. * @exception AsynchronousCloseException If this channel is already connected.
  65. * @exception ClosedByInterruptException If another thread interrupts the
  66. * current thread while the connect operation is in progress, thereby closing
  67. * the channel and setting the current thread's interrupt status.
  68. * @exception IOException If an error occurs
  69. * @exception SecurityException If a security manager has been installed and
  70. * it does not permit access to the given remote endpoint.
  71. * @exception UnresolvedAddressException If the given remote address is not
  72. * fully resolved.
  73. * @exception UnsupportedAddressTypeException If the type of the given remote
  74. * address is not supported.
  75. */
  76. public static SocketChannel open (SocketAddress remote) throws IOException
  77. {
  78. SocketChannel ch = open ();
  79. if (ch.connect (remote))
  80. {
  81. }
  82. return ch;
  83. }
  84. /**
  85. * Reads data from the channel.
  86. *
  87. * @exception IOException If an error occurs
  88. * @exception NotYetConnectedException If this channel is not yet connected.
  89. */
  90. public final long read (ByteBuffer[] dsts) throws IOException
  91. {
  92. long b = 0;
  93. for (int i = 0; i < dsts.length; i++)
  94. {
  95. b += read (dsts [i]);
  96. }
  97. return b;
  98. }
  99. /**
  100. * Writes data to the channel.
  101. *
  102. * @exception IOException If an error occurs
  103. * @exception NotYetConnectedException If this channel is not yet connected.
  104. */
  105. public final long write (ByteBuffer[] dsts) throws IOException
  106. {
  107. long b = 0;
  108. for (int i= 0; i < dsts.length; i++)
  109. {
  110. b += write (dsts [i]);
  111. }
  112. return b;
  113. }
  114. /**
  115. * Retrieves the valid operations for this channel.
  116. */
  117. public final int validOps ()
  118. {
  119. return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE;
  120. }
  121. /**
  122. * Reads data from the channel.
  123. *
  124. * @exception IOException If an error occurs
  125. * @exception NotYetConnectedException If this channel is not yet connected.
  126. */
  127. public abstract int read (ByteBuffer dst) throws IOException;
  128. /**
  129. * Connects the channel's socket to the remote address.
  130. *
  131. * @exception AlreadyConnectedException If this channel is already connected.
  132. * @exception AsynchronousCloseException If this channel is already connected.
  133. * @exception ClosedByInterruptException If another thread interrupts the
  134. * current thread while the connect operation is in progress, thereby closing
  135. * the channel and setting the current thread's interrupt status.
  136. * @exception ClosedChannelException If this channel is closed.
  137. * @exception ConnectionPendingException If a non-blocking connection
  138. * operation is already in progress on this channel.
  139. * @exception IOException If an error occurs
  140. * @exception SecurityException If a security manager has been installed and
  141. * it does not permit access to the given remote endpoint.
  142. * @exception UnresolvedAddressException If the given remote address is not
  143. * fully resolved.
  144. * @exception UnsupportedAddressTypeException If the type of the given remote
  145. * address is not supported.
  146. */
  147. public abstract boolean connect (SocketAddress remote) throws IOException;
  148. /**
  149. * Finishes the process of connecting a socket channel.
  150. *
  151. * @exception AsynchronousCloseException If this channel is already connected.
  152. * @exception ClosedByInterruptException If another thread interrupts the
  153. * current thread while the connect operation is in progress, thereby closing
  154. * the channel and setting the current thread's interrupt status.
  155. * @exception ClosedChannelException If this channel is closed.
  156. * @exception IOException If an error occurs
  157. * @exception NoConnectionPendingException If this channel is not connected
  158. * and a connection operation has not been initiated.
  159. */
  160. public abstract boolean finishConnect () throws IOException;
  161. /**
  162. * Tells whether or not the channel's socket is connected.
  163. */
  164. public abstract boolean isConnected ();
  165. /**
  166. * Tells whether or not a connection operation is in progress on this channel.
  167. */
  168. public abstract boolean isConnectionPending ();
  169. /**
  170. * Reads data from the channel.
  171. *
  172. * @exception IOException If an error occurs
  173. * @exception NotYetConnectedException If this channel is not yet connected.
  174. */
  175. public abstract long read (ByteBuffer[] dsts, int offset, int length)
  176. throws IOException;
  177. /**
  178. * Retrieves the channel's socket.
  179. */
  180. public abstract Socket socket ();
  181. /**
  182. * Writes data to the channel.
  183. *
  184. * @exception IOException If an error occurs
  185. * @exception NotYetConnectedException If this channel is not yet connected.
  186. */
  187. public abstract int write (ByteBuffer src) throws IOException;
  188. /**
  189. * Writes data to the channel.
  190. *
  191. * @exception IOException If an error occurs
  192. * @exception NotYetConnectedException If this channel is not yet connected.
  193. */
  194. public abstract long write (ByteBuffer[] srcs, int offset, int length)
  195. throws IOException;
  196. }