src/java.base/share/classes/java/net/SocketImpl.java
author alanb
Sat, 09 Feb 2019 19:16:30 +0000
branchniosocketimpl-branch
changeset 57171 d8ed7335dadd
parent 57167 82874527373e
child 57172 63ab5af5d009
permissions -rw-r--r--
Fix issues when configured to use PlainSocketImpl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
     2
 * Copyright (c) 1995, 2016, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    28
import java.io.*;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    29
import java.security.AccessController;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    30
import java.security.PrivilegedActionException;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    31
import java.security.PrivilegedExceptionAction;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
    32
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    34
import sun.net.NetProperties;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    35
import sun.nio.ch.NioSocketImpl;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    38
 * The abstract class {@code SocketImpl} is a common superclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of all classes that actually implement sockets. It is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * create both client and server sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A "plain" socket implements these methods exactly as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * described, without attempting to go through a firewall or proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  unascribed
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24046
diff changeset
    46
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class SocketImpl implements SocketOptions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * The actual Socket object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ServerSocket serverSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The file descriptor object for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The IP address of the remote end of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The port number on the remote host to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * The local port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected int localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
57171
d8ed7335dadd Fix issues when configured to use PlainSocketImpl
alanb
parents: 57167
diff changeset
    75
    private static final boolean useNioSocketImpl = getUseNioSocketImpl();
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    76
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    77
    // A simple way to override the socketimpl by creating a file in $user.dir
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    78
    private static boolean getUseNioSocketImpl() {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    79
        // temporary for testing only
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    80
        try {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    81
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    82
                @Override
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    83
                public Boolean run() throws Exception {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    84
                    String s = NetProperties.get("jdk.net.socketimpl.default");
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    85
                    return (s == null || !s.equalsIgnoreCase("classic"));
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    86
                }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    87
            });
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    88
        } catch (PrivilegedActionException e) {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    89
            return false;
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    90
        }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    91
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    92
57171
d8ed7335dadd Fix issues when configured to use PlainSocketImpl
alanb
parents: 57167
diff changeset
    93
    static SocketImpl createDefaultSocketImpl(boolean server) {
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    94
        if (useNioSocketImpl) {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    95
            return new NioSocketImpl(server);
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    96
        } else {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    97
            return new PlainSocketImpl();
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    98
        }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
    99
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   100
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Creates either a stream or a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   104
     * @param      stream   if {@code true}, create a stream socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *                      otherwise, create a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception  IOException  if an I/O error occurs while creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *               socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected abstract void create(boolean stream) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Connects this socket to the specified port on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param      host   the name of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param      port   the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception  IOException  if an I/O error occurs when connecting to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *               remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected abstract void connect(String host, int port) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Connects this socket to the specified port number on the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param      address   the IP address of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param      port      the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception  IOException  if an I/O error occurs when attempting a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected abstract void connect(InetAddress address, int port) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Connects this socket to the specified port number on the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * A timeout of zero is interpreted as an infinite timeout. The connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * will then block until established or an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param      address   the Socket address of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param     timeout  the timeout value, in milliseconds, or zero for no timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception  IOException  if an I/O error occurs when attempting a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected abstract void connect(SocketAddress address, int timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Binds this socket to the specified local IP address and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param      host   an IP address that belongs to a local interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param      port   the port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception  IOException  if an I/O error occurs when binding this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected abstract void bind(InetAddress host, int port) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Sets the maximum queue length for incoming connection indications
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   155
     * (a request to connect) to the {@code count} argument. If a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * connection indication arrives when the queue is full, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param      backlog   the maximum length of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception  IOException  if an I/O error occurs when creating the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected abstract void listen(int backlog) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Accepts a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param      s   the accepted connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception  IOException  if an I/O error occurs when accepting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected abstract void accept(SocketImpl s) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns an input stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return     a stream for reading from this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception  IOException  if an I/O error occurs when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *               input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected abstract InputStream getInputStream() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns an output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return     an output stream for writing to this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception  IOException  if an I/O error occurs when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *               output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected abstract OutputStream getOutputStream() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the number of bytes that can be read from this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return     the number of bytes that can be read from this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *             without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @exception  IOException  if an I/O error occurs when determining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *               number of bytes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected abstract int available() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception  IOException  if an I/O error occurs when closing this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected abstract void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Places the input stream for this socket at "end of stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Any data sent to this socket is acknowledged and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * silently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 5506
diff changeset
   214
     * If you read from a socket input stream after invoking this method on the
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 5506
diff changeset
   215
     * socket, the stream's {@code available} method will return 0, and its
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 5506
diff changeset
   216
     * {@code read} methods will return {@code -1} (end of stream).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception IOException if an I/O error occurs when shutting down this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see java.net.Socket#shutdownOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    protected void shutdownInput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      throw new IOException("Method not implemented!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Disables the output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * For a TCP socket, any previously written data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * followed by TCP's normal connection termination sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * If you write to a socket output stream after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * shutdownOutput() on the socket, the stream will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception IOException if an I/O error occurs when shutting down this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see java.net.Socket#shutdownInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    protected void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      throw new IOException("Method not implemented!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   250
     * Returns the value of this socket's {@code fd} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   252
     * @return  the value of this socket's {@code fd} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see     java.net.SocketImpl#fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected FileDescriptor getFileDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   260
     * Returns the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   262
     * @return  the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   270
     * Returns the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   272
     * @return  the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see     java.net.SocketImpl#port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    protected int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns whether or not this SocketImpl supports sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * urgent data. By default, false is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * unless the method is overridden in a sub-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return  true if urgent data supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected boolean supportsUrgentData () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return false; // must be overridden in sub-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Send one byte of urgent data on the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * The byte to be sent is the low eight bits of the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param data The byte of data to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @exception IOException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *  sending the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    protected abstract void sendUrgentData (int data) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   303
     * Returns the value of this socket's {@code localport} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   305
     * @return  the value of this socket's {@code localport} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see     java.net.SocketImpl#localport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    protected int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    void setSocket(Socket soc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        this.socket = soc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    Socket getSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    void setServerSocket(ServerSocket soc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this.serverSocket = soc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    ServerSocket getServerSocket() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
57167
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   329
     * Implemented by SocksSocketImpl and HttpConnectSocketImpl to show
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   330
     * they delegate to a "real" impl. Accessible through delegate().
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   331
     */
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   332
    interface DelegatingImpl {
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   333
        SocketImpl delegate();
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   334
    }
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   335
82874527373e Socket changes to support both NioSocketImpl and PlainSocketImpl switchable by net property
michaelm
parents: 47478
diff changeset
   336
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   337
     * Returns the address and port of this socket as a {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            ",port=" + getPort() + ",localport=" + getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        localport = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Sets performance preferences for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * compared, with larger values indicating stronger preferences. Negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * values represent a lower priority than positive values. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * bandwidth, for example, then it could invoke this method with the values
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   369
     * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * latency, and low latency above short connection time, then it could
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   371
     * invoke this method with the values {@code (0, 1, 2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   373
     * By default, this method does nothing, unless it is overridden in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * a sub-class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param  connectionTime
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   377
     *         An {@code int} expressing the relative importance of a short
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @param  latency
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   381
     *         An {@code int} expressing the relative importance of low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param  bandwidth
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   385
     *         An {@code int} expressing the relative importance of high
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    protected void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   396
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   397
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   398
     * Called to set a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   399
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 27341
diff changeset
   400
     * @param <T> The type of the socket option value
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   401
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   402
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   403
     * @param value The value of the socket option. A value of {@code null}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   404
     *              may be valid for some options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   405
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   406
     * @throws UnsupportedOperationException if the SocketImpl does not
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   407
     *         support the option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   408
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   409
     * @throws IOException if an I/O error occurs, or if the socket is closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   410
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34968
diff changeset
   411
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   412
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   413
    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   414
        if (name == StandardSocketOptions.SO_KEEPALIVE &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   415
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   416
            setOption(SocketOptions.SO_KEEPALIVE, value);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   417
        } else if (name == StandardSocketOptions.SO_SNDBUF &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   418
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   419
            setOption(SocketOptions.SO_SNDBUF, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   420
        } else if (name == StandardSocketOptions.SO_RCVBUF) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   421
            setOption(SocketOptions.SO_RCVBUF, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   422
        } else if (name == StandardSocketOptions.SO_REUSEADDR) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   423
            setOption(SocketOptions.SO_REUSEADDR, value);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   424
        } else if (name == StandardSocketOptions.SO_REUSEPORT &&
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   425
            supportedOptions().contains(name)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   426
            setOption(SocketOptions.SO_REUSEPORT, value);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   427
        } else if (name == StandardSocketOptions.SO_LINGER &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   428
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   429
            setOption(SocketOptions.SO_LINGER, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   430
        } else if (name == StandardSocketOptions.IP_TOS) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   431
            setOption(SocketOptions.IP_TOS, value);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   432
        } else if (name == StandardSocketOptions.TCP_NODELAY &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   433
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   434
            setOption(SocketOptions.TCP_NODELAY, value);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   435
        } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   436
            throw new UnsupportedOperationException("unsupported option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   437
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   438
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   439
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   440
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   441
     * Called to get a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   442
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 27341
diff changeset
   443
     * @param <T> The type of the socket option value
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   444
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   445
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   446
     * @return the value of the named option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   447
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   448
     * @throws UnsupportedOperationException if the SocketImpl does not
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   449
     *         support the option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   450
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   451
     * @throws IOException if an I/O error occurs, or if the socket is closed.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   452
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34968
diff changeset
   453
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   454
     */
24046
d1f6da65881a 8041397: Lint regression in java.net.SocketOption
michaelm
parents: 23879
diff changeset
   455
    @SuppressWarnings("unchecked")
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   456
    protected <T> T getOption(SocketOption<T> name) throws IOException {
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   457
        if (name == StandardSocketOptions.SO_KEEPALIVE &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   458
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   459
            return (T)getOption(SocketOptions.SO_KEEPALIVE);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   460
        } else if (name == StandardSocketOptions.SO_SNDBUF &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   461
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   462
            return (T)getOption(SocketOptions.SO_SNDBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   463
        } else if (name == StandardSocketOptions.SO_RCVBUF) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   464
            return (T)getOption(SocketOptions.SO_RCVBUF);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   465
        } else if (name == StandardSocketOptions.SO_REUSEADDR) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   466
            return (T)getOption(SocketOptions.SO_REUSEADDR);
36115
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   467
        } else if (name == StandardSocketOptions.SO_REUSEPORT &&
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   468
            supportedOptions().contains(name)) {
0676e37a0b9c 6432031: Add support for SO_REUSEPORT
alanb
parents: 35302
diff changeset
   469
            return (T)getOption(SocketOptions.SO_REUSEPORT);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   470
        } else if (name == StandardSocketOptions.SO_LINGER &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   471
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   472
            return (T)getOption(SocketOptions.SO_LINGER);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   473
        } else if (name == StandardSocketOptions.IP_TOS) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   474
            return (T)getOption(SocketOptions.IP_TOS);
34968
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   475
        } else if (name == StandardSocketOptions.TCP_NODELAY &&
93b315c61ca3 8143554: UnsupportedOperationException is not thrown for unsupported options
kshefov
parents: 28852
diff changeset
   476
                (getSocket() != null)) {
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   477
            return (T)getOption(SocketOptions.TCP_NODELAY);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   478
        } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   479
            throw new UnsupportedOperationException("unsupported option");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   480
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   481
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   482
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   483
    private static final Set<SocketOption<?>> socketOptions;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   484
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   485
    private static final Set<SocketOption<?>> serverSocketOptions;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   486
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   487
    static {
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   488
        socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   489
                               StandardSocketOptions.SO_SNDBUF,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   490
                               StandardSocketOptions.SO_RCVBUF,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   491
                               StandardSocketOptions.SO_REUSEADDR,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   492
                               StandardSocketOptions.SO_LINGER,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   493
                               StandardSocketOptions.IP_TOS,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   494
                               StandardSocketOptions.TCP_NODELAY);
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   495
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   496
        serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   497
                                     StandardSocketOptions.SO_REUSEADDR,
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   498
                                     StandardSocketOptions.IP_TOS);
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   499
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   500
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   501
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   502
     * Returns a set of SocketOptions supported by this impl
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   503
     * and by this impl's socket (Socket or ServerSocket)
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   504
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   505
     * @return a Set of SocketOptions
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   506
     *
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   507
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   508
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   509
    protected Set<SocketOption<?>> supportedOptions() {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   510
        if (getSocket() != null) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   511
            return socketOptions;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   512
        } else {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   513
            return serverSocketOptions;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   514
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   515
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
}