| java.nio.channels.SelectableChannel java.nio.channels.spi.AbstractSelectableChannel java.nio.channels.DatagramChannel
DatagramChannel | abstract public class DatagramChannel extends AbstractSelectableChannel implements ByteChannel,ScatteringByteChannel,GatheringByteChannel(Code) | | A DatagramChannel is a selectable channel for part abstraction of 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 by
open method. After connected, it will keep the connected
status before disconnecting or closing. 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, no more than one thread can read or write
at given time.
|
Method Summary | |
abstract public DatagramChannel | connect(SocketAddress address) Connect the socket of this channel to a remote address, which is the only
communication peer of getting and sending datagrams after connected.
This method can be called at any moment, and won't affect the processing
read and write operation. | abstract public DatagramChannel | disconnect() Disconnect the socket of this channel, which is connected before for
getting and sending datagrams.
This method can be called at any moment, and won't affect the processing
read and write operation. | abstract public boolean | isConnected() Answer whether this channel's socket is connected or not. | public static DatagramChannel | open() Create a open and not-connected datagram channel. | abstract public int | read(ByteBuffer target) Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. | abstract public long | read(ByteBuffer[] targets, int offset, int length) Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. | final public synchronized long | read(ByteBuffer[] targets) Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. | abstract public SocketAddress | receive(ByteBuffer target) Get a datagram from this channel.
This method transfers the datagram from the channel into the target byte
buffer and return the address of the datagram, if the datagram is
available or will be available as this channel is in blocking mode. | abstract public int | send(ByteBuffer source, SocketAddress address) Sends out a datagram by the channel.
The precondition of sending is that whether the channel is in blocking
mode and enough byte buffer space will be available, or the channel is in
non-blocking mode and byte buffer space is enough. | abstract public DatagramSocket | socket() Return the related datagram socket of this channel, which won't declare
public methods that not declared in DatagramSocket . | final public int | validOps() Get the valid operations of this channel. | abstract public int | write(ByteBuffer source) Write datagram from the byte buffer into the channel.
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. | abstract public long | write(ByteBuffer[] sources, int offset, int length) Write datagram from the byte buffer into the channel.
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. | final public synchronized long | write(ByteBuffer[] sources) Write datagram from the byte buffer into the channel.
The precondition of calling this method is that the channel is connected
and the datagram is sent to the connected address. |
DatagramChannel | protected DatagramChannel(SelectorProvider selectorProvider)(Code) | | Constructor for this class.
Parameters: selectorProvider - A instance of SelectorProvider |
connect | abstract public DatagramChannel connect(SocketAddress address) throws IOException(Code) | | Connect the socket of this channel to a remote address, which is the only
communication peer of getting and sending datagrams after connected.
This method can be called at any moment, and won't affect the processing
read and write operation. The connect status won't changed before
disconnected and closed.
This method just execute the same security checks as the connect method
of the DatagramSocket class.
Parameters: address - The address to be connected. This channel. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: SecurityException - If there is a security manager, and the address is notpermitted to access. throws: IOException - Some other IO error occurred. |
disconnect | abstract public DatagramChannel disconnect() throws IOException(Code) | | Disconnect the socket of this channel, which is connected before for
getting and sending datagrams.
This method can be called at any moment, and won't affect the processing
read and write operation. It won't has any effect if the socket is not
connected or the channel is closed.
This channel. throws: IOException - Some other IO error occurred. |
isConnected | abstract public boolean isConnected()(Code) | | Answer whether this channel's socket is connected or not.
true for this channel's socket is connected;false otherwise. |
open | public static DatagramChannel open() throws IOException(Code) | | Create a open and not-connected datagram channel.
This channel is got by openDatagramChannel method of the
default SelectorProvider instance.
The new created channel which is open but not-connected. throws: IOException - If some IO problem occurs. |
read | abstract public int read(ByteBuffer target) throws IOException(Code) | | Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. If the buffer is
not enough to store the datagram, the residual part of the datagram is
ignored. Otherwise, this method has the same behavior as the read method
in the ReadableByteChannel interface.
See Also: java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) Parameters: target - The byte buffer to store the received datagram. Non-negative number as the number of bytes read, or -1 as theread operation reaches the end of stream. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
read | abstract public long read(ByteBuffer[] targets, int offset, int length) throws IOException(Code) | | Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. If the buffer is
not enough to store the datagram, the residual part of the datagram is
ignored. Otherwise, this method has the same behavior as the read method
in the ScatteringByteChannel interface.
See Also: java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]intint) Parameters: targets - The byte buffers to store the received datagram. Parameters: offset - A non-negative offset in the array of buffer, pointing to thestarting buffer to store the byte transferred, must no largerthan targets.length. Parameters: length - A non-negative length to indicate the maximum number of byteto be read, must no larger than targets.length - offset. Non-negative number as the number of bytes read, or -1 as theread operation reaches the end of stream. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
read | final public synchronized long read(ByteBuffer[] targets) throws IOException(Code) | | Reads datagram from the channel into the byte buffer.
The precondition of calling this method is that the channel is connected
and the coming datagram is from the connected address. If the buffer is
not enough to store the datagram, the residual part of the datagram is
ignored. Otherwise, this method has the same behavior as the read method
in the ScatteringByteChannel interface.
See Also: java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) Parameters: targets - The byte buffers to store the received datagram. Non-negative number as the number of bytes read, or -1 as theread operation reaches the end of stream. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
receive | abstract public SocketAddress receive(ByteBuffer target) throws IOException(Code) | | Get a datagram from this channel.
This method transfers the datagram from the channel into the target byte
buffer and return the address of the datagram, if the datagram is
available or will be available as this channel is in blocking mode. This
method returns null if the datagram is not available now
and the channel is in non-blocking mode. The transfer start at the
current position of the buffer, and the residual part of the datagram
will be ignored if there is no efficient remaining in the buffer to store
the datagram.
This method can be called at any moment, and will block if there is
another thread started a read operation on the channel.
This method just execute the same security checks as the receive method
of the DatagramSocket class.
Parameters: target - The byte buffer to store the received datagram. Address of the datagram if the transfer is performed, or null ifthe channel is in non-blocking mode and the datagram areunavailable. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: SecurityException - If there is a security manager, and the address is notpermitted to access. throws: IOException - Some other IO error occurred. |
send | abstract public int send(ByteBuffer source, SocketAddress address) throws IOException(Code) | | Sends out a datagram by the channel.
The precondition of sending is that whether the channel is in blocking
mode and enough byte buffer space will be available, or the channel is in
non-blocking mode and byte buffer space is enough. The transfer action is
just like a regular write operation.
This method can be called at any moment, and will block if there is
another thread started a read operation on the channel.
This method just execute the same security checks as the send method of
the DatagramSocket class.
Parameters: source - The byte buffer with the datagram to be sent. Parameters: address - The address to be sent. The number of sent bytes. If this method is called, it returnsthe number of bytes that remaining in the byte buffer. If thechannel is in non-blocking mode and no enough space for thedatagram in the buffer, it may returns zero. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: SecurityException - If there is a security manager, and the address is notpermitted to access. throws: IOException - Some other IO error occurred. |
socket | abstract public DatagramSocket socket()(Code) | | Return the related datagram socket of this channel, which won't declare
public methods that not declared in DatagramSocket .
The related DatagramSocket instance. |
validOps | final public int validOps()(Code) | | Get the valid operations of this channel. Datagram channels support read
and write operation, so this method returns (
SelectionKey.OP_READ | SelectionKey.OP_WRITE ).
See Also: java.nio.channels.SelectableChannel.validOps Valid operations in bit-set. |
write | abstract public int write(ByteBuffer source) throws IOException(Code) | | Write datagram from the byte buffer into the channel.
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.
See Also: java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) Parameters: source - The byte buffer as the source of the datagram. Non-negative number of bytes written. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
write | abstract public long write(ByteBuffer[] sources, int offset, int length) throws IOException(Code) | | Write datagram from the byte buffer into the channel.
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.
See Also: java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]intint) Parameters: sources - The byte buffers as the source of the datagram. Parameters: offset - A non-negative offset in the array of buffer, pointing to thestarting buffer to be retrieved, must no larger thansources.length. Parameters: length - A non-negative length to indicate the maximum number of byteto be written, must no larger than sources.length - offset. The number of written bytes. If this method is called, it returnsthe number of bytes that remaining in the byte buffer. If thechannel is in non-blocking mode and no enough space for thedatagram in the buffer, it may returns zero. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
write | final public synchronized long write(ByteBuffer[] sources) throws IOException(Code) | | Write datagram from the byte buffer into the channel.
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.
See Also: java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) Parameters: sources - The byte buffers as the source of the datagram. The number of written bytes. If this method is called, it returnsthe number of bytes that remaining in the byte buffer. If thechannel is in non-blocking mode and no enough space for thedatagram in the buffer, it may returns zero. throws: NotYetConnectedException - If the channel is not connected yet. throws: ClosedChannelException - If the channel is already closed. throws: AsynchronousCloseException - If the channel is closed by another thread while this methodis in operation. throws: ClosedByInterruptException - If another thread interrupts the calling thread while theoperation is in progress. The calling thread will have theinterrupt state set, and the channel will be closed. throws: IOException - Some other IO error occurred. |
|
|