src/java.base/share/classes/java/net/SocketImpl.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 55346 730ed3fc6605
child 58679 9c3209ff7550
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
     2
 * Copyright (c) 1995, 2019, 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
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    28
import java.io.FileDescriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    32
import java.security.AccessController;
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    33
import java.security.PrivilegedAction;
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
    34
import java.util.Objects;
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
    35
import java.util.Set;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    36
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    37
import sun.net.NetProperties;
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    38
import sun.net.PlatformSocketImpl;
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    39
import sun.nio.ch.NioSocketImpl;
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
    42
 * The abstract class {@code SocketImpl} is a common superclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of all classes that actually implement sockets. It is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * create both client and server sockets.
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    45
 *
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    46
 * @implNote Client and server sockets created with the {@code Socket} and
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    47
 * {@code SocketServer} public constructors create a system-default
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    48
 * {@code SocketImpl}. The JDK historically used a {@code SocketImpl}
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    49
 * implementation type named "PlainSocketImpl" that has since been replaced by a
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    50
 * newer implementation. The JDK continues to ship with the older implementation
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    51
 * to allow code to run that depends on unspecified behavior that differs between
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    52
 * the old and new implementations. The old implementation will be used if the
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    53
 * Java virtual machine is started with the system property {@systemProperty
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    54
 * jdk.net.usePlainSocketImpl} set to use the old implementation. It may also be
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    55
 * set in the JDK's network configuration file, located in {@code
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    56
 * ${java.home}/conf/net.properties}. The value of the property is the string
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    57
 * representation of a boolean. If set without a value then it defaults to {@code
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    58
 * true}, hence running with {@code -Djdk.net.usePlainSocketImpl} or {@code
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    59
 * -Djdk.net.usePlainSocketImpl=true} will configure the Java virtual machine
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    60
 * to use the old implementation. The property and old implementation will be
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    61
 * removed in a future version.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @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
    64
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public abstract class SocketImpl implements SocketOptions {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    67
    private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl();
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    68
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    69
    private static boolean usePlainSocketImpl() {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    70
        PrivilegedAction<String> pa = () -> NetProperties.get("jdk.net.usePlainSocketImpl");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    71
        String s = AccessController.doPrivileged(pa);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    72
        return (s != null) && !s.equalsIgnoreCase("false");
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    73
    }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    74
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    75
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    76
     * Creates an instance of platform's SocketImpl
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    77
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    78
    @SuppressWarnings("unchecked")
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    79
    static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) {
55102
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    80
        if (USE_PLAINSOCKETIMPL) {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    81
            return (S) new PlainSocketImpl(server);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    82
        } else {
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    83
            return (S) new NioSocketImpl(server);
59567035d279 8221481: Reimplement the Legacy Socket API
alanb
parents: 55081
diff changeset
    84
        }
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    85
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The file descriptor object for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The IP address of the remote end of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected InetAddress address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The port number on the remote host to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * The local port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected int localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54155
diff changeset
   108
     * Initialize a new instance of this class
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54155
diff changeset
   109
     */
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   110
    public SocketImpl() { }
54689
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54155
diff changeset
   111
b28b7f631301 8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents: 54155
diff changeset
   112
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Creates either a stream or a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   115
     * @param      stream   if {@code true}, create a stream socket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *                      otherwise, create a datagram socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   117
     * @throws     IOException  if an I/O error occurs while creating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *               socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected abstract void create(boolean stream) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Connects this socket to the specified port on the named host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param      host   the name of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param      port   the port number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   127
     * @throws     IOException  if an I/O error occurs when connecting to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *               remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected abstract void connect(String host, int port) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Connects this socket to the specified port number on the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param      address   the IP address of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param      port      the port number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   137
     * @throws     IOException  if an I/O error occurs when attempting a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected abstract void connect(InetAddress address, int port) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Connects this socket to the specified port number on the specified host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * A timeout of zero is interpreted as an infinite timeout. The connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * will then block until established or an error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param      address   the Socket address of the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param     timeout  the timeout value, in milliseconds, or zero for no timeout.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   149
     * @throws     IOException  if an I/O error occurs when attempting a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    protected abstract void connect(SocketAddress address, int timeout) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Binds this socket to the specified local IP address and port number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param      host   an IP address that belongs to a local interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param      port   the port number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   160
     * @throws     IOException  if an I/O error occurs when binding this socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected abstract void bind(InetAddress host, int port) 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
     * Sets the maximum queue length for incoming connection indications
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   166
     * (a request to connect) to the {@code count} argument. If a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * connection indication arrives when the queue is full, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * connection is refused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param      backlog   the maximum length of the queue.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   171
     * @throws     IOException  if an I/O error occurs when creating the queue.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected abstract void listen(int backlog) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Accepts a connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param      s   the accepted connection.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   179
     * @throws     IOException  if an I/O error occurs when accepting the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected abstract void accept(SocketImpl s) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns an input stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return     a stream for reading from this socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   188
     * @throws     IOException  if an I/O error occurs when creating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *               input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected abstract InputStream getInputStream() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns an output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return     an output stream for writing to this socket.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   197
     * @throws     IOException  if an I/O error occurs when creating the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *               output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected abstract OutputStream getOutputStream() 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
     * Returns the number of bytes that can be read from this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @return     the number of bytes that can be read from this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *             without blocking.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   208
     * @throws     IOException  if an I/O error occurs when determining the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *               number of bytes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    protected abstract int available() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Closes this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   216
     * @throws     IOException  if an I/O error occurs when closing this socket.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected abstract void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   221
     * Closes this socket, ignoring any IOException that is thrown by close.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   222
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   223
    void closeQuietly() {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   224
        try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   225
            close();
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   226
        } catch (IOException ignore) { }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   227
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   228
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   229
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Places the input stream for this socket at "end of stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Any data sent to this socket is acknowledged and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * silently discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents: 5506
diff changeset
   234
     * 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
   235
     * 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
   236
     * {@code read} methods will return {@code -1} (end of stream).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   238
     * @throws    IOException if an I/O error occurs when shutting down this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see java.net.Socket#shutdownOutput()
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 shutdownInput() 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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Disables the output stream for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * For a TCP socket, any previously written data will be sent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * followed by TCP's normal connection termination sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * If you write to a socket output stream after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * shutdownOutput() on the socket, the stream will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   258
     * @throws    IOException if an I/O error occurs when shutting down this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see java.net.Socket#shutdownInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @see java.net.Socket#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @see java.net.Socket#setSoLinger(boolean, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected void shutdownOutput() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
      throw new IOException("Method not implemented!");
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 fd} 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 fd} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see     java.net.SocketImpl#fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    protected FileDescriptor getFileDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   280
     * Returns the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   282
     * @return  the value of this socket's {@code address} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    protected InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   290
     * Returns the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   292
     * @return  the value of this socket's {@code port} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see     java.net.SocketImpl#port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns whether or not this SocketImpl supports sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * urgent data. By default, false is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * unless the method is overridden in a sub-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return  true if urgent data supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @see     java.net.SocketImpl#address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    protected boolean supportsUrgentData () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return false; // must be overridden in sub-class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Send one byte of urgent data on the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * The byte to be sent is the low eight bits of the parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param data The byte of data to send
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55346
diff changeset
   316
     * @throws    IOException if there is an error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *  sending the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    protected abstract void sendUrgentData (int data) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   323
     * Returns the value of this socket's {@code localport} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   325
     * @return  the value of this socket's {@code localport} field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see     java.net.SocketImpl#localport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    protected int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return localport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   333
     * Returns the address and port of this socket as a {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return  a string representation of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return "Socket[addr=" + getInetAddress() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            ",port=" + getPort() + ",localport=" + getLocalPort()  + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   342
    void reset() {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   343
        fd = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        localport = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Sets performance preferences for this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p> Sockets use the TCP/IP protocol by default.  Some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * may offer alternative protocols which have different performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * characteristics than TCP/IP.  This method allows the application to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * express its own preferences as to how these tradeoffs should be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * when the implementation chooses from the available protocols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <p> Performance preferences are described by three integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * whose values indicate the relative importance of short connection time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * low latency, and high bandwidth.  The absolute values of the integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * are irrelevant; in order to choose a protocol the values are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * compared, with larger values indicating stronger preferences. Negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * values represent a lower priority than positive values. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * application prefers short connection time over both low latency and high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * 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
   366
     * {@code (1, 0, 0)}.  If the application prefers high bandwidth above low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * latency, and low latency above short connection time, then it could
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   368
     * invoke this method with the values {@code (0, 1, 2)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   370
     * By default, this method does nothing, unless it is overridden in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * a sub-class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param  connectionTime
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   374
     *         An {@code int} expressing the relative importance of a short
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *         connection time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param  latency
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   378
     *         An {@code int} expressing the relative importance of low
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *         latency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param  bandwidth
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   382
     *         An {@code int} expressing the relative importance of high
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *         bandwidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    protected void setPerformancePreferences(int connectionTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                          int latency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                          int bandwidth)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /* Not implemented yet */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   393
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   394
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   395
     * Called to set a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   396
     *
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   397
     * @implSpec
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   398
     * The default implementation of this method first checks that the given
55346
730ed3fc6605 8225651: Missed the `@` in a couple of code tags of SocketImpl
chegar
parents: 55102
diff changeset
   399
     * socket option {@code name} is not null, then throws {@code
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   400
     * UnsupportedOperationException}. Subclasses should override this method
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   401
     * with an appropriate implementation.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   402
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 27341
diff changeset
   403
     * @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
   404
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   405
     * @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
   406
     *              may be valid for some options.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   407
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   408
     * @throws UnsupportedOperationException if the SocketImpl does not
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   409
     *         support the option
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   410
     * @throws IllegalArgumentException if the value is not valid for
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   411
     *         the option
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   412
     * @throws IOException if an I/O error occurs, or if the socket is closed
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   413
     * @throws NullPointerException if name is {@code null}
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   414
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34968
diff changeset
   415
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   416
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   417
    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   418
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   419
        throw new UnsupportedOperationException("'" + name + "' not supported");
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   420
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   421
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   422
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   423
     * Called to get a socket option.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   424
     *
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   425
     * @implSpec
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   426
     * The default implementation of this method first checks that the given
55346
730ed3fc6605 8225651: Missed the `@` in a couple of code tags of SocketImpl
chegar
parents: 55102
diff changeset
   427
     * socket option {@code name} is not null, then throws {@code
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   428
     * UnsupportedOperationException}. Subclasses should override this method
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   429
     * with an appropriate implementation.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   430
     *
28852
a581c7868768 8041395: Doclint regression in java.net.SocketOption
darcy
parents: 27341
diff changeset
   431
     * @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
   432
     * @param name The socket option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   433
     * @return the value of the named option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   434
     *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   435
     * @throws UnsupportedOperationException if the SocketImpl does not
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   436
     *         support the option
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   437
     * @throws IOException if an I/O error occurs, or if the socket is closed
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   438
     * @throws NullPointerException if name is {@code null}
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   439
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34968
diff changeset
   440
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   441
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   442
    protected <T> T getOption(SocketOption<T> name) throws IOException {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   443
        Objects.requireNonNull(name);
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   444
        throw new UnsupportedOperationException("'" + name + "' not supported");
23879
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
54155
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   447
    /**
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   448
     * Attempts to copy socket options from this SocketImpl to a target SocketImpl.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   449
     * At this time, only the SO_TIMEOUT make sense to copy.
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   450
     */
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   451
    void copyOptionsTo(SocketImpl target) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   452
        try {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   453
            Object timeout = getOption(SocketOptions.SO_TIMEOUT);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   454
            if (timeout instanceof Integer) {
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   455
                target.setOption(SocketOptions.SO_TIMEOUT, timeout);
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   456
            }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   457
        } catch (IOException ignore) { }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   458
    }
b5a73f22b2bd 8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
alanb
parents: 47478
diff changeset
   459
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   460
    /**
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   461
     * 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
   462
     * 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
   463
     *
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   464
     * @implSpec
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   465
     * The default implementation of this method returns an empty set.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   466
     * Subclasses should override this method with an appropriate implementation.
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   467
     *
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   468
     * @return a Set of SocketOptions
36228
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   469
     *
a16a22218e23 8148609: socket impl supportedOptions() should return an immutable set
vtewari
parents: 36115
diff changeset
   470
     * @since 9
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   471
     */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   472
    protected Set<SocketOption<?>> supportedOptions() {
55081
dd321e3596c0 8224477: java.net socket types new-style socket option methods - spec and impl mismatch
chegar
parents: 54689
diff changeset
   473
        return Set.of();
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents: 19069
diff changeset
   474
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}