--- old/src/java.base/share/classes/java/net/SocketImpl.java 2019-04-18 10:58:19.000000000 +0100 +++ new/src/java.base/share/classes/java/net/SocketImpl.java 2019-04-18 10:58:19.000000000 +0100 @@ -46,19 +46,32 @@ */ public abstract class SocketImpl implements SocketOptions { + // true if this SocketImpl for a ServerSocket. By default, this field + // will be set to true. It can only be set to false by platform impls. + /*package*/ final boolean server; + /** - * Creates an instance of platform's SocketImpl + * Creates a new SocketImpl. */ - @SuppressWarnings("unchecked") - static S createPlatformSocketImpl(boolean server) { - return (S) new PlainSocketImpl(); + public SocketImpl() { + this(true); + } + + /** + * Creates a new SocketImpl. + * @param server true if this SocketImpl for a ServerSocket + */ + /*package*/ SocketImpl(boolean server) { + this.server = server; } /** - * The actual Socket object. + * Creates an instance of platform's SocketImpl */ - Socket socket = null; - ServerSocket serverSocket = null; + @SuppressWarnings("unchecked") + static S createPlatformSocketImpl(boolean server) { + return (S) new PlainSocketImpl(server); + } /** * The file descriptor object for this socket. @@ -300,22 +313,6 @@ return localport; } - void setSocket(Socket soc) { - this.socket = soc; - } - - Socket getSocket() { - return socket; - } - - void setServerSocket(ServerSocket soc) { - this.serverSocket = soc; - } - - ServerSocket getServerSocket() { - return serverSocket; - } - /** * Returns the address and port of this socket as a {@code String}. * @@ -395,11 +392,9 @@ * @since 9 */ protected void setOption(SocketOption name, T value) throws IOException { - if (name == StandardSocketOptions.SO_KEEPALIVE && - (getSocket() != null)) { + if (name == StandardSocketOptions.SO_KEEPALIVE && !server) { setOption(SocketOptions.SO_KEEPALIVE, value); - } else if (name == StandardSocketOptions.SO_SNDBUF && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.SO_SNDBUF && !server) { setOption(SocketOptions.SO_SNDBUF, value); } else if (name == StandardSocketOptions.SO_RCVBUF) { setOption(SocketOptions.SO_RCVBUF, value); @@ -408,13 +403,11 @@ } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { setOption(SocketOptions.SO_REUSEPORT, value); - } else if (name == StandardSocketOptions.SO_LINGER && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.SO_LINGER && !server) { setOption(SocketOptions.SO_LINGER, value); } else if (name == StandardSocketOptions.IP_TOS) { setOption(SocketOptions.IP_TOS, value); - } else if (name == StandardSocketOptions.TCP_NODELAY && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.TCP_NODELAY && !server) { setOption(SocketOptions.TCP_NODELAY, value); } else { throw new UnsupportedOperationException("unsupported option"); @@ -438,11 +431,9 @@ */ @SuppressWarnings("unchecked") protected T getOption(SocketOption name) throws IOException { - if (name == StandardSocketOptions.SO_KEEPALIVE && - (getSocket() != null)) { + if (name == StandardSocketOptions.SO_KEEPALIVE && !server) { return (T)getOption(SocketOptions.SO_KEEPALIVE); - } else if (name == StandardSocketOptions.SO_SNDBUF && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.SO_SNDBUF && !server) { return (T)getOption(SocketOptions.SO_SNDBUF); } else if (name == StandardSocketOptions.SO_RCVBUF) { return (T)getOption(SocketOptions.SO_RCVBUF); @@ -451,13 +442,11 @@ } else if (name == StandardSocketOptions.SO_REUSEPORT && supportedOptions().contains(name)) { return (T)getOption(SocketOptions.SO_REUSEPORT); - } else if (name == StandardSocketOptions.SO_LINGER && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.SO_LINGER && !server) { return (T)getOption(SocketOptions.SO_LINGER); } else if (name == StandardSocketOptions.IP_TOS) { return (T)getOption(SocketOptions.IP_TOS); - } else if (name == StandardSocketOptions.TCP_NODELAY && - (getSocket() != null)) { + } else if (name == StandardSocketOptions.TCP_NODELAY && !server) { return (T)getOption(SocketOptions.TCP_NODELAY); } else { throw new UnsupportedOperationException("unsupported option"); @@ -504,10 +493,10 @@ * @since 9 */ protected Set> supportedOptions() { - if (getSocket() != null) { - return socketOptions; - } else { + if (server) { return serverSocketOptions; + } else { + return socketOptions; } } }