DatagramChannelpublic abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannelA selectable channel for datagram-oriented sockets.
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.
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.
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.
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. |
Constructors Summary |
---|
protected DatagramChannel(SelectorProvider provider)Initializes a new instance of this class.
super(provider);
|
Methods Summary |
---|
public abstract java.nio.channels.DatagramChannel | connect(java.net.SocketAddress remote)Connects this channel's socket.
The channel's socket is configured so that it only receives
datagrams from, and sends datagrams to, the given remote peer
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.
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.
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.
| public abstract java.nio.channels.DatagramChannel | disconnect()Disconnects this channel's socket.
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.
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.
If this channel's socket is not connected, or if the channel is
closed, then invoking this method has no effect.
| public abstract boolean | isConnected()Tells whether or not this channel's socket is connected.
| public static java.nio.channels.DatagramChannel | open()Opens a datagram channel.
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.
return SelectorProvider.provider().openDatagramChannel();
| public abstract int | read(java.nio.ByteBuffer dst)Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffer
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the {@link
ReadableByteChannel} interface.
| public abstract long | read(java.nio.ByteBuffer[] dsts, int offset, int length)Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the {@link
ScatteringByteChannel} interface.
| public final long | read(java.nio.ByteBuffer[] dsts)Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer. If
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded. Otherwise
this method behaves exactly as specified in the {@link
ScatteringByteChannel} interface.
return read(dsts, 0, dsts.length);
| public abstract java.net.SocketAddress | receive(java.nio.ByteBuffer dst)Receives a datagram via this channel.
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
null.
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.
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
then for each datagram received this method verifies that the source's
address and port number are permitted by the security manager's {@link
java.lang.SecurityManager#checkAccept checkAccept} method. The overhead
of this security check can be avoided by first connecting the socket via
the {@link #connect connect} method.
This method may be invoked at any time. If another thread has
already initiated a read operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete.
| public abstract int | send(java.nio.ByteBuffer src, java.net.SocketAddress target)Sends a datagram via this channel.
If this channel is in non-blocking mode and there is sufficient room
in the underlying output buffer, or if this channel is in blocking mode
and sufficient room becomes available, then the remaining bytes in the
given buffer are transmitted as a single datagram to the given target
address.
The datagram is transferred from the byte buffer as if by a regular
{@link WritableByteChannel#write(java.nio.ByteBuffer) write} operation.
This method performs exactly the same security checks as the {@link
java.net.DatagramSocket#send send} 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
then for each datagram sent this method verifies that the target address
and port number are permitted by the security manager's {@link
java.lang.SecurityManager#checkConnect checkConnect} method. The
overhead of this security check can be avoided by first connecting the
socket via the {@link #connect connect} method.
This method may be invoked at any time. If another thread has
already initiated a write operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete.
| public abstract java.net.DatagramSocket | socket()Retrieves a datagram socket associated with this channel.
The returned object will not declare any public methods that are not
declared in the {@link java.net.DatagramSocket} class.
| public final int | validOps()Returns an operation set identifying this channel's supported
operations.
Datagram channels support reading and writing, so this method
returns ({@link SelectionKey#OP_READ} | {@link
SelectionKey#OP_WRITE}).
return (SelectionKey.OP_READ
| SelectionKey.OP_WRITE);
| public abstract int | write(java.nio.ByteBuffer src)Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the {@link
WritableByteChannel} interface.
| public abstract long | write(java.nio.ByteBuffer[] srcs, int offset, int length)Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the {@link
GatheringByteChannel} interface.
| public final long | write(java.nio.ByteBuffer[] srcs)Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
peer. Otherwise it behaves exactly as specified in the {@link
GatheringByteChannel} interface.
return write(srcs, 0, srcs.length);
|
|