1. /*
  2. * @(#)DatagramChannel.java 1.32 03/12/19
  3. *
  4. * Copyright 2004 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.net.DatagramSocket;
  10. import java.net.SocketAddress;
  11. import java.nio.ByteBuffer;
  12. import java.nio.channels.spi.*;
  13. /**
  14. * A selectable channel for datagram-oriented sockets.
  15. *
  16. *
  17. * <p> Datagram channels are not a complete abstraction of network datagram
  18. * sockets. Binding and the manipulation of socket options must be done
  19. * through an associated {@link java.net.DatagramSocket} object obtained by
  20. * invoking the {@link #socket() socket} method. It is not possible to create
  21. * a channel for an arbitrary, pre-existing datagram socket, nor is it possible
  22. * to specify the {@link java.net.DatagramSocketImpl} object to be used by a
  23. * datagram socket associated with a datagram channel.
  24. *
  25. * <p> A datagram channel is created by invoking the {@link #open open} method
  26. * of this class. A newly-created datagram channel is open but not connected.
  27. * A datagram channel need not be connected in order for the {@link #send send}
  28. * and {@link #receive receive} methods to be used. A datagram channel may be
  29. * connected, by invoking its {@link #connect connect} method, in order to
  30. * avoid the overhead of the security checks are otherwise performed as part of
  31. * every send and receive operation. A datagram channel must be connected in
  32. * order to use the {@link #read(java.nio.ByteBuffer) read} and {@link
  33. * #write(java.nio.ByteBuffer) write} methods, since those methods do not
  34. * accept or return socket addresses.
  35. *
  36. * <p> Once connected, a datagram channel remains connected until it is
  37. * disconnected or closed. Whether or not a datagram channel is connected may
  38. * be determined by invoking its {@link #isConnected isConnected} method.
  39. *
  40. * <p> Datagram channels are safe for use by multiple concurrent threads. They
  41. * support concurrent reading and writing, though at most one thread may be
  42. * reading and at most one thread may be writing at any given time. </p>
  43. *
  44. *
  45. * @author Mark Reinhold
  46. * @author JSR-51 Expert Group
  47. * @version 1.32, 03/12/19
  48. * @since 1.4
  49. */
  50. public abstract class DatagramChannel
  51. extends AbstractSelectableChannel
  52. implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
  53. {
  54. /**
  55. * Initializes a new instance of this class.
  56. */
  57. protected DatagramChannel(SelectorProvider provider) {
  58. super(provider);
  59. }
  60. /**
  61. * Opens a datagram channel.
  62. *
  63. * <p> The new channel is created by invoking the {@link
  64. * java.nio.channels.spi.SelectorProvider#openDatagramChannel()
  65. * openDatagramChannel} method of the system-wide default {@link
  66. * java.nio.channels.spi.SelectorProvider} object. The channel will not be
  67. * connected. </p>
  68. *
  69. * @return A new datagram channel
  70. *
  71. * @throws IOException
  72. * If an I/O error occurs
  73. */
  74. public static DatagramChannel open() throws IOException {
  75. return SelectorProvider.provider().openDatagramChannel();
  76. }
  77. /**
  78. * Returns an operation set identifying this channel's supported
  79. * operations.
  80. *
  81. * <p> Datagram channels support reading and writing, so this method
  82. * returns <tt>(</tt>{@link SelectionKey#OP_READ} <tt>|</tt> {@link
  83. * SelectionKey#OP_WRITE}<tt>)</tt>. </p>
  84. *
  85. * @return The valid-operation set
  86. */
  87. public final int validOps() {
  88. return (SelectionKey.OP_READ
  89. | SelectionKey.OP_WRITE);
  90. }
  91. // -- Socket-specific operations --
  92. /**
  93. * Retrieves a datagram socket associated with this channel.
  94. *
  95. * <p> The returned object will not declare any public methods that are not
  96. * declared in the {@link java.net.DatagramSocket} class. </p>
  97. *
  98. * @return A datagram socket associated with this channel
  99. */
  100. public abstract DatagramSocket socket();
  101. /**
  102. * Tells whether or not this channel's socket is connected. </p>
  103. *
  104. * @return <tt>true</tt> if, and only if, this channel's socket
  105. * is connected
  106. */
  107. public abstract boolean isConnected();
  108. /**
  109. * Connects this channel's socket.
  110. *
  111. * <p> The channel's socket is configured so that it only receives
  112. * datagrams from, and sends datagrams to, the given remote <i>peer</i>
  113. * address. Once connected, datagrams may not be received from or sent to
  114. * any other address. A datagram socket remains connected until it is
  115. * explicitly disconnected or until it is closed.
  116. *
  117. * <p> This method performs exactly the same security checks as the {@link
  118. * java.net.DatagramSocket#connect connect} method of the {@link
  119. * java.net.DatagramSocket} class. That is, if a security manager has been
  120. * installed then this method verifies that its {@link
  121. * java.lang.SecurityManager#checkAccept checkAccept} and {@link
  122. * java.lang.SecurityManager#checkConnect checkConnect} methods permit
  123. * datagrams to be received from and sent to, respectively, the given
  124. * remote address.
  125. *
  126. * <p> This method may be invoked at any time. It will not have any effect
  127. * on read or write operations that are already in progress at the moment
  128. * that it is invoked. </p>
  129. *
  130. * @param remote
  131. * The remote address to which this channel is to be connected
  132. *
  133. * @return This datagram channel
  134. *
  135. * @throws ClosedChannelException
  136. * If this channel is closed
  137. *
  138. * @throws AsynchronousCloseException
  139. * If another thread closes this channel
  140. * while the connect operation is in progress
  141. *
  142. * @throws ClosedByInterruptException
  143. * If another thread interrupts the current thread
  144. * while the connect operation is in progress, thereby
  145. * closing the channel and setting the current thread's
  146. * interrupt status
  147. *
  148. * @throws SecurityException
  149. * If a security manager has been installed
  150. * and it does not permit access to the given remote address
  151. *
  152. * @throws IOException
  153. * If some other I/O error occurs
  154. */
  155. public abstract DatagramChannel connect(SocketAddress remote)
  156. throws IOException;
  157. /**
  158. * Disconnects this channel's socket.
  159. *
  160. * <p> The channel's socket is configured so that it can receive datagrams
  161. * from, and sends datagrams to, any remote address so long as the security
  162. * manager, if installed, permits it.
  163. *
  164. * <p> This method may be invoked at any time. It will not have any effect
  165. * on read or write operations that are already in progress at the moment
  166. * that it is invoked.
  167. *
  168. * <p> If this channel's socket is not connected, or if the channel is
  169. * closed, then invoking this method has no effect. </p>
  170. *
  171. * @return This datagram channel
  172. *
  173. * @throws IOException
  174. * If some other I/O error occurs
  175. */
  176. public abstract DatagramChannel disconnect() throws IOException;
  177. /**
  178. * Receives a datagram via this channel.
  179. *
  180. * <p> If a datagram is immediately available, or if this channel is in
  181. * blocking mode and one eventually becomes available, then the datagram is
  182. * copied into the given byte buffer and its source address is returned.
  183. * If this channel is in non-blocking mode and a datagram is not
  184. * immediately available then this method immediately returns
  185. * <tt>null</tt>.
  186. *
  187. * <p> The datagram is transferred into the given byte buffer starting at
  188. * its current position, as if by a regular {@link
  189. * ReadableByteChannel#read(java.nio.ByteBuffer) read} operation. If there
  190. * are fewer bytes remaining in the buffer than are required to hold the
  191. * datagram then the remainder of the datagram is silently discarded.
  192. *
  193. * <p> This method performs exactly the same security checks as the {@link
  194. * java.net.DatagramSocket#receive receive} method of the {@link
  195. * java.net.DatagramSocket} class. That is, if the socket is not connected
  196. * to a specific remote address and a security manager has been installed
  197. * then for each datagram received this method verifies that the source's
  198. * address and port number are permitted by the security manager's {@link
  199. * java.lang.SecurityManager#checkAccept checkAccept} method. The overhead
  200. * of this security check can be avoided by first connecting the socket via
  201. * the {@link #connect connect} method.
  202. *
  203. * <p> This method may be invoked at any time. If another thread has
  204. * already initiated a read operation upon this channel, however, then an
  205. * invocation of this method will block until the first operation is
  206. * complete. </p>
  207. *
  208. * @param dst
  209. * The buffer into which the datagram is to be transferred
  210. *
  211. * @return The datagram's source address,
  212. * or <tt>null</tt> if this channel is in non-blocking mode
  213. * and no datagram was immediately available
  214. *
  215. * @throws ClosedChannelException
  216. * If this channel is closed
  217. *
  218. * @throws AsynchronousCloseException
  219. * If another thread closes this channel
  220. * while the read operation is in progress
  221. *
  222. * @throws ClosedByInterruptException
  223. * If another thread interrupts the current thread
  224. * while the read operation is in progress, thereby
  225. * closing the channel and setting the current thread's
  226. * interrupt status
  227. *
  228. * @throws SecurityException
  229. * If a security manager has been installed
  230. * and it does not permit datagrams to be accepted
  231. * from the datagram's sender
  232. *
  233. * @throws IOException
  234. * If some other I/O error occurs
  235. */
  236. public abstract SocketAddress receive(ByteBuffer dst) throws IOException;
  237. /**
  238. * Sends a datagram via this channel.
  239. *
  240. * <p> If this channel is in non-blocking mode and there is sufficient room
  241. * in the underlying output buffer, or if this channel is in blocking mode
  242. * and sufficient room becomes available, then the remaining bytes in the
  243. * given buffer are transmitted as a single datagram to the given target
  244. * address.
  245. *
  246. * <p> The datagram is transferred from the byte buffer as if by a regular
  247. * {@link WritableByteChannel#write(java.nio.ByteBuffer) write} operation.
  248. *
  249. * <p> This method performs exactly the same security checks as the {@link
  250. * java.net.DatagramSocket#send send} method of the {@link
  251. * java.net.DatagramSocket} class. That is, if the socket is not connected
  252. * to a specific remote address and a security manager has been installed
  253. * then for each datagram sent this method verifies that the target address
  254. * and port number are permitted by the security manager's {@link
  255. * java.lang.SecurityManager#checkConnect checkConnect} method. The
  256. * overhead of this security check can be avoided by first connecting the
  257. * socket via the {@link #connect connect} method.
  258. *
  259. * <p> This method may be invoked at any time. If another thread has
  260. * already initiated a write operation upon this channel, however, then an
  261. * invocation of this method will block until the first operation is
  262. * complete. </p>
  263. *
  264. * @param src
  265. * The buffer containing the datagram to be sent
  266. *
  267. * @param target
  268. * The address to which the datagram is to be sent
  269. *
  270. * @return The number of bytes sent, which will be either the number
  271. * of bytes that were remaining in the source buffer when this
  272. * method was invoked or, if this channel is non-blocking, may be
  273. * zero if there was insufficient room for the datagram in the
  274. * underlying output buffer
  275. *
  276. * @throws ClosedChannelException
  277. * If this channel is closed
  278. *
  279. * @throws AsynchronousCloseException
  280. * If another thread closes this channel
  281. * while the read operation is in progress
  282. *
  283. * @throws ClosedByInterruptException
  284. * If another thread interrupts the current thread
  285. * while the read operation is in progress, thereby
  286. * closing the channel and setting the current thread's
  287. * interrupt status
  288. *
  289. * @throws SecurityException
  290. * If a security manager has been installed
  291. * and it does not permit datagrams to be sent
  292. * to the given address
  293. *
  294. * @throws IOException
  295. * If some other I/O error occurs
  296. */
  297. public abstract int send(ByteBuffer src, SocketAddress target)
  298. throws IOException;
  299. // -- ByteChannel operations --
  300. /**
  301. * Reads a datagram from this channel.
  302. *
  303. * <p> This method may only be invoked if this channel's socket is
  304. * connected, and it only accepts datagrams from the socket's peer. If
  305. * there are more bytes in the datagram than remain in the given buffer
  306. * then the remainder of the datagram is silently discarded. Otherwise
  307. * this method behaves exactly as specified in the {@link
  308. * ReadableByteChannel} interface. </p>
  309. *
  310. * @throws NotYetConnectedException
  311. * If this channel's socket is not connected
  312. */
  313. public abstract int read(ByteBuffer dst) throws IOException;
  314. /**
  315. * Reads a datagram from this channel.
  316. *
  317. * <p> This method may only be invoked if this channel's socket is
  318. * connected, and it only accepts datagrams from the socket's peer. If
  319. * there are more bytes in the datagram than remain in the given buffers
  320. * then the remainder of the datagram is silently discarded. Otherwise
  321. * this method behaves exactly as specified in the {@link
  322. * ScatteringByteChannel} interface. </p>
  323. *
  324. * @throws NotYetConnectedException
  325. * If this channel's socket is not connected
  326. */
  327. public abstract long read(ByteBuffer[] dsts, int offset, int length)
  328. throws IOException;
  329. /**
  330. * Reads a datagram from this channel.
  331. *
  332. * <p> This method may only be invoked if this channel's socket is
  333. * connected, and it only accepts datagrams from the socket's peer. If
  334. * there are more bytes in the datagram than remain in the given buffers
  335. * then the remainder of the datagram is silently discarded. Otherwise
  336. * this method behaves exactly as specified in the {@link
  337. * ScatteringByteChannel} interface. </p>
  338. *
  339. * @throws NotYetConnectedException
  340. * If this channel's socket is not connected
  341. */
  342. public final long read(ByteBuffer[] dsts) throws IOException {
  343. return read(dsts, 0, dsts.length);
  344. }
  345. /**
  346. * Writes a datagram to this channel.
  347. *
  348. * <p> This method may only be invoked if this channel's socket is
  349. * connected, in which case it sends datagrams directly to the socket's
  350. * peer. Otherwise it behaves exactly as specified in the {@link
  351. * WritableByteChannel} interface. </p>
  352. *
  353. * @throws NotYetConnectedException
  354. * If this channel's socket is not connected
  355. */
  356. public abstract int write(ByteBuffer src) throws IOException;
  357. /**
  358. * Writes a datagram to this channel.
  359. *
  360. * <p> This method may only be invoked if this channel's socket is
  361. * connected, in which case it sends datagrams directly to the socket's
  362. * peer. Otherwise it behaves exactly as specified in the {@link
  363. * GatheringByteChannel} interface. </p>
  364. *
  365. * @return The number of bytes sent, which will be either the number
  366. * of bytes that were remaining in the source buffer when this
  367. * method was invoked or, if this channel is non-blocking, may be
  368. * zero if there was insufficient room for the datagram in the
  369. * underlying output buffer
  370. *
  371. * @throws NotYetConnectedException
  372. * If this channel's socket is not connected
  373. */
  374. public abstract long write(ByteBuffer[] srcs, int offset, int length)
  375. throws IOException;
  376. /**
  377. * Writes a datagram to this channel.
  378. *
  379. * <p> This method may only be invoked if this channel's socket is
  380. * connected, in which case it sends datagrams directly to the socket's
  381. * peer. Otherwise it behaves exactly as specified in the {@link
  382. * GatheringByteChannel} interface. </p>
  383. *
  384. * @return The number of bytes sent, which will be either the number
  385. * of bytes that were remaining in the source buffer when this
  386. * method was invoked or, if this channel is non-blocking, may be
  387. * zero if there was insufficient room for the datagram in the
  388. * underlying output buffer
  389. *
  390. * @throws NotYetConnectedException
  391. * If this channel's socket is not connected
  392. */
  393. public final long write(ByteBuffer[] srcs) throws IOException {
  394. return write(srcs, 0, srcs.length);
  395. }
  396. }