java.nio.channels
Class DatagramChannel

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel 
      extended by java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel 
          extended by java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel 
              extended by java.nio.channels.DatagramChannel
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel , InterruptibleChannel sample code for java.nio.channels.InterruptibleChannel definition code for java.nio.channels.InterruptibleChannel , ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel , WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel

public abstract class DatagramChannel
extends AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel
implements ByteChannel sample code for java.nio.channels.ByteChannel definition code for java.nio.channels.ByteChannel , ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel , GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel

A 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 DatagramSocket sample code for java.net.DatagramSocket definition code for java.net.DatagramSocket object obtained by invoking the socket sample code for java.nio.channels.DatagramChannel.socket() definition code for java.nio.channels.DatagramChannel.socket() method. It is not possible to create a channel for an arbitrary, pre-existing datagram socket, nor is it possible to specify the DatagramSocketImpl sample code for java.net.DatagramSocketImpl definition code for java.net.DatagramSocketImpl object to be used by a datagram socket associated with a datagram channel.

A datagram channel is created by invoking the open sample code for java.nio.channels.DatagramChannel.open() definition code for java.nio.channels.DatagramChannel.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 send sample code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress) and receive sample code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer) methods to be used. A datagram channel may be connected, by invoking its connect sample code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) 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 read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer) and write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer) 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 isConnected sample code for java.nio.channels.DatagramChannel.isConnected() definition code for java.nio.channels.DatagramChannel.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.

Since:
1.4

Constructor Summary
protected DatagramChannel sample code for java.nio.channels.DatagramChannel.DatagramChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.DatagramChannel.DatagramChannel(java.nio.channels.spi.SelectorProvider) (SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
          Initializes a new instance of this class.
 
Method Summary
abstract  DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel connect sample code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
          Connects this channel's socket.
abstract  DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel disconnect sample code for java.nio.channels.DatagramChannel.disconnect() definition code for java.nio.channels.DatagramChannel.disconnect() ()
          Disconnects this channel's socket.
abstract  boolean isConnected sample code for java.nio.channels.DatagramChannel.isConnected() definition code for java.nio.channels.DatagramChannel.isConnected() ()
          Tells whether or not this channel's socket is connected.
static DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel open sample code for java.nio.channels.DatagramChannel.open() definition code for java.nio.channels.DatagramChannel.open() ()
          Opens a datagram channel.
abstract  int read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
          Reads a datagram from this channel.
 long read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
          Reads a datagram from this channel.
abstract  long read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts, int offset, int length)
          Reads a datagram from this channel.
abstract  SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress receive sample code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
          Receives a datagram via this channel.
abstract  int send sample code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src, SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  target)
          Sends a datagram via this channel.
abstract  DatagramSocket sample code for java.net.DatagramSocket definition code for java.net.DatagramSocket socket sample code for java.nio.channels.DatagramChannel.socket() definition code for java.nio.channels.DatagramChannel.socket() ()
          Retrieves a datagram socket associated with this channel.
 int validOps sample code for java.nio.channels.DatagramChannel.validOps() definition code for java.nio.channels.DatagramChannel.validOps() ()
          Returns an operation set identifying this channel's supported operations.
abstract  int write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
          Writes a datagram to this channel.
 long write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[]) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
          Writes a datagram to this channel.
abstract  long write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[], int, int) (ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs, int offset, int length)
          Writes a datagram to this channel.
 
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel
blockingLock sample code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() definition code for java.nio.channels.spi.AbstractSelectableChannel.blockingLock() , configureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.configureBlocking(boolean) , implCloseChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel() , implCloseSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() definition code for java.nio.channels.spi.AbstractSelectableChannel.implCloseSelectableChannel() , implConfigureBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) definition code for java.nio.channels.spi.AbstractSelectableChannel.implConfigureBlocking(boolean) , isBlocking sample code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() definition code for java.nio.channels.spi.AbstractSelectableChannel.isBlocking() , isRegistered sample code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() definition code for java.nio.channels.spi.AbstractSelectableChannel.isRegistered() , keyFor sample code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) definition code for java.nio.channels.spi.AbstractSelectableChannel.keyFor(java.nio.channels.Selector) , provider sample code for java.nio.channels.spi.AbstractSelectableChannel.provider() definition code for java.nio.channels.spi.AbstractSelectableChannel.provider() , register sample code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object)
 
Methods inherited from class java.nio.channels.SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
register sample code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int) definition code for java.nio.channels.SelectableChannel.register(java.nio.channels.Selector, int)
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel sample code for java.nio.channels.spi.AbstractInterruptibleChannel definition code for java.nio.channels.spi.AbstractInterruptibleChannel
begin sample code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.begin() , close sample code for java.nio.channels.spi.AbstractInterruptibleChannel.close() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.close() , end sample code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) definition code for java.nio.channels.spi.AbstractInterruptibleChannel.end(boolean) , isOpen sample code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen() definition code for java.nio.channels.spi.AbstractInterruptibleChannel.isOpen()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 
Methods inherited from interface java.nio.channels.Channel sample code for java.nio.channels.Channel definition code for java.nio.channels.Channel
close sample code for java.nio.channels.Channel.close() definition code for java.nio.channels.Channel.close() , isOpen sample code for java.nio.channels.Channel.isOpen() definition code for java.nio.channels.Channel.isOpen()
 

Constructor Detail

DatagramChannel sample code for java.nio.channels.DatagramChannel(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.DatagramChannel(java.nio.channels.spi.SelectorProvider)

protected DatagramChannel(SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
Initializes a new instance of this class.

Method Detail

open sample code for java.nio.channels.DatagramChannel.open() definition code for java.nio.channels.DatagramChannel.open()

public static DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel  open()
                            throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Opens a datagram channel.

The new channel is created by invoking the openDatagramChannel sample code for java.nio.channels.spi.SelectorProvider.openDatagramChannel() definition code for java.nio.channels.spi.SelectorProvider.openDatagramChannel() method of the system-wide default SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider object. The channel will not be connected.

Returns:
A new datagram channel
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

validOps sample code for java.nio.channels.DatagramChannel.validOps() definition code for java.nio.channels.DatagramChannel.validOps()

public final int validOps()
Returns an operation set identifying this channel's supported operations.

Datagram channels support reading and writing, so this method returns (SelectionKey.OP_READ sample code for java.nio.channels.SelectionKey.OP_READ definition code for java.nio.channels.SelectionKey.OP_READ | SelectionKey.OP_WRITE sample code for java.nio.channels.SelectionKey.OP_WRITE definition code for java.nio.channels.SelectionKey.OP_WRITE ).

Specified by:
validOps sample code for java.nio.channels.SelectableChannel.validOps() definition code for java.nio.channels.SelectableChannel.validOps() in class SelectableChannel sample code for java.nio.channels.SelectableChannel definition code for java.nio.channels.SelectableChannel
Returns:
The valid-operation set

socket sample code for java.nio.channels.DatagramChannel.socket() definition code for java.nio.channels.DatagramChannel.socket()

public abstract DatagramSocket sample code for java.net.DatagramSocket definition code for 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 DatagramSocket sample code for java.net.DatagramSocket definition code for java.net.DatagramSocket class.

Returns:
A datagram socket associated with this channel

isConnected sample code for java.nio.channels.DatagramChannel.isConnected() definition code for java.nio.channels.DatagramChannel.isConnected()

public abstract boolean isConnected()
Tells whether or not this channel's socket is connected.

Returns:
true if, and only if, this channel's socket is connected

connect sample code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress)

public abstract DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel  connect(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  remote)
                                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 connect sample code for java.net.DatagramSocket.connect(java.net.InetAddress, int) definition code for java.net.DatagramSocket.connect(java.net.InetAddress, int) method of the DatagramSocket sample code for java.net.DatagramSocket definition code for java.net.DatagramSocket class. That is, if a security manager has been installed then this method verifies that its checkAccept sample code for java.lang.SecurityManager.checkAccept(java.lang.String, int) definition code for java.lang.SecurityManager.checkAccept(java.lang.String, int) and checkConnect sample code for java.lang.SecurityManager.checkConnect(java.lang.String, int) definition code for java.lang.SecurityManager.checkConnect(java.lang.String, int) 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.

Parameters:
remote - The remote address to which this channel is to be connected
Returns:
This datagram channel
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the connect operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.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
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit access to the given remote address
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

disconnect sample code for java.nio.channels.DatagramChannel.disconnect() definition code for java.nio.channels.DatagramChannel.disconnect()

public abstract DatagramChannel sample code for java.nio.channels.DatagramChannel definition code for java.nio.channels.DatagramChannel  disconnect()
                                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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.

Returns:
This datagram channel
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

receive sample code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.receive(java.nio.ByteBuffer)

public abstract SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  receive(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
                               throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 read sample code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) 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 receive sample code for java.net.DatagramSocket.receive(java.net.DatagramPacket) definition code for java.net.DatagramSocket.receive(java.net.DatagramPacket) method of the DatagramSocket sample code for java.net.DatagramSocket definition code for 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 checkAccept sample code for java.lang.SecurityManager.checkAccept(java.lang.String, int) definition code for java.lang.SecurityManager.checkAccept(java.lang.String, int) method. The overhead of this security check can be avoided by first connecting the socket via the connect sample code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) 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.

Parameters:
dst - The buffer into which the datagram is to be transferred
Returns:
The datagram's source address, or null if this channel is in non-blocking mode and no datagram was immediately available
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit datagrams to be accepted from the datagram's sender
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

send sample code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.send(java.nio.ByteBuffer, java.net.SocketAddress)

public abstract int send(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src,
                         SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  target)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 write sample code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) operation.

This method performs exactly the same security checks as the send sample code for java.net.DatagramSocket.send(java.net.DatagramPacket) definition code for java.net.DatagramSocket.send(java.net.DatagramPacket) method of the DatagramSocket sample code for java.net.DatagramSocket definition code for 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 checkConnect sample code for java.lang.SecurityManager.checkConnect(java.lang.String, int) definition code for java.lang.SecurityManager.checkConnect(java.lang.String, int) method. The overhead of this security check can be avoided by first connecting the socket via the connect sample code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) definition code for java.nio.channels.DatagramChannel.connect(java.net.SocketAddress) 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.

Parameters:
src - The buffer containing the datagram to be sent
target - The address to which the datagram is to be sent
Returns:
The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws:
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager has been installed and it does not permit datagrams to be sent to the given address
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer)

public abstract int read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  dst)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel interface.

Specified by:
read sample code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) definition code for java.nio.channels.ReadableByteChannel.read(java.nio.ByteBuffer) in interface ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel
Parameters:
dst - The buffer into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[], int, int)

public abstract long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts,
                          int offset,
                          int length)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel interface.

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[], int, int) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
offset - The offset within the buffer array of the first buffer into which bytes are to be transferred; must be non-negative and no larger than dsts.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than dsts.length - offset
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

read sample code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.DatagramChannel.read(java.nio.ByteBuffer[])

public final long read(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] dsts)
                throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel interface.

Specified by:
read sample code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) definition code for java.nio.channels.ScatteringByteChannel.read(java.nio.ByteBuffer[]) in interface ScatteringByteChannel sample code for java.nio.channels.ScatteringByteChannel definition code for java.nio.channels.ScatteringByteChannel
Parameters:
dsts - The buffers into which bytes are to be transferred
Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer)

public abstract int write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer  src)
                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel interface.

Specified by:
write sample code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) definition code for java.nio.channels.WritableByteChannel.write(java.nio.ByteBuffer) in interface WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel
Parameters:
src - The buffer from which bytes are to be retrieved
Returns:
The number of bytes written, possibly zero
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[], int, int)

public abstract long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs,
                           int offset,
                           int length)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel interface.

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[], int, int) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
offset - The offset within the buffer array of the first buffer from which bytes are to be retrieved; must be non-negative and no larger than srcs.length
length - The maximum number of buffers to be accessed; must be non-negative and no larger than srcs.length - offset
Returns:
The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs

write sample code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.DatagramChannel.write(java.nio.ByteBuffer[])

public final long write(ByteBuffer sample code for java.nio.ByteBuffer definition code for java.nio.ByteBuffer [] srcs)
                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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 GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel interface.

Specified by:
write sample code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) definition code for java.nio.channels.GatheringByteChannel.write(java.nio.ByteBuffer[]) in interface GatheringByteChannel sample code for java.nio.channels.GatheringByteChannel definition code for java.nio.channels.GatheringByteChannel
Parameters:
srcs - The buffers from which bytes are to be retrieved
Returns:
The number of bytes sent, which will be either the number of bytes that were remaining in the source buffer when this method was invoked or, if this channel is non-blocking, may be zero if there was insufficient room for the datagram in the underlying output buffer
Throws:
NotYetConnectedException sample code for java.nio.channels.NotYetConnectedException definition code for java.nio.channels.NotYetConnectedException - If this channel's socket is not connected
ClosedChannelException sample code for java.nio.channels.ClosedChannelException definition code for java.nio.channels.ClosedChannelException - If this channel is closed
AsynchronousCloseException sample code for java.nio.channels.AsynchronousCloseException definition code for java.nio.channels.AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException sample code for java.nio.channels.ClosedByInterruptException definition code for java.nio.channels.ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException sample code for java.io.IOException definition code for java.io.IOException - If some other I/O error occurs