SocketChannel.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SocketChannel.java --
  2. Copyright (C) 2002, 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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.io.IOException;
  33. import java.net.Socket;
  34. import java.net.SocketAddress;
  35. import java.nio.ByteBuffer;
  36. import java.nio.channels.spi.AbstractSelectableChannel;
  37. import java.nio.channels.spi.SelectorProvider;
  38. /**
  39. * @author Michael Koch (konqueror@gmx.de)
  40. * @since 1.4
  41. */
  42. public abstract class SocketChannel extends AbstractSelectableChannel
  43. implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
  44. {
  45. /**
  46. * Initializes this socket channel.
  47. */
  48. protected SocketChannel(SelectorProvider provider)
  49. {
  50. super(provider);
  51. }
  52. /**
  53. * Opens a socket channel.
  54. *
  55. * @return the new <code>SocketChannel</code> object
  56. *
  57. * @exception IOException If an error occurs
  58. */
  59. public static SocketChannel open() throws IOException
  60. {
  61. return SelectorProvider.provider().openSocketChannel();
  62. }
  63. /**
  64. * Opens a channel and connects it to a remote address.
  65. *
  66. * @return the new <code>SocketChannel</code> object
  67. *
  68. * @exception AsynchronousCloseException If this channel is already connected.
  69. * @exception ClosedByInterruptException If another thread interrupts the
  70. * current thread while the connect operation is in progress, thereby closing
  71. * the channel and setting the current thread's interrupt status.
  72. * @exception IOException If an error occurs
  73. * @exception SecurityException If a security manager has been installed and
  74. * it does not permit access to the given remote endpoint.
  75. * @exception UnresolvedAddressException If the given remote address is not
  76. * fully resolved.
  77. * @exception UnsupportedAddressTypeException If the type of the given remote
  78. * address is not supported.
  79. */
  80. public static SocketChannel open(SocketAddress remote)
  81. throws IOException
  82. {
  83. SocketChannel ch = open();
  84. ch.connect(remote);
  85. return ch;
  86. }
  87. /**
  88. * Reads data from the channel.
  89. *
  90. * @return the number of bytes read, zero is valid too, -1 if end of stream
  91. * is reached
  92. *
  93. * @exception IOException If an error occurs
  94. * @exception NotYetConnectedException If this channel is not yet connected.
  95. */
  96. public final long read(ByteBuffer[] dsts) throws IOException
  97. {
  98. long b = 0;
  99. for (int i = 0; i < dsts.length; i++)
  100. b += read(dsts[i]);
  101. return b;
  102. }
  103. /**
  104. * Writes data to the channel.
  105. *
  106. * @return the number of bytes written, zero is valid too
  107. *
  108. * @exception IOException If an error occurs
  109. * @exception NotYetConnectedException If this channel is not yet connected.
  110. */
  111. public final long write(ByteBuffer[] dsts) throws IOException
  112. {
  113. long b = 0;
  114. for (int i = 0; i < dsts.length; i++)
  115. b += write(dsts[i]);
  116. return b;
  117. }
  118. /**
  119. * Retrieves the valid operations for this channel.
  120. *
  121. * @return the valid operations
  122. */
  123. public final int validOps()
  124. {
  125. return SelectionKey.OP_CONNECT | SelectionKey.OP_READ
  126. | SelectionKey.OP_WRITE;
  127. }
  128. /**
  129. * Reads data from the channel.
  130. *
  131. * @return the number of bytes read, zero is valid too, -1 if end of stream
  132. * is reached
  133. *
  134. * @exception IOException If an error occurs
  135. * @exception NotYetConnectedException If this channel is not yet connected.
  136. */
  137. public abstract int read(ByteBuffer dst) throws IOException;
  138. /**
  139. * Connects the channel's socket to the remote address.
  140. *
  141. * @return <code>true</code> if the channel got successfully connected,
  142. * <code>false</code> if the channel is in non-blocking mode and connection
  143. * operation is still in progress.
  144. *
  145. * @exception AlreadyConnectedException If this channel is already connected.
  146. * @exception AsynchronousCloseException If this channel is already connected.
  147. * @exception ClosedByInterruptException If another thread interrupts the
  148. * current thread while the connect operation is in progress, thereby closing
  149. * the channel and setting the current thread's interrupt status.
  150. * @exception ClosedChannelException If this channel is closed.
  151. * @exception ConnectionPendingException If a non-blocking connection
  152. * operation is already in progress on this channel.
  153. * @exception IOException If an error occurs
  154. * @exception SecurityException If a security manager has been installed and
  155. * it does not permit access to the given remote endpoint.
  156. * @exception UnresolvedAddressException If the given remote address is not
  157. * fully resolved.
  158. * @exception UnsupportedAddressTypeException If the type of the given remote
  159. * address is not supported.
  160. */
  161. public abstract boolean connect(SocketAddress remote)
  162. throws IOException;
  163. /**
  164. * Finishes the process of connecting a socket channel.
  165. *
  166. * @exception AsynchronousCloseException If this channel is already connected.
  167. * @exception ClosedByInterruptException If another thread interrupts the
  168. * current thread while the connect operation is in progress, thereby closing
  169. * the channel and setting the current thread's interrupt status.
  170. * @exception ClosedChannelException If this channel is closed.
  171. * @exception IOException If an error occurs
  172. * @exception NoConnectionPendingException If this channel is not connected
  173. * and a connection operation has not been initiated.
  174. */
  175. public abstract boolean finishConnect() throws IOException;
  176. /**
  177. * Tells whether or not the channel's socket is connected.
  178. */
  179. public abstract boolean isConnected();
  180. /**
  181. * Tells whether or not a connection operation is in progress on this channel.
  182. */
  183. public abstract boolean isConnectionPending();
  184. /**
  185. * Reads data from the channel.
  186. *
  187. * @return the number of bytes read, zero is valid too, -1 if end of stream
  188. * is reached
  189. *
  190. * @exception IOException If an error occurs
  191. * @exception NotYetConnectedException If this channel is not yet connected.
  192. */
  193. public abstract long read(ByteBuffer[] dsts, int offset, int length)
  194. throws IOException;
  195. /**
  196. * Retrieves the channel's socket.
  197. *
  198. * @return the socket
  199. */
  200. public abstract Socket socket();
  201. /**
  202. * Writes data to the channel.
  203. *
  204. * @return the number of bytes written, zero is valid too
  205. *
  206. * @exception IOException If an error occurs
  207. * @exception NotYetConnectedException If this channel is not yet connected.
  208. */
  209. public abstract int write(ByteBuffer src) throws IOException;
  210. /**
  211. * Writes data to the channel.
  212. *
  213. * @return the number of bytes written, zero is valid too
  214. *
  215. * @exception IOException If an error occurs
  216. * @exception NotYetConnectedException If this channel is not yet connected.
  217. */
  218. public abstract long write(ByteBuffer[] srcs, int offset, int length)
  219. throws IOException;
  220. }