com.sun.sgs.nio.channels
Class AsynchronousDatagramChannel

java.lang.Object
  extended by com.sun.sgs.nio.channels.AsynchronousChannel
      extended by com.sun.sgs.nio.channels.AsynchronousDatagramChannel
All Implemented Interfaces:
AsynchronousByteChannel, MulticastChannel, NetworkChannel, Closeable, Channel
Direct Known Subclasses:
AsyncDatagramChannelImpl

public abstract class AsynchronousDatagramChannel
extends AsynchronousChannel
implements AsynchronousByteChannel, NetworkChannel, MulticastChannel

An asynchronous channel for datagram-oriented sockets.

An asynchronous datagram channel is created by invoking one of the open methods defined by this class. It is not possible to create a channel for an arbitrary, pre-existing datagram socket. A newly-created asynchronous datagram channel is open but not connected. It need not be connected in order for the send and receive methods to be used. A datagram channel may be connected, by invoking its connect method, in order to avoid the overhead of the security checks that are otherwise performed as part of every send and receive operation when a security manager is set. The channel must be connected in order to use the read and write methods, since those methods do not accept or return socket addresses. Once connected, an asynchronous datagram channel remains connected until it is disconnected or closed.

Socket options are configured using the setOption method. Channels of this type support the following options:

Option Name Description
SO_SNDBUF The size of the socket send buffer
SO_RCVBUF The size of the socket receive buffer
SO_REUSEADDR Re-use address
SO_BROADCAST Allow transmission of broadcast datagrams
IP_TOS The Type of Service (ToS) octet in the Internet Protocol (IP) header
IP_MULTICAST_IF The network interface for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_TTL The time-to-live for Internet Protocol (IP) multicast datagrams
IP_MULTICAST_LOOP Loopback for Internet Protocol (IP) multicast datagrams
and may support additional (implementation specific) options. The list of options supported is obtained by invoking the options method.

Timeouts

[TBD]

Usage Example:

   final AsynchronousDatagramChannel dc = AsynchronousDatagramChannel.open()
       .bind(new InetSocketAddress(4000));
 
   // print the source address of all packets that we receive
   dc.receive(buffer, buffer, 
     new CompletionHandler<SocketAddress,ByteBuffer>() {
       public void completed(IoFuture<SocketAddress,ByteBuffer> result)
       {
           try {
                SocketAddress sa = result.getNow();
                System.out.println(sa);
 
                ByteBuffer buffer = result.attachment();
                buffer.clear();
                dc.receive(buffer, buffer, this); 
            } catch (ExecutionException x) { ... }
       }
   });
 


Constructor Summary
protected AsynchronousDatagramChannel(AsynchronousChannelProvider provider)
          Initializes a new instance of this class.
 
Method Summary
abstract  AsynchronousDatagramChannel bind(SocketAddress local)
          Binds the channel's socket to a local address.
abstract
<A> IoFuture<Void,A>
connect(SocketAddress remote, A attachment, CompletionHandler<Void,? super A> handler)
          Connects this channel.
<A> IoFuture<Void,A>
connect(SocketAddress remote, CompletionHandler<Void,? super A> handler)
          Connects this channel.
abstract
<A> IoFuture<Void,A>
disconnect(A attachment, CompletionHandler<Void,? super A> handler)
          Disconnects this channel.
<A> IoFuture<Void,A>
disconnect(CompletionHandler<Void,? super A> handler)
          Disconnects this channel.
abstract  SocketAddress getConnectedAddress()
          Returns the remote address to which this channel is connected, or null if the channel is not connected.
abstract  boolean isReadPending()
          Tells whether or not a read is pending for this channel.
abstract  boolean isWritePending()
          Tells whether or not a write is pending for this channel.
static AsynchronousDatagramChannel open()
          Opens an asynchronous datagram channel.
static AsynchronousDatagramChannel open(ProtocolFamily pf, AsynchronousChannelGroup group)
          Opens an asynchronous datagram channel.
<A> IoFuture<Integer,A>
read(ByteBuffer dst, A attachment, CompletionHandler<Integer,? super A> handler)
          Receives a datagram via this channel.
<A> IoFuture<Integer,A>
read(ByteBuffer dst, CompletionHandler<Integer,? super A> handler)
          Receives a datagram via this channel.
abstract
<A> IoFuture<Integer,A>
read(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
          Receives a datagram via this channel.
<A> IoFuture<SocketAddress,A>
receive(ByteBuffer dst, A attachment, CompletionHandler<SocketAddress,? super A> handler)
          Receives a datagram via this channel.
<A> IoFuture<SocketAddress,A>
receive(ByteBuffer dst, CompletionHandler<SocketAddress,? super A> handler)
          Receives a datagram via this channel.
abstract
<A> IoFuture<SocketAddress,A>
receive(ByteBuffer dst, long timeout, TimeUnit unit, A attachment, CompletionHandler<SocketAddress,? super A> handler)
          Receives a datagram via this channel.
<A> IoFuture<Integer,A>
send(ByteBuffer src, SocketAddress target, A attachment, CompletionHandler<Integer,? super A> handler)
          Sends a datagram via this channel.
<A> IoFuture<Integer,A>
send(ByteBuffer src, SocketAddress target, CompletionHandler<Integer,? super A> handler)
          Sends a datagram via this channel.
abstract
<A> IoFuture<Integer,A>
send(ByteBuffer src, SocketAddress target, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
          Sends a datagram via this channel.
abstract  AsynchronousDatagramChannel setOption(SocketOption name, Object value)
          Sets the value of a socket option.
<A> IoFuture<Integer,A>
write(ByteBuffer src, A attachment, CompletionHandler<Integer,? super A> handler)
          Writes a datagram to this channel.
<A> IoFuture<Integer,A>
write(ByteBuffer src, CompletionHandler<Integer,? super A> handler)
          Writes a datagram to this channel.
abstract
<A> IoFuture<Integer,A>
write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, CompletionHandler<Integer,? super A> handler)
          Writes a datagram to this channel.
 
Methods inherited from class com.sun.sgs.nio.channels.AsynchronousChannel
close, provider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.sgs.nio.channels.MulticastChannel
join, join
 
Methods inherited from interface com.sun.sgs.nio.channels.NetworkChannel
getLocalAddress, getOption, options
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Constructor Detail

AsynchronousDatagramChannel

protected AsynchronousDatagramChannel(AsynchronousChannelProvider provider)
Initializes a new instance of this class.

Parameters:
provider - the asynchronous channel provider for this channel
Method Detail

open

public static AsynchronousDatagramChannel open(ProtocolFamily pf,
                                               AsynchronousChannelGroup group)
                                        throws IOException
Opens an asynchronous datagram channel.

The new channel is created by invoking the openAsynchronousDatagramChannel method on the AsynchronousChannelProvider object that created the given group. If the group parameter is null then the resulting channel is created by the system-wide default provider, and bound to the default group.

The pf parameter is used to specify the ProtocolFamily. If the datagram channel is to be used for Internet Protocol multicasting then this parameter should correspond to the address type of the multicast groups that this channel will join.

Parameters:
pf - the protocol family, or null to use the default protocol family
group - the group to which the newly constructed channel should be bound, or null for the default group
Returns:
a new asynchronous datagram channel
Throws:
ShutdownChannelGroupException - if the specified group is shutdown
IOException - if an I/O error occurs

open

public static AsynchronousDatagramChannel open()
                                        throws IOException
Opens an asynchronous datagram channel.

This method returns an asynchronous datagram channel that is bound to the default group.This method is equivalent to evaluating the expression:

     open((ProtocolFamily)null, (AsynchronousChannelGroup)null);
 

Returns:
a new asynchronous datagram channel
Throws:
IOException - if an I/O error occurs

bind

public abstract AsynchronousDatagramChannel bind(SocketAddress local)
                                          throws IOException
Binds the channel's socket to a local address.

This method is used to establish an association between the socket and a local address. Once an association is established then the socket remains bound until the channel is closed. An attempt to bind a socket that is already bound throws AlreadyBoundException. If the local parameter has the value null then the socket will be bound to an address that is assigned automatically.

An implementation of this interface should specify if a permission is required when a security manager is installed.

Specified by:
bind in interface NetworkChannel
Parameters:
local - the address to bind the socket, or null to bind the socket to an automatically assigned socket address
Returns:
this channel
Throws:
SecurityException - if a security manager exists and its checkListen method doesn't allow the operation
ClosedChannelException - if the channel is closed
IOException - if some other I/O error occurs

setOption

public abstract AsynchronousDatagramChannel setOption(SocketOption name,
                                                      Object value)
                                               throws IOException
Sets the value of a socket option.

The name parameter is the name of the socket option. The value parameter is the value of the option and is of the type specified by the option. A value of null may be a valid value for some socket options.

Specified by:
setOption in interface NetworkChannel
Parameters:
name - the name of the socket option
value - the value of the socket option
Returns:
this channel
Throws:
ClosedChannelException - if this channel is closed
IOException - if an I/O error occurs
See Also:
SocketOption

getConnectedAddress

public abstract SocketAddress getConnectedAddress()
                                           throws IOException
Returns the remote address to which this channel is connected, or null if the channel is not connected.

Returns:
the remote address; null if the channel is not open or the channel's socket is not connected
Throws:
IOException - if an I/O error occurs

isReadPending

public abstract boolean isReadPending()
Tells whether or not a read is pending for this channel.

The result of this method is a snapshot of the channel state. It may be invalid when the caller goes to examine the result and should not be used for purposes of coordination.

Returns:
true, if and only if, a read is pending for this channel but has not yet completed.
See Also:
ReadPendingException

isWritePending

public abstract boolean isWritePending()
Tells whether or not a write is pending for this channel.

The result of this method is a snapshot of the channel state. It may be invalid when the caller goes to examine the result and should not be used for purposes of coordination.

Returns:
true, if and only if, a write is pending for this channel but has not yet completed.
See Also:
WritePendingException

connect

public abstract <A> IoFuture<Void,A> connect(SocketAddress remote,
                                             A attachment,
                                             CompletionHandler<Void,? super A> handler)
Connects this channel.

This method initiates an operation to configure the channel 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. The channel remains connected until it is disconnected or until it is closed. This method returns an IoFuture representing the pending result of the operation. The get method returns null upon successful completion.

This method performs exactly the same security checks as the connect method of the DatagramSocket class. That is, if a security manager has been installed then this method verifies that its checkAccept and checkConnect methods permit datagrams to be received from and sent to, respectively, the given remote address.

This method may be invoked at any time and may thus affect read or write operations that are already in progress at the moment that the socket is connected.

Type Parameters:
A - the attachment type
Parameters:
remote - the remote address to which this channel is to be connected
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ConnectionPendingException - if a connection operation is already in progress on this channel
UnresolvedAddressException - if the given remote address is not fully resolved
UnsupportedAddressTypeException - if the type of the given remote address is not supported
SecurityException - if a security manager has been installed and it does not permit access to the given remote address

connect

public final <A> IoFuture<Void,A> connect(SocketAddress remote,
                                          CompletionHandler<Void,? super A> handler)
Connects this channel.

This method initiates an operation to configure the channel 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. The channel remains connected until it is disconnected or until it is closed. This method returns an IoFuture representing the pending result of the operation. The get method returns null upon successful completion.

This method is equivalent to invoking connect(SocketAddress,Object,CompletionHandler) with an attachment of null.

Type Parameters:
A - the attachment type
Parameters:
remote - the remote address to which this channel is to be connected
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ConnectionPendingException - if a connection operation is already in progress on this channel
UnresolvedAddressException - if the given remote address is not fully resolved
UnsupportedAddressTypeException - if the type of the given remote address is not supported
SecurityException - if a security manager has been installed and it does not permit access to the given remote address

disconnect

public abstract <A> IoFuture<Void,A> disconnect(A attachment,
                                                CompletionHandler<Void,? super A> handler)
Disconnects this channel.

This method initiates an operation to configure the channel 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 returns an IoFuture representing the pending result of the operation. The get method returns null upon successful completion.

This method may be invoked at any time and may thus affect read or write operations that are already in progress at the moment that the socket is disconnected.

If this channel's is not connected then the operation completes succesfully (meaning that the completion handler is invoked, and the IoFuture's get method returns null).

Type Parameters:
A - the attachment type
Parameters:
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed

disconnect

public final <A> IoFuture<Void,A> disconnect(CompletionHandler<Void,? super A> handler)
Disconnects this channel.

This method initiates an operation to configure the channel 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 returns an IoFuture representing the pending result of the operation. The get method returns null upon successful completion.

This method is equivalent to invoking disconnect(Object,CompletionHandler) with an attachment of null.

Type Parameters:
A - the attachment type
Parameters:
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed

receive

public abstract <A> IoFuture<SocketAddress,A> receive(ByteBuffer dst,
                                                      long timeout,
                                                      TimeUnit unit,
                                                      A attachment,
                                                      CompletionHandler<SocketAddress,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the source address of the datagram upon successful completion.

The datagram is transferred into the given byte buffer starting at its current position, as if by a regular 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.

When a security manager has been installed and the channel is not connected, then it verifies that the source's address and port number are permitted by the security manager's checkAccept method. The permission check is performed with privileges that are restricted by the calling context of this method. If the permission check fails then the operation completes by throwing ExecutionException with cause SecurityException. The overhead of this security check can be avoided by first connecting the socket via the connect method.

Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel

receive

public final <A> IoFuture<SocketAddress,A> receive(ByteBuffer dst,
                                                   A attachment,
                                                   CompletionHandler<SocketAddress,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the source address of the datagram upon successful completion.

This method is equivalent to invoking receive(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L.

Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel

receive

public final <A> IoFuture<SocketAddress,A> receive(ByteBuffer dst,
                                                   CompletionHandler<SocketAddress,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the source address of the datagram upon successful completion.

This method is equivalent to invoking receive(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L, and an attachment of null.

Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel

send

public abstract <A> IoFuture<Integer,A> send(ByteBuffer src,
                                             SocketAddress target,
                                             long timeout,
                                             TimeUnit unit,
                                             A attachment,
                                             CompletionHandler<Integer,? super A> handler)
Sends a datagram via this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram to the given target address. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

The datagram is transferred from the byte buffer as if by a regular write operation.

If there is a security manager installed and the the channel is not connected then this method verifies that the target address and port number are permitted by the security manager's checkConnect method. The overhead of this security check can be avoided by first connecting the socket via the connect method.

Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
target - the address to which the datagram is to be sent
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a write operation is already in progress on this channel
UnresolvedAddressException - if the given remote address is not fully resolved
UnsupportedAddressTypeException - if the type of the given remote address is not supported
SecurityException - if a security manager has been installed and it does not permit datagrams to be sent to the given address

send

public final <A> IoFuture<Integer,A> send(ByteBuffer src,
                                          SocketAddress target,
                                          A attachment,
                                          CompletionHandler<Integer,? super A> handler)
Sends a datagram via this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram to the given target address. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

This method is equivalent to invoking send(ByteBuffer, SocketAddress, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L.

Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
target - the address to which the datagram is to be sent
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a write operation is already in progress on this channel
UnresolvedAddressException - if the given remote address is not fully resolved
UnsupportedAddressTypeException - if the type of the given remote address is not supported
SecurityException - if a security manager has been installed and it does not permit datagrams to be sent to the given address

send

public final <A> IoFuture<Integer,A> send(ByteBuffer src,
                                          SocketAddress target,
                                          CompletionHandler<Integer,? super A> handler)
Sends a datagram via this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram to the given target address. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

This method is equivalent to invoking send(ByteBuffer, SocketAddress, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L, and an attachment of null.

Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
target - the address to which the datagram is to be sent
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a write operation is already in progress on this channel
UnresolvedAddressException - if the given remote address is not fully resolved
UnsupportedAddressTypeException - if the type of the given remote address is not supported
SecurityException - if a security manager has been installed and it does not permit datagrams to be sent to the given address

read

public abstract <A> IoFuture<Integer,A> read(ByteBuffer dst,
                                             long timeout,
                                             TimeUnit unit,
                                             A attachment,
                                             CompletionHandler<Integer,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the the number of bytes transferred upon successful completion.

This method may only be invoked if this channel is connected, and it only accepts datagrams from the peer that the channel is connected too. The datagram is transferred into the given byte buffer starting at its current position and exactly as specified in the AsynchronousByteChannel interface. 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.

Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

read

public final <A> IoFuture<Integer,A> read(ByteBuffer dst,
                                          A attachment,
                                          CompletionHandler<Integer,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the the number of bytes transferred upon successful completion.

This method is equivalent to invoking read(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L.

Specified by:
read in interface AsynchronousByteChannel
Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

read

public final <A> IoFuture<Integer,A> read(ByteBuffer dst,
                                          CompletionHandler<Integer,? super A> handler)
Receives a datagram via this channel.

This method initiates the receiving of a datagram, returning an IoFuture representing the pending result of the operation. The IoFuture's get method returns the the number of bytes transferred upon successful completion.

This method is equivalent to invoking read(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L, and an attachment of null.

Specified by:
read in interface AsynchronousByteChannel
Type Parameters:
A - the attachment type
Parameters:
dst - the buffer into which the datagram is to be transferred
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
ReadPendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

write

public abstract <A> IoFuture<Integer,A> write(ByteBuffer src,
                                              long timeout,
                                              TimeUnit unit,
                                              A attachment,
                                              CompletionHandler<Integer,? super A> handler)
Writes a datagram to this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

The datagram is transferred from the byte buffer as if by a regular write operation.

This method may only be invoked if this channel is connected, in which case it sends datagrams directly to the socket's peer. Otherwise it behaves exactly as specified in the AsynchronousByteChannel interface.

Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
timeout - the timeout, or 0L for no timeout
unit - the time unit of the timeout argument
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

write

public final <A> IoFuture<Integer,A> write(ByteBuffer src,
                                           A attachment,
                                           CompletionHandler<Integer,? super A> handler)
Writes a datagram to this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

This method is equivalent to invoking write(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L.

Specified by:
write in interface AsynchronousByteChannel
Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
attachment - the object to attach to the returned IoFuture object; can be null
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

write

public final <A> IoFuture<Integer,A> write(ByteBuffer src,
                                           CompletionHandler<Integer,? super A> handler)
Writes a datagram to this channel.

This method initiates sending of a datagram, returning an IoFuture representing the pending result of the operation. The operation sends the remaining bytes in the given buffer as a single datagram. The result of the operation, obtained by invoking the IoFuture's get method, is the number of bytes sent. The number of bytes sent is either the number of bytes that were remaining in the buffer or zero if there was insufficient room for the datagram in the underlying output buffer.

This method is equivalent to invoking write(ByteBuffer, long, TimeUnit, Object, CompletionHandler) with a timeout of 0L, and an attachment of null.

Specified by:
write in interface AsynchronousByteChannel
Type Parameters:
A - the attachment type
Parameters:
src - the buffer containing the datagram to be sent
handler - the handler for consuming the result; can be null
Returns:
an IoFuture object representing the pending result
Throws:
ClosedAsynchronousChannelException - if this channel is closed
WritePendingException - if a read operation is already in progress on this channel
NotYetConnectedException - if this channel is not connected

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved