jdk/src/share/classes/java/nio/channels/DatagramChannel.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 1247 b4c26443dee5
child 2427 f35f516befc3
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    29
import java.net.ProtocolFamily;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.DatagramSocket;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    31
import java.net.SocketOption;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.SocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.ByteBuffer;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    34
import java.nio.channels.spi.AbstractSelectableChannel;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    35
import java.nio.channels.spi.SelectorProvider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A selectable channel for datagram-oriented sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    40
 * <p> A datagram channel is created by invoking one of the {@link #open open} methods
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    41
 * of this class. It is not possible to create a channel for an arbitrary,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    42
 * pre-existing datagram socket. A newly-created datagram channel is open but not
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    43
 * connected. A datagram channel need not be connected in order for the {@link #send
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    44
 * send} and {@link #receive receive} methods to be used.  A datagram channel may be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * connected, by invoking its {@link #connect connect} method, in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * avoid the overhead of the security checks are otherwise performed as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * every send and receive operation.  A datagram channel must be connected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * order to use the {@link #read(java.nio.ByteBuffer) read} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * #write(java.nio.ByteBuffer) write} methods, since those methods do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * accept or return socket addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> Once connected, a datagram channel remains connected until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * disconnected or closed.  Whether or not a datagram channel is connected may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * be determined by invoking its {@link #isConnected isConnected} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    56
 * <p> Socket options are configured using the {@link #setOption(SocketOption,Object)
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    57
 * setOption} method. A datagram channel to an Internet Protocol socket supports
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
    58
 * the following options:
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    59
 * <blockquote>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    60
 * <table border>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    61
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    62
 *     <th>Option Name</th>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    63
 *     <th>Description</th>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    64
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    65
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    66
 *     <td> {@link java.net.StandardSocketOption#SO_SNDBUF SO_SNDBUF} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    67
 *     <td> The size of the socket send buffer </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    68
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    69
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    70
 *     <td> {@link java.net.StandardSocketOption#SO_RCVBUF SO_RCVBUF} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    71
 *     <td> The size of the socket receive buffer </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    72
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    73
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    74
 *     <td> {@link java.net.StandardSocketOption#SO_REUSEADDR SO_REUSEADDR} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    75
 *     <td> Re-use address </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    76
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    77
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    78
 *     <td> {@link java.net.StandardSocketOption#SO_BROADCAST SO_BROADCAST} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    79
 *     <td> Allow transmission of broadcast datagrams </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    80
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    81
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    82
 *     <td> {@link java.net.StandardSocketOption#IP_TOS IP_TOS} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    83
 *     <td> The Type of Service (ToS) octet in the Internet Protocol (IP) header </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    84
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    85
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    86
 *     <td> {@link java.net.StandardSocketOption#IP_MULTICAST_IF IP_MULTICAST_IF} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    87
 *     <td> The network interface for Internet Protocol (IP) multicast datagrams </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    88
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    89
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    90
 *     <td> {@link java.net.StandardSocketOption#IP_MULTICAST_TTL
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    91
 *       IP_MULTICAST_TTL} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    92
 *     <td> The <em>time-to-live</em> for Internet Protocol (IP) multicast
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    93
 *       datagrams </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    94
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    95
 *   <tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    96
 *     <td> {@link java.net.StandardSocketOption#IP_MULTICAST_LOOP
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    97
 *       IP_MULTICAST_LOOP} </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    98
 *     <td> Loopback for Internet Protocol (IP) multicast datagrams </td>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
    99
 *   </tr>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   100
 * </table>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   101
 * </blockquote>
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   102
 * Additional (implementation specific) options may also be supported.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   103
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p> Datagram channels are safe for use by multiple concurrent threads.  They
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * support concurrent reading and writing, though at most one thread may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * reading and at most one thread may be writing at any given time.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
public abstract class DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    extends AbstractSelectableChannel
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   115
    implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, MulticastChannel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected DatagramChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        super(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Opens a datagram channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p> The new channel is created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * java.nio.channels.spi.SelectorProvider#openDatagramChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * openDatagramChannel} method of the system-wide default {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * java.nio.channels.spi.SelectorProvider} object.  The channel will not be
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   132
     * connected.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   133
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   134
     * <p> The {@link ProtocolFamily ProtocolFamily} of the channel's socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   135
     * is platform (and possibly configuration) dependent and therefore unspecified.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   136
     * The {@link #open(ProtocolFamily) open} allows the protocol family to be
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   137
     * selected when opening a datagram channel, and should be used to open
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   138
     * datagram channels that are intended for Internet Protocol multicasting.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return  A new datagram channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static DatagramChannel open() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return SelectorProvider.provider().openDatagramChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   150
     * Opens a datagram channel.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   151
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   152
     * <p> The {@code family} parameter is used to specify the {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   153
     * ProtocolFamily}. If the datagram channel is to be used for IP multicasing
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   154
     * then this should correspond to the address type of the multicast groups
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   155
     * that this channel will join.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   156
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   157
     * <p> The new channel is created by invoking the {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   158
     * java.nio.channels.spi.SelectorProvider#openDatagramChannel(ProtocolFamily)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   159
     * openDatagramChannel} method of the system-wide default {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   160
     * java.nio.channels.spi.SelectorProvider} object.  The channel will not be
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   161
     * connected.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   162
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   163
     * @param   family
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   164
     *          The protocol family
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   165
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   166
     * @return  A new datagram channel
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   167
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   168
     * @throws  UnsupportedOperationException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   169
     *          If the specified protocol family is not supported. For example,
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   170
     *          suppose the parameter is specified as {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   171
     *          java.net.StandardProtocolFamily#INET6 StandardProtocolFamily.INET6}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   172
     *          but IPv6 is not enabled on the platform.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   173
     * @throws  IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   174
     *          If an I/O error occurs
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   175
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   176
     * @since   1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   177
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   178
    public static DatagramChannel open(ProtocolFamily family) throws IOException {
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   179
        return SelectorProvider.provider().openDatagramChannel(family);
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   180
    }
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   181
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   182
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns an operation set identifying this channel's supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p> Datagram channels support reading and writing, so this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * returns <tt>(</tt>{@link SelectionKey#OP_READ} <tt>|</tt>&nbsp;{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * SelectionKey#OP_WRITE}<tt>)</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return  The valid-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public final int validOps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return (SelectionKey.OP_READ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                | SelectionKey.OP_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    // -- Socket-specific operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   201
     * @throws  AlreadyBoundException               {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   202
     * @throws  UnsupportedAddressTypeException     {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   203
     * @throws  ClosedChannelException              {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   204
     * @throws  IOException                         {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   205
     * @throws  SecurityException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   206
     *          If a security manager has been installed and its {@link
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   207
     *          SecurityManager#checkListen checkListen} method denies the
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   208
     *          operation
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   209
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   210
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   211
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   212
    public abstract DatagramChannel bind(SocketAddress local)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   213
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   214
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   215
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   216
     * @throws  UnsupportedOperationException           {@inheritDoc}
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   217
     * @throws  IllegalArgumentException                {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   218
     * @throws  ClosedChannelException                  {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   219
     * @throws  IOException                             {@inheritDoc}
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   220
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   221
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   222
     */
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   223
    public abstract <T> DatagramChannel setOption(SocketOption<T> name, T value)
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   224
        throws IOException;
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   225
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   226
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Retrieves a datagram socket associated with this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> The returned object will not declare any public methods that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * declared in the {@link java.net.DatagramSocket} class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return  A datagram socket associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public abstract DatagramSocket socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   237
     * Tells whether or not this channel's socket is connected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   239
     * @return  {@code true} if, and only if, this channel's socket
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   240
     *          is {@link #isOpen open} and connected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public abstract boolean isConnected();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Connects this channel's socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p> The channel's socket is configured so that it only receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * datagrams from, and sends datagrams to, the given remote <i>peer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * address.  Once connected, datagrams may not be received from or sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * any other address.  A datagram socket remains connected until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * explicitly disconnected or until it is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * java.net.DatagramSocket#connect connect} method of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * java.net.DatagramSocket} class.  That is, if a security manager has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * installed then this method verifies that its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * java.lang.SecurityManager#checkAccept checkAccept} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * java.lang.SecurityManager#checkConnect checkConnect} methods permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * datagrams to be received from and sent to, respectively, the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * remote address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p> This method may be invoked at any time.  It will not have any effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * on read or write operations that are already in progress at the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * that it is invoked.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param  remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         The remote address to which this channel is to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return  This datagram channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          while the connect operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          while the connect operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *          and it does not permit access to the given remote address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public abstract DatagramChannel connect(SocketAddress remote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Disconnects this channel's socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p> The channel's socket is configured so that it can receive datagrams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * from, and sends datagrams to, any remote address so long as the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * manager, if installed, permits it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p> This method may be invoked at any time.  It will not have any effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * on read or write operations that are already in progress at the moment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * that it is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p> If this channel's socket is not connected, or if the channel is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * closed, then invoking this method has no effect.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return  This datagram channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public abstract DatagramChannel disconnect() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   316
     * Returns the remote address to which this channel's socket is connected.
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   317
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   318
     * @return  The remote address; {@code null} if the channel's socket is not
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   319
     *          connected
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   320
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   321
     * @throws  ClosedChannelException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   322
     *          If the channel is closed
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   323
     * @throws  IOException
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   324
     *          If an I/O error occurs
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   325
     *
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   326
     * @since 1.7
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   327
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1247
diff changeset
   328
    public abstract SocketAddress getRemoteAddress() throws IOException;
1152
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   329
29d6145d1097 4640544: New I/O: Complete socket-channel functionality
alanb
parents: 2
diff changeset
   330
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Receives a datagram via this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p> If a datagram is immediately available, or if this channel is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * blocking mode and one eventually becomes available, then the datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * copied into the given byte buffer and its source address is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * If this channel is in non-blocking mode and a datagram is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * immediately available then this method immediately returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p> The datagram is transferred into the given byte buffer starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * its current position, as if by a regular {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * ReadableByteChannel#read(java.nio.ByteBuffer) read} operation.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * are fewer bytes remaining in the buffer than are required to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * datagram then the remainder of the datagram is silently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * java.net.DatagramSocket#receive receive} method of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * java.net.DatagramSocket} class.  That is, if the socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * to a specific remote address and a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * then for each datagram received this method verifies that the source's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * address and port number are permitted by the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * java.lang.SecurityManager#checkAccept checkAccept} method.  The overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * of this security check can be avoided by first connecting the socket via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * the {@link #connect connect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * already initiated a read operation upon this channel, however, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * invocation of this method will block until the first operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param  dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         The buffer into which the datagram is to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return  The datagram's source address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          or <tt>null</tt> if this channel is in non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *          and no datagram was immediately available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *          and it does not permit datagrams to be accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *          from the datagram's sender
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public abstract SocketAddress receive(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Sends a datagram via this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p> If this channel is in non-blocking mode and there is sufficient room
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * in the underlying output buffer, or if this channel is in blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * and sufficient room becomes available, then the remaining bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * given buffer are transmitted as a single datagram to the given target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <p> The datagram is transferred from the byte buffer as if by a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * {@link WritableByteChannel#write(java.nio.ByteBuffer) write} operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p> This method performs exactly the same security checks as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * java.net.DatagramSocket#send send} method of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * java.net.DatagramSocket} class.  That is, if the socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * to a specific remote address and a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * then for each datagram sent this method verifies that the target address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * and port number are permitted by the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * java.lang.SecurityManager#checkConnect checkConnect} method.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * overhead of this security check can be avoided by first connecting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * socket via the {@link #connect connect} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * already initiated a write operation upon this channel, however, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * invocation of this method will block until the first operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         The buffer containing the datagram to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param  target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *         The address to which the datagram is to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return   The number of bytes sent, which will be either the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *           of bytes that were remaining in the source buffer when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *           method was invoked or, if this channel is non-blocking, may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *           zero if there was insufficient room for the datagram in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *           underlying output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *          If a security manager has been installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *          and it does not permit datagrams to be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *          to the given address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public abstract int send(ByteBuffer src, SocketAddress target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    // -- ByteChannel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Reads a datagram from this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * connected, and it only accepts datagrams from the socket's peer.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * there are more bytes in the datagram than remain in the given buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * then the remainder of the datagram is silently discarded.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * ReadableByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public abstract int read(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Reads a datagram from this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * connected, and it only accepts datagrams from the socket's peer.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * there are more bytes in the datagram than remain in the given buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * then the remainder of the datagram is silently discarded.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * ScatteringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public abstract long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Reads a datagram from this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * connected, and it only accepts datagrams from the socket's peer.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * there are more bytes in the datagram than remain in the given buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * then the remainder of the datagram is silently discarded.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * this method behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * ScatteringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public final long read(ByteBuffer[] dsts) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return read(dsts, 0, dsts.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Writes a datagram to this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * connected, in which case it sends datagrams directly to the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * peer.  Otherwise it behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * WritableByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public abstract int write(ByteBuffer src) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Writes a datagram to this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * connected, in which case it sends datagrams directly to the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * peer.  Otherwise it behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * GatheringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return   The number of bytes sent, which will be either the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *           of bytes that were remaining in the source buffer when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *           method was invoked or, if this channel is non-blocking, may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *           zero if there was insufficient room for the datagram in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *           underlying output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public abstract long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Writes a datagram to this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p> This method may only be invoked if this channel's socket is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * connected, in which case it sends datagrams directly to the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * peer.  Otherwise it behaves exactly as specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * GatheringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @return   The number of bytes sent, which will be either the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *           of bytes that were remaining in the source buffer when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *           method was invoked or, if this channel is non-blocking, may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *           zero if there was insufficient room for the datagram in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *           underlying output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @throws  NotYetConnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *          If this channel's socket is not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public final long write(ByteBuffer[] srcs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return write(srcs, 0, srcs.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
}