|
NIO2 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.classpath.icedtea.java.nio.channels.AsynchronousDatagramChannel
public abstract class AsynchronousDatagramChannel
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. An asynchronous datagram channel to an Internet Protocol
(IP) socket supports the following options:
Additional (implementation specific) options may also be supported.
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
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(SocketAddress sa, ByteBuffer buffer) { try { System.out.println(sa); buffer.clear(); dc.receive(buffer, buffer, this); } catch (...) { ... } } public void failed(Throwable exc, ByteBuffer buffer) { ... } public void cancelled(ByteBuffer buffer) { ... } });
Constructor Summary | |
---|---|
protected |
AsynchronousDatagramChannel(AsynchronousChannelProvider provider)
Initializes a new instance of this class. |
Method Summary | ||
---|---|---|
abstract AsynchronousDatagramChannel |
bind(java.net.SocketAddress local)
Binds the channel's socket to a local address. |
|
abstract AsynchronousDatagramChannel |
connect(java.net.SocketAddress remote)
Connects this channel's socket. |
|
abstract AsynchronousDatagramChannel |
disconnect()
Disconnects this channel's socket. |
|
abstract java.net.SocketAddress |
getRemoteAddress()
Returns the remote address to which this channel is connected. |
|
static AsynchronousDatagramChannel |
open()
Opens an asynchronous datagram channel. |
|
static AsynchronousDatagramChannel |
open(ProtocolFamily family,
AsynchronousChannelGroup group)
Opens an asynchronous datagram channel. |
|
AsynchronousChannelProvider |
provider()
Returns the provider that created this channel. |
|
java.util.concurrent.Future<java.lang.Integer> |
read(java.nio.ByteBuffer dst)
Reads a sequence of bytes from this channel into the given buffer. |
|
|
read(java.nio.ByteBuffer dst,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Reads a sequence of bytes from this channel into the given buffer. |
|
abstract
|
read(java.nio.ByteBuffer dst,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Receives a datagram via this channel. |
|
|
receive(java.nio.ByteBuffer dst)
Receives a datagram via this channel. |
|
|
receive(java.nio.ByteBuffer dst,
A attachment,
CompletionHandler<java.net.SocketAddress,? super A> handler)
Receives a datagram via this channel. |
|
abstract
|
receive(java.nio.ByteBuffer dst,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.net.SocketAddress,? super A> handler)
Receives a datagram via this channel. |
|
java.util.concurrent.Future<java.lang.Integer> |
send(java.nio.ByteBuffer src,
java.net.SocketAddress target)
Sends a datagram via this channel. |
|
|
send(java.nio.ByteBuffer src,
java.net.SocketAddress target,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Sends a datagram via this channel. |
|
abstract
|
send(java.nio.ByteBuffer src,
java.net.SocketAddress target,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Sends a datagram via this channel. |
|
abstract
|
setOption(SocketOption<T> name,
T value)
Sets the value of a socket option. |
|
java.util.concurrent.Future<java.lang.Integer> |
write(java.nio.ByteBuffer src)
Writes a sequence of bytes to this channel from the given buffer. |
|
|
write(java.nio.ByteBuffer src,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Writes a sequence of bytes to this channel from the given buffer. |
|
abstract
|
write(java.nio.ByteBuffer src,
long timeout,
java.util.concurrent.TimeUnit unit,
A attachment,
CompletionHandler<java.lang.Integer,? super A> handler)
Writes a datagram to this channel. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface org.classpath.icedtea.java.nio.channels.AsynchronousChannel |
---|
close |
Methods inherited from interface org.classpath.icedtea.java.nio.channels.MulticastChannel |
---|
join, join |
Methods inherited from interface org.classpath.icedtea.java.nio.channels.NetworkChannel |
---|
getLocalAddress, getOption, supportedOptions |
Methods inherited from interface java.nio.channels.Channel |
---|
isOpen |
Constructor Detail |
---|
protected AsynchronousDatagramChannel(AsynchronousChannelProvider provider)
Method Detail |
---|
public final AsynchronousChannelProvider provider()
public static AsynchronousDatagramChannel open(ProtocolFamily family, AsynchronousChannelGroup group) throws java.io.IOException
The new channel is created by invoking the openAsynchronousDatagramChannel
method on the java.nio.channels.spi.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 family 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.
family
- The protocol family, or null to use the default protocol
familygroup
- The group to which the newly constructed channel should be bound,
or null for the default group
java.lang.UnsupportedOperationException
- If the specified protocol family is not supported. For example,
suppose the parameter is specified as INET6
but IPv6 is not
enabled on the platform.
ShutdownChannelGroupException
- The specified group is shutdown
java.io.IOException
- If an I/O error occurspublic static AsynchronousDatagramChannel open() throws java.io.IOException
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);
java.io.IOException
- If an I/O error occurspublic abstract AsynchronousDatagramChannel bind(java.net.SocketAddress local) throws java.io.IOException
NetworkChannel
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. If the local
parameter has the
value null
then the socket will be bound to an address that is
assigned automatically.
bind
in interface NetworkChannel
local
- The address to bind the socket, or null
to bind the socket
to an automatically assigned socket address
AlreadyBoundException
- If the socket is already bound
UnsupportedAddressTypeException
- If the type of the given address is not supported
ClosedChannelException
- If the channel is closed
java.io.IOException
- If some other I/O error occurs
java.lang.SecurityException
- If a security manager has been installed and its checkListen
method denies the
operationNetworkChannel.getLocalAddress()
public abstract <T> AsynchronousDatagramChannel setOption(SocketOption<T> name, T value) throws java.io.IOException
NetworkChannel
setOption
in interface NetworkChannel
name
- The socket optionvalue
- The value of the socket option. A value of null
may be
a valid value for some socket options.
java.lang.IllegalArgumentException
- If the value is not a valid value for this socket option
ClosedChannelException
- If this channel is closed
java.io.IOException
- If an I/O error occursjava.net.StandardSocketOption
public abstract java.net.SocketAddress getRemoteAddress() throws java.io.IOException
Where the channel is connected to an Internet Protocol socket address
then the return value from this method is of type InetSocketAddress
.
null
if the channel's socket is not
connected
ClosedChannelException
- If the channel is closed
java.io.IOException
- If an I/O error occurspublic abstract AsynchronousDatagramChannel connect(java.net.SocketAddress remote) throws java.io.IOException
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
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. Whether it has any effect on outstanding read or write operations is implementation specific and therefore not specified.
remote
- The remote address to which this channel is to be connected
ClosedChannelException
- If this channel is closed
java.lang.SecurityException
- If a security manager has been installed
and it does not permit access to the given remote address
java.io.IOException
- If some other I/O error occurspublic abstract AsynchronousDatagramChannel disconnect() throws java.io.IOException
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.
This method may be invoked at any time. Whether it has any effect on outstanding read or write operations is implementation specific and therefore not specified.
java.io.IOException
- If some other I/O error occurspublic abstract <A> java.util.concurrent.Future<java.net.SocketAddress> receive(java.nio.ByteBuffer dst, long timeout, java.util.concurrent.TimeUnit unit, A attachment, CompletionHandler<java.net.SocketAddress,? super A> handler)
This method initiates the receiving of a datagram, returning a
Future representing the pending result of the operation.
The Future'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.
If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception InterruptedByTimeoutException
.
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 with a SecurityException
.
The overhead of this security check can be avoided by first connecting the
socket via the connect
method.
dst
- The buffer into which the datagram is to be transferredtimeout
- The timeout, or 0L for no timeoutunit
- The time unit of the timeout argumentattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
java.lang.IllegalArgumentException
- If the timeout is negative or the buffer is read-only
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final <A> java.util.concurrent.Future<java.net.SocketAddress> receive(java.nio.ByteBuffer dst, A attachment, CompletionHandler<java.net.SocketAddress,? super A> handler)
This method initiates the receiving of a datagram, returning a
Future representing the pending result of the operation.
The Future'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.
dst
- The buffer into which the datagram is to be transferredattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
java.lang.IllegalArgumentException
- If the buffer is read-only
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final <A> java.util.concurrent.Future<java.net.SocketAddress> receive(java.nio.ByteBuffer dst)
This method initiates the receiving of a datagram, returning a
Future representing the pending result of the operation.
The Future'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 and completion handler
of null
.
dst
- The buffer into which the datagram is to be transferred
java.lang.IllegalArgumentException
- If the buffer is read-onlypublic abstract <A> java.util.concurrent.Future<java.lang.Integer> send(java.nio.ByteBuffer src, java.net.SocketAddress target, long timeout, java.util.concurrent.TimeUnit unit, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates sending of a datagram, returning a
Future 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 Future's get
method, is the number of bytes sent.
The datagram is transferred from the byte buffer as if by a regular
write
operation.
If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception InterruptedByTimeoutException
.
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.
src
- The buffer containing the datagram to be senttarget
- The address to which the datagram is to be senttimeout
- The timeout, or 0L for no timeoutunit
- The time unit of the timeout argumentattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
UnresolvedAddressException
- If the given remote address is not fully resolved
UnsupportedAddressTypeException
- If the type of the given remote address is not supported
java.lang.IllegalArgumentException
- If the channel's socket is connected and is connected to an
address that is not equal to target
java.lang.SecurityException
- If a security manager has been installed and it does not permit
datagrams to be sent to the given address
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final <A> java.util.concurrent.Future<java.lang.Integer> send(java.nio.ByteBuffer src, java.net.SocketAddress target, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates sending of a datagram, returning a
Future 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 Future's get
method, is the number of bytes sent.
This method is equivalent to invoking send(ByteBuffer,SocketAddress,long,TimeUnit,Object,CompletionHandler)
with a timeout of 0L.
src
- The buffer containing the datagram to be senttarget
- The address to which the datagram is to be sentattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
UnresolvedAddressException
- If the given remote address is not fully resolved
UnsupportedAddressTypeException
- If the type of the given remote address is not supported
java.lang.IllegalArgumentException
- If the channel's socket is connected and is connected to an
address that is not equal to target
java.lang.SecurityException
- If a security manager has been installed and it does not permit
datagrams to be sent to the given address
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final java.util.concurrent.Future<java.lang.Integer> send(java.nio.ByteBuffer src, java.net.SocketAddress target)
This method initiates sending of a datagram, returning a
Future 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 Future's get
method, is the number of bytes sent.
This method is equivalent to invoking send(ByteBuffer,SocketAddress,long,TimeUnit,Object,CompletionHandler)
with a timeout of 0L
and an attachment and completion handler
of null
.
src
- The buffer containing the datagram to be senttarget
- The address to which the datagram is to be sent
UnresolvedAddressException
- If the given remote address is not fully resolved
UnsupportedAddressTypeException
- If the type of the given remote address is not supported
java.lang.IllegalArgumentException
- If the channel's socket is connected and is connected to an
address that is not equal to target
java.lang.SecurityException
- If a security manager has been installed and it does not permit
datagrams to be sent to the given addresspublic abstract <A> java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst, long timeout, java.util.concurrent.TimeUnit unit, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates the receiving of a datagram, returning a
Future representing the pending result of the operation.
The Future's get
method returns
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.
If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception InterruptedByTimeoutException
.
dst
- The buffer into which the datagram is to be transferredtimeout
- The timeout, or 0L for no timeoutunit
- The time unit of the timeout argumentattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
java.lang.IllegalArgumentException
- If the timeout is negative or buffer is read-only
NotYetConnectedException
- If this channel is not connected
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final <A> java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
AsynchronousByteChannel
This method initiates an operation to read a sequence of bytes from
this channel into the given buffer. The method returns a Future
representing the pending result of the operation. The result of the
operation, obtained by invoking the Future
's get
method, is the number of bytes read or -1
if
all bytes have been read and the channel has reached end-of-stream.
This method initiates a read operation to read up to r bytes
from the channel, where r is the number of bytes remaining in the
buffer, that is, dst.remaining()
at the time that the read is
attempted. Where r is 0, the read operation completes immediately
with a result of 0
without initiating an I/O operation.
Suppose that a byte sequence of length n is read, where 0 < n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment the read is performed. Upon completion the buffer's position will be equal to p + n; its limit will not have changed.
Buffers are not safe for use by multiple concurrent threads so care should be taken to not to access the buffer until the operaton has completed.
This method may be invoked at any time. Some channel types may not
allow more than one read to be outstanding at any given time. If a thread
initiates a read operation before a previous read operation has
completed then a ReadPendingException
will be thrown.
The handler parameter is used to specify a CompletionHandler
. When the read operation completes the handler's
completed
method is executed.
read
in interface AsynchronousByteChannel
dst
- The buffer into which bytes are to be transferredattachment
- The object to attach to the I/O operation; can be null
handler
- The completion handler object; can be null
NotYetConnectedException
- If this channel is not connected
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final java.util.concurrent.Future<java.lang.Integer> read(java.nio.ByteBuffer dst)
AsynchronousByteChannel
An invocation of this method of the form c.read(dst) behaves in exactly the same manner as the invocation
c.read(dst, null, null);
read
in interface AsynchronousByteChannel
dst
- The buffer into which bytes are to be transferred
NotYetConnectedException
- If this channel is not connectedpublic abstract <A> java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src, long timeout, java.util.concurrent.TimeUnit unit, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
This method initiates sending of a datagram, returning a
Future 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
Future's get
method, is the
number of bytes sent.
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.
If a timeout is specified and the timeout elapses before the operation
completes then the operation completes with the exception InterruptedByTimeoutException
.
src
- The buffer containing the datagram to be senttimeout
- The timeout, or 0L for no timeoutunit
- The time unit of the timeout argumentattachment
- The object to attach to the I/O operation; can be null
handler
- The handler for consuming the result; can be null
java.lang.IllegalArgumentException
- If the timeout is negative
NotYetConnectedException
- If this channel is not connected
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final <A> java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src, A attachment, CompletionHandler<java.lang.Integer,? super A> handler)
AsynchronousByteChannel
This method initiates an operation to write a sequence of bytes to
this channel from the given buffer. This method returns a Future
representing the pending result of the operation. The result
of the operation, obtained by invoking the Future's get
method, is the number of bytes written, possibly zero.
This method initiates a write operation to write up to r bytes
to the channel, where r is the number of bytes remaining in the
buffer, that is, src.remaining()
at the moment the write is
attempted. Where r is 0, the write operation completes immediately
with a result of 0
without initiating an I/O operation.
Suppose that a byte sequence of length n is written, where 0 < n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer's position at the moment the write is performed; the index of the last byte written will be p + n - 1. Upon completion the buffer's position will be equal to p + n; its limit will not have changed.
Buffers are not safe for use by multiple concurrent threads so care should be taken to not to access the buffer until the operaton has completed.
This method may be invoked at any time. Some channel types may not
allow more than one write to be outstanding at any given time. If a thread
initiates a write operation before a previous write operation has
completed then a WritePendingException
will be thrown.
The handler parameter is used to specify a CompletionHandler
. When the write operation completes the handler's
completed
method is executed.
write
in interface AsynchronousByteChannel
src
- The buffer from which bytes are to be retrievedattachment
- The object to attach to the I/O operation; can be null
handler
- The completion handler object; can be null
NotYetConnectedException
- If this channel is not connected
ShutdownChannelGroupException
- If a handler is specified, and the channel group is shutdownpublic final java.util.concurrent.Future<java.lang.Integer> write(java.nio.ByteBuffer src)
AsynchronousByteChannel
An invocation of this method of the form c.write(src) behaves in exactly the same manner as the invocation
c.write(src, null, null);
write
in interface AsynchronousByteChannel
src
- The buffer from which bytes are to be retrieved
NotYetConnectedException
- If this channel is not connected
|
NIO2 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 2010 Sun Microsystems, Inc. All rights reserved. Use is subject to the terms of the GNU General Public License.