|
Java™ Platform Standard Ed. 6 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.nio.channels.spi.AbstractInterruptibleChannel java.nio.channels.SelectableChannel java.nio.channels.spi.AbstractSelectableChannel java.nio.channels.DatagramChannel
public abstract class DatagramChannel
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
object obtained by
invoking the 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
object to be used by a
datagram socket associated with a datagram channel.
A datagram channel is created by invoking the 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
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 are otherwise performed as part of
every send and receive operation. A datagram 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, 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
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.
Constructor Summary | |
---|---|
protected |
DatagramChannel(SelectorProvider provider)
Initializes a new instance of this class. |
Method Summary | |
---|---|
abstract DatagramChannel |
connect(SocketAddress remote)
Connects this channel's socket. |
abstract DatagramChannel |
disconnect()
Disconnects this channel's socket. |
abstract boolean |
isConnected()
Tells whether or not this channel's socket is connected. |
static DatagramChannel |
open()
Opens a datagram channel. |
abstract int |
read(ByteBuffer dst)
Reads a datagram from this channel. |
long |
read(ByteBuffer[] dsts)
Reads a datagram from this channel. |
abstract long |
read(ByteBuffer[] dsts,
int offset,
int length)
Reads a datagram from this channel. |
abstract SocketAddress |
receive(ByteBuffer dst)
Receives a datagram via this channel. |
abstract int |
send(ByteBuffer src,
SocketAddress target)
Sends a datagram via this channel. |
abstract DatagramSocket |
socket()
Retrieves a datagram socket associated with this channel. |
int |
validOps()
Returns an operation set identifying this channel's supported operations. |
abstract int |
write(ByteBuffer src)
Writes a datagram to this channel. |
long |
write(ByteBuffer[] srcs)
Writes a datagram to this channel. |
abstract long |
write(ByteBuffer[] srcs,
int offset,
int length)
Writes a datagram to this channel. |
Methods inherited from class java.nio.channels.spi.AbstractSelectableChannel |
---|
blockingLock, configureBlocking, implCloseChannel, implCloseSelectableChannel, implConfigureBlocking, isBlocking, isRegistered, keyFor, provider, register |
Methods inherited from class java.nio.channels.SelectableChannel |
---|
register |
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel |
---|
begin, close, end, isOpen |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.nio.channels.Channel |
---|
close, isOpen |
Constructor Detail |
---|
protected DatagramChannel(SelectorProvider provider)
Method Detail |
---|
public static DatagramChannel open() throws IOException
The new channel is created by invoking the openDatagramChannel
method of the system-wide default SelectorProvider
object. The channel will not be
connected.
IOException
- If an I/O error occurspublic final int validOps()
Datagram channels support reading and writing, so this method
returns (SelectionKey.OP_READ
| SelectionKey.OP_WRITE
).
validOps
in class SelectableChannel
public abstract DatagramSocket socket()
The returned object will not declare any public methods that are not
declared in the DatagramSocket
class.
public abstract boolean isConnected()
public abstract DatagramChannel connect(SocketAddress remote) throws 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. It will not have any effect on read or write operations that are already in progress at the moment that it is invoked.
remote
- The remote address to which this channel is to be connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the connect operation is in progress
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
- If a security manager has been installed
and it does not permit access to the given remote address
IOException
- If some other I/O error occurspublic abstract DatagramChannel disconnect() throws 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.
If this channel's socket is not connected, or if the channel is closed, then invoking this method has no effect.
IOException
- If some other I/O error occurspublic abstract SocketAddress receive(ByteBuffer dst) throws IOException
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
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
method of the 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
method. The overhead
of this security check can be avoided by first connecting the socket via
the connect
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.
dst
- The buffer into which the datagram is to be transferred
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
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
- If a security manager has been installed
and it does not permit datagrams to be accepted
from the datagram's sender
IOException
- If some other I/O error occurspublic abstract int send(ByteBuffer src, SocketAddress target) throws IOException
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
operation.
This method performs exactly the same security checks as the send
method of the 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
method. The
overhead of this security check can be avoided by first connecting the
socket via the connect
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.
src
- The buffer containing the datagram to be senttarget
- The address to which the datagram is to be sent
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
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
- If a security manager has been installed
and it does not permit datagrams to be sent
to the given address
IOException
- If some other I/O error occurspublic abstract int read(ByteBuffer dst) throws IOException
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
interface.
read
in interface ReadableByteChannel
dst
- The buffer into which bytes are to be transferred
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
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
- If some other I/O error occurspublic abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException
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
interface.
read
in interface ScatteringByteChannel
dsts
- The buffers into which bytes are to be transferredoffset
- 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.lengthlength
- The maximum number of buffers to be accessed; must be
non-negative and no larger than
dsts.length - offset
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
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
- If some other I/O error occurspublic final long read(ByteBuffer[] dsts) throws IOException
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
interface.
read
in interface ScatteringByteChannel
dsts
- The buffers into which bytes are to be transferred
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the read operation is in progress
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
- If some other I/O error occurspublic abstract int write(ByteBuffer src) throws IOException
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
interface.
write
in interface WritableByteChannel
src
- The buffer from which bytes are to be retrieved
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the write operation is in progress
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
- If some other I/O error occurspublic abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException
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
interface.
write
in interface GatheringByteChannel
srcs
- The buffers from which bytes are to be retrievedoffset
- 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.lengthlength
- The maximum number of buffers to be accessed; must be
non-negative and no larger than
srcs.length - offset
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the write operation is in progress
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
- If some other I/O error occurspublic final long write(ByteBuffer[] srcs) throws IOException
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
interface.
write
in interface GatheringByteChannel
srcs
- The buffers from which bytes are to be retrieved
NotYetConnectedException
- If this channel's socket is not connected
ClosedChannelException
- If this channel is closed
AsynchronousCloseException
- If another thread closes this channel
while the write operation is in progress
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
- If some other I/O error occurs
|
Java™ Platform Standard Ed. 6 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.