📄 datagramchannel.java
字号:
* then for each datagram received this method verifies that the source's * address and port number are permitted by the security manager's {@link * java.lang.SecurityManager#checkAccept checkAccept} method. The overhead * of this security check can be avoided by first connecting the socket via * the {@link #connect connect} method. * * <p> 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. </p> * * @param dst * The buffer into which the datagram is to be transferred * * @return The datagram's source address, * or <tt>null</tt> if this channel is in non-blocking mode * and no datagram was immediately available * * @throws ClosedChannelException * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel * while the read operation is in progress * * @throws 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 * * @throws SecurityException * If a security manager has been installed * and it does not permit datagrams to be accepted * from the datagram's sender * * @throws IOException * If some other I/O error occurs */ public abstract SocketAddress receive(ByteBuffer dst) throws IOException; /** * Sends a datagram via this channel. * * <p> 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. * * <p> The datagram is transferred from the byte buffer as if by a regular * {@link WritableByteChannel#write(java.nio.ByteBuffer) write} operation. * * <p> This method performs exactly the same security checks as the {@link * java.net.DatagramSocket#send send} method of the {@link * 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 {@link * java.lang.SecurityManager#checkConnect checkConnect} method. The * overhead of this security check can be avoided by first connecting the * socket via the {@link #connect connect} method. * * <p> 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. </p> * * @param src * The buffer containing the datagram to be sent * * @param target * The address to which the datagram is to be sent * * @return 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 * If this channel is closed * * @throws AsynchronousCloseException * If another thread closes this channel * while the read operation is in progress * * @throws 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 * * @throws SecurityException * If a security manager has been installed * and it does not permit datagrams to be sent * to the given address * * @throws IOException * If some other I/O error occurs */ public abstract int send(ByteBuffer src, SocketAddress target) throws IOException; // -- ByteChannel operations -- /** * Reads a datagram from this channel. * * <p> 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 {@link * ReadableByteChannel} interface. </p> * * @throws NotYetConnectedException * If this channel's socket is not connected */ public abstract int read(ByteBuffer dst) throws IOException; /** * Reads a datagram from this channel. * * <p> 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 {@link * ScatteringByteChannel} interface. </p> * * @throws NotYetConnectedException * If this channel's socket is not connected */ public abstract long read(ByteBuffer[] dsts, int offset, int length) throws IOException; /** * Reads a datagram from this channel. * * <p> 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 {@link * ScatteringByteChannel} interface. </p> * * @throws NotYetConnectedException * If this channel's socket is not connected */ public final long read(ByteBuffer[] dsts) throws IOException { return read(dsts, 0, dsts.length); } /** * Writes a datagram to this channel. * * <p> 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 {@link * WritableByteChannel} interface. </p> * * @throws NotYetConnectedException * If this channel's socket is not connected */ public abstract int write(ByteBuffer src) throws IOException; /** * Writes a datagram to this channel. * * <p> 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 {@link * GatheringByteChannel} interface. </p> * * @return 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 * If this channel's socket is not connected */ public abstract long write(ByteBuffer[] srcs, int offset, int length) throws IOException; /** * Writes a datagram to this channel. * * <p> 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 {@link * GatheringByteChannel} interface. </p> * * @return 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 * If this channel's socket is not connected */ public final long write(ByteBuffer[] srcs) throws IOException { return write(srcs, 0, srcs.length); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -