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