public abstract class DatagramChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel
DatagramChannel
is a selectable channel that represents a partial
abstraction of a datagram socket. The socket
method of this class can
return the related DatagramSocket
instance, which can handle the
socket.
A datagram channel is open but not connected when created with the
open()
method. After it is connected, it will keep the connected
status until it is disconnected or closed. The benefit of a connected channel
is the reduced effort of security checks during send and receive. When
invoking read
or write
, a connected channel is required.
Datagram channels are thread-safe; only one thread can read or write at the same time.
Modifier | Constructor and Description |
---|---|
protected |
DatagramChannel(SelectorProvider selectorProvider)
Constructs a new
DatagramChannel . |
Modifier and Type | Method and Description |
---|---|
abstract DatagramChannel |
connect(SocketAddress address)
Connects the socket of this channel to a remote address, which is the
only communication peer for getting and sending datagrams after being
connected.
|
abstract DatagramChannel |
disconnect()
Disconnects the socket of this channel, which has been connected before
in order to send and receive datagrams.
|
abstract boolean |
isConnected()
Returns whether this channel's socket is connected or not.
|
static DatagramChannel |
open()
Creates an opened and not-connected datagram channel.
|
abstract int |
read(ByteBuffer target)
Reads a datagram from this channel into the byte buffer.
|
long |
read(ByteBuffer[] targets)
Reads a datagram from this channel into an array of byte buffers.
|
abstract long |
read(ByteBuffer[] targets,
int offset,
int length)
Reads a datagram from this channel into an array of byte buffers.
|
abstract SocketAddress |
receive(ByteBuffer target)
Gets a datagram from this channel.
|
abstract int |
send(ByteBuffer source,
SocketAddress address)
Sends a datagram through this channel.
|
abstract DatagramSocket |
socket()
Returns the related datagram socket of this channel, which does not
define additional public methods to those defined by
DatagramSocket . |
int |
validOps()
Gets the valid operations of this channel.
|
abstract int |
write(ByteBuffer source)
Writes a datagram from the byte buffer to this channel.
|
long |
write(ByteBuffer[] sources)
Writes a datagram from the byte buffers to this channel.
|
abstract long |
write(ByteBuffer[] sources,
int offset,
int length)
Writes a datagram from the byte buffers to this channel.
|
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, register
register
begin, close, end, isOpen
protected DatagramChannel(SelectorProvider selectorProvider)
DatagramChannel
.selectorProvider
- an instance of SelectorProvider.public static DatagramChannel open() throws IOException
This channel is created by calling the openDatagramChannel
method of the default SelectorProvider
instance.
IOException
- if some I/O error occurs.public final int validOps()
SelectionKey.OP_READ
| SelectionKey.OP_WRITE
).validOps
in class SelectableChannel
SelectableChannel.validOps()
public abstract DatagramSocket socket()
DatagramSocket
.public abstract boolean isConnected()
true
if this channel's socket is connected;
false
otherwise.public abstract DatagramChannel connect(SocketAddress address) throws IOException
This method can be called at any time without affecting the read and write operations being processed at the time the method is called. The connection status does not change until the channel is disconnected or closed.
This method executes the same security checks as the connect method of
the DatagramSocket
class.
address
- the address to be connected to.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- if some other I/O error occurs.public abstract DatagramChannel disconnect() throws IOException
This method can be called at any time without affecting the read and write operations being underway. It does not have any effect if the socket is not connected or the channel is closed.
IOException
- some other I/O error occurs.public abstract SocketAddress receive(ByteBuffer target) throws IOException
This method transfers a datagram from the channel into the target byte
buffer. If this channel is in blocking mode, it waits for the datagram
and returns its address when it is available. If this channel is in
non-blocking mode and no datagram is available, it returns null
immediately. The transfer starts at the current position of the buffer,
and if there is not enough space remaining in the buffer to store the
datagram then the part of the datagram that does not fit is discarded.
This method can be called at any time and it will block if there is another thread that has started a read operation on the channel.
This method executes the same security checks as the receive method of
the DatagramSocket
class.
target
- the byte buffer to store the received datagram.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.public abstract int send(ByteBuffer source, SocketAddress address) throws IOException
source
.
If this channel is in blocking mode then the datagram is sent as soon as there is enough space in the underlying output buffer. If this channel is in non-blocking mode then the datagram is only sent if there is enough space in the underlying output buffer at that moment. The transfer action is just like a regular write operation.
This method can be called at any time and it will block if another thread has started a send operation on this channel.
This method executes the same security checks as the send method of the
DatagramSocket
class.
source
- the byte buffer with the datagram to be sent.address
- the destination address for the datagram.source
or zero if the channel is in non-blocking mode
and there is not enough space for the datagram in the underlying
output buffer.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.public abstract int read(ByteBuffer target) throws IOException
The precondition for calling this method is that the channel is connected
and the incoming datagram is from the connected address. If the buffer is
not big enough to store the datagram, the part of the datagram that does
not fit in the buffer is discarded. Otherwise, this method has the same
behavior as the read
method in the ReadableByteChannel
interface.
read
in interface ReadableByteChannel
target
- the byte buffer to store the received datagram.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.ReadableByteChannel.read(java.nio.ByteBuffer)
public abstract long read(ByteBuffer[] targets, int offset, int length) throws IOException
The precondition for calling this method is that the channel is connected
and the incoming datagram is from the connected address. If the buffers
do not have enough remaining space to store the datagram, the part of the
datagram that does not fit in the buffers is discarded. Otherwise, this
method has the same behavior as the read
method in the
ScatteringByteChannel
interface.
read
in interface ScatteringByteChannel
targets
- the byte buffers to store the received datagram.offset
- a non-negative offset in the array of buffers, pointing to the
starting buffer to store the bytes transferred, must not be
bigger than targets.length
.length
- a non-negative length to indicate the maximum number of
buffers to be filled, must not be bigger than
targets.length - offset
.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.ScatteringByteChannel.read(java.nio.ByteBuffer[],
int, int)
public final long read(ByteBuffer[] targets) throws IOException
The precondition for calling this method is that the channel is connected
and the incoming datagram is from the connected address. If the buffers
do not have enough remaining space to store the datagram, the part of the
datagram that does not fit in the buffers is discarded. Otherwise, this
method has the same behavior as the read
method in the
ScatteringByteChannel
interface.
read
in interface ScatteringByteChannel
targets
- the byte buffers to store the received datagram.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.ScatteringByteChannel.read(java.nio.ByteBuffer[])
public abstract int write(ByteBuffer source) throws IOException
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. Otherwise, this method
has the same behavior as the write
method in the
WritableByteChannel
interface.
write
in interface WritableByteChannel
source
- the byte buffer as the source of the datagram.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.WritableByteChannel.write(java.nio.ByteBuffer)
public abstract long write(ByteBuffer[] sources, int offset, int length) throws IOException
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. Otherwise, this method
has the same behavior as the write
method in the
GatheringByteChannel
interface.
write
in interface GatheringByteChannel
sources
- the byte buffers as the source of the datagram.offset
- a non-negative offset in the array of buffers, pointing to the
starting buffer to be retrieved, must be no larger than
sources.length
.length
- a non-negative length to indicate the maximum number of
buffers to be submitted, must be no bigger than
sources.length - offset
.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.GatheringByteChannel.write(java.nio.ByteBuffer[],
int, int)
public final long write(ByteBuffer[] sources) throws IOException
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. Otherwise, this method
has the same behavior as the write method in the
GatheringByteChannel
interface.
write
in interface GatheringByteChannel
sources
- the byte buffers as the source of the datagram.NotYetConnectedException
- if the channel is not connected yet.ClosedChannelException
- if the channel is already closed.AsynchronousCloseException
- if the channel is closed by another thread while this method
is in operation.ClosedByInterruptException
- if another thread interrupts the calling thread while the
operation is in progress. The calling thread will have the
interrupt state set and the channel will be closed.IOException
- some other I/O error occurs.GatheringByteChannel.write(java.nio.ByteBuffer[])