📄 datagramchannel.java
字号:
/* * @(#)DatagramChannel.java 1.31 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.nio.channels;import java.io.IOException;import java.net.DatagramSocket;import java.net.SocketAddress;import java.nio.ByteBuffer;import java.nio.channels.spi.*;/** * A selectable channel for datagram-oriented sockets. * * * <p> Datagram channels are not a complete abstraction of network datagram * sockets. Binding and the manipulation of socket options must be done * through an associated {@link java.net.DatagramSocket} object obtained by * invoking the {@link #socket() socket} method. It is not possible to create * a channel for an arbitrary, pre-existing datagram socket, nor is it possible * to specify the {@link java.net.DatagramSocketImpl} object to be used by a * datagram socket associated with a datagram channel. * * <p> A datagram channel is created by invoking the {@link #open open} method * of this class. A newly-created datagram channel is open but not connected. * A datagram channel need not be connected in order for the {@link #send send} * and {@link #receive receive} methods to be used. A datagram channel may be * connected, by invoking its {@link #connect connect} method, in order to * avoid the overhead of the security checks are otherwise performed as part of * every send and receive operation. A datagram channel must be connected in * order to use the {@link #read(java.nio.ByteBuffer) read} and {@link * #write(java.nio.ByteBuffer) write} methods, since those methods do not * accept or return socket addresses. * * <p> Once connected, a datagram channel remains connected until it is * disconnected or closed. Whether or not a datagram channel is connected may * be determined by invoking its {@link #isConnected isConnected} method. * * <p> Datagram channels are safe for use by multiple concurrent threads. They * support concurrent reading and writing, though at most one thread may be * reading and at most one thread may be writing at any given time. </p> * * * @author Mark Reinhold * @author JSR-51 Expert Group * @version 1.31, 03/01/23 * @since 1.4 */public abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel{ /** * Initializes a new instance of this class. */ protected DatagramChannel(SelectorProvider provider) { super(provider); } /** * Opens a datagram channel. * * <p> The new channel is created by invoking the {@link * java.nio.channels.spi.SelectorProvider#openDatagramChannel() * openDatagramChannel} method of the system-wide default {@link * java.nio.channels.spi.SelectorProvider} object. The channel will not be * connected. </p> * * @return A new datagram channel * * @throws IOException * If an I/O error occurs */ public static DatagramChannel open() throws IOException { return SelectorProvider.provider().openDatagramChannel(); } /** * Returns an operation set identifying this channel's supported * operations. * * <p> Datagram channels support reading and writing, so this method * returns <tt>(</tt>{@link SelectionKey#OP_READ} <tt>|</tt> {@link * SelectionKey#OP_WRITE}<tt>)</tt>. </p> * * @return The valid-operation set */ public final int validOps() { return (SelectionKey.OP_READ | SelectionKey.OP_WRITE); } // -- Socket-specific operations -- /** * Retrieves a datagram socket associated with this channel. * * <p> The returned object will not declare any public methods that are not * declared in the {@link java.net.DatagramSocket} class. </p> * * @return A datagram socket associated with this channel */ public abstract DatagramSocket socket(); /** * Tells whether or not this channel's socket is connected. </p> * * @return <tt>true</tt> if, and only if, this channel's socket * is connected */ public abstract boolean isConnected(); /** * Connects this channel's socket. * * <p> The channel's socket is configured so that it only receives * datagrams from, and sends datagrams to, the given remote <i>peer</i> * address. Once connected, datagrams may not be received from or sent to * any other address. A datagram socket remains connected until it is * explicitly disconnected or until it is closed. * * <p> This method performs exactly the same security checks as the {@link * java.net.DatagramSocket#connect connect} method of the {@link * java.net.DatagramSocket} class. That is, if a security manager has been * installed then this method verifies that its {@link * java.lang.SecurityManager#checkAccept checkAccept} and {@link * java.lang.SecurityManager#checkConnect checkConnect} methods permit * datagrams to be received from and sent to, respectively, the given * remote address. * * <p> This method may be invoked at any time. It will not have any effect * on read or write operations that are already in progress at the moment * that it is invoked. </p> * * @param remote * The remote address to which this channel is to be connected * * @return This datagram channel * * @throws ClosedChannelException * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel * while the connect operation is in progress * * @throws ClosedByInterruptException * If another thread interrupts the current thread * while the connect operation is in progress, thereby * closing the channel and setting the current thread's * interrupt status * * @throws SecurityException * If a security manager has been installed * and it does not permit access to the given remote address * * @throws IOException * If some other I/O error occurs */ public abstract DatagramChannel connect(SocketAddress remote) throws IOException; /** * Disconnects this channel's socket. * * <p> The channel's socket is configured so that it can receive datagrams * from, and sends datagrams to, any remote address so long as the security * manager, if installed, permits it. * * <p> This method may be invoked at any time. It will not have any effect * on read or write operations that are already in progress at the moment * that it is invoked. * * <p> If this channel's socket is not connected, or if the channel is * closed, then invoking this method has no effect. </p> * * @return This datagram channel * * @throws IOException * If some other I/O error occurs */ public abstract DatagramChannel disconnect() throws IOException; /** * Receives a datagram via this channel. * * <p> If a datagram is immediately available, or if this channel is in * blocking mode and one eventually becomes available, then the datagram is * copied into the given byte buffer and its source address is returned. * If this channel is in non-blocking mode and a datagram is not * immediately available then this method immediately returns * <tt>null</tt>. * * <p> The datagram is transferred into the given byte buffer starting at * its current position, as if by a regular {@link * ReadableByteChannel#read(java.nio.ByteBuffer) read} operation. If there * are fewer bytes remaining in the buffer than are required to hold the * datagram then the remainder of the datagram is silently discarded. * * <p> This method performs exactly the same security checks as the {@link * java.net.DatagramSocket#receive receive} method of the {@link * java.net.DatagramSocket} class. That is, if the socket is not connected * to a specific remote address and a security manager has been installed
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -