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