Moving delegating SocketImpl to its own class niosocketimpl-branch
authoralanb
Sun, 10 Feb 2019 09:54:31 +0000
branchniosocketimpl-branch
changeset 57172 63ab5af5d009
parent 57171 d8ed7335dadd
child 57173 6692e71a4e9f
Moving delegating SocketImpl to its own class
src/java.base/share/classes/java/net/DelegatingSocketImpl.java
src/java.base/share/classes/java/net/HttpConnectSocketImpl.java
src/java.base/share/classes/java/net/ServerSocket.java
src/java.base/share/classes/java/net/SocketImpl.java
src/java.base/share/classes/java/net/SocksSocketImpl.java
src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/java/net/DelegatingSocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.net;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * A SocketImpl that delegates all methods to another SocketImpl.
+ */
+
+class DelegatingSocketImpl extends SocketImpl {
+    protected final SocketImpl delegate;
+
+    DelegatingSocketImpl(SocketImpl delegate) {
+        this.delegate = Objects.requireNonNull(delegate);
+    }
+
+    final SocketImpl delegate() {
+        return delegate;
+    }
+
+    @Override
+    protected FileDescriptor getFileDescriptor() {
+        return delegate.getFileDescriptor();
+    }
+
+    @Override
+    protected InetAddress getInetAddress() {
+        return delegate.getInetAddress();
+    }
+
+    @Override
+    protected int getPort() {
+        return delegate.getPort();
+    }
+
+    @Override
+    protected int getLocalPort() {
+        return delegate.getLocalPort();
+    }
+
+    @Override
+    protected void create(boolean stream) throws IOException {
+        delegate.create(stream);
+    }
+
+    @Override
+    protected void connect(String host, int port) throws IOException {
+        delegate.connect(host, port);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        delegate.connect(address, port);
+    }
+
+    @Override
+    protected void connect(SocketAddress address, int timeout) throws IOException {
+        delegate.connect(address, timeout);
+    }
+
+    @Override
+    protected void bind(InetAddress host, int port) throws IOException {
+        delegate.bind(host, port);
+    }
+
+    @Override
+    protected void listen(int backlog) throws IOException {
+        delegate.listen(backlog);
+    }
+
+    @Override
+    protected void accept(SocketImpl s) throws IOException {
+        delegate.accept(s);
+    }
+
+    @Override
+    protected InputStream getInputStream() throws IOException {
+        return delegate.getInputStream();
+    }
+
+    @Override
+    protected OutputStream getOutputStream() throws IOException {
+        return delegate.getOutputStream();
+    }
+
+    @Override
+    protected int available() throws IOException {
+        return delegate.available();
+    }
+
+    @Override
+    protected void close() throws IOException {
+        delegate.close();
+    }
+
+    @Override
+    protected boolean supportsUrgentData() {
+        return delegate.supportsUrgentData();
+    }
+
+    @Override
+    protected void sendUrgentData(int data) throws IOException {
+        delegate.sendUrgentData(data);
+    }
+
+    @Override
+    protected Set<SocketOption<?>> supportedOptions() {
+        return delegate.supportedOptions();
+    }
+
+    @Override
+    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
+        delegate.setOption(opt, value);
+    }
+
+    @Override
+    protected <T> T getOption(SocketOption<T> opt) throws IOException {
+        return delegate.getOption(opt);
+    }
+
+    @Override
+    public void setOption(int optID, Object value) throws SocketException {
+        delegate.setOption(optID, value);
+    }
+
+    @Override
+    public Object getOption(int optID) throws SocketException {
+        return delegate.getOption(optID);
+    }
+
+    @Override
+    protected void shutdownInput() throws IOException {
+        delegate.shutdownInput();
+    }
+
+    @Override
+    protected void shutdownOutput() throws IOException {
+        delegate.shutdownOutput();
+    }
+}
--- a/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sat Feb 09 19:16:30 2019 +0000
+++ b/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
@@ -46,7 +46,7 @@
  * @since 1.8
  */
 
-/*package*/ class HttpConnectSocketImpl extends SocketImpl implements SocketImpl.DelegatingImpl {
+/*package*/ class HttpConnectSocketImpl extends DelegatingSocketImpl {
 
     private static final String httpURLClazzStr =
                                   "sun.net.www.protocol.http.HttpURLConnection";
@@ -59,7 +59,6 @@
     private final String server;
     private InetSocketAddress external_address;
     private HashMap<Integer, Object> optionsMap = new HashMap<>();
-    private final SocketImpl delegate;
 
     static  {
         try {
@@ -82,13 +81,13 @@
         }
     }
     HttpConnectSocketImpl(SocketImpl delegate) {
-        this.delegate = delegate;
+        super(delegate);
         this.server = null;
         throw new InternalError();
     }
 
     HttpConnectSocketImpl(Proxy proxy, SocketImpl delegate) {
-        this.delegate = delegate;
+        super(delegate);
         SocketAddress a = proxy.address();
         if ( !(a instanceof InetSocketAddress) )
             throw new IllegalArgumentException("Unsupported address type");
@@ -99,11 +98,6 @@
     }
 
     @Override
-    protected void create(boolean stream) throws IOException {
-        delegate.create(stream);
-    }
-
-    @Override
     protected void connect(String host, int port) throws IOException {
         connect(new InetSocketAddress(host, port), 0);
     }
@@ -121,8 +115,7 @@
 
     @Override
     void setServerSocket(ServerSocket socket) {
-        delegate.serverSocket = socket;
-        super.setServerSocket(socket);
+        throw new InternalError("should not get here");
     }
 
     @Override
@@ -163,49 +156,15 @@
         } catch (IOException x) {  /* gulp! */  }
     }
 
-    @Override
-    protected void bind(InetAddress host, int port) throws IOException {
-        delegate.bind(host, port);
-    }
 
     @Override
-    protected void listen(int backlog) throws IOException {
-        throw new IllegalStateException();
-    }
-
-    @Override
-    protected void accept(SocketImpl s) throws IOException {
-        throw new IllegalStateException();
-    }
-
-    @Override
-    protected InputStream getInputStream() throws IOException {
-        return delegate.getInputStream();
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
     }
 
     @Override
-    protected OutputStream getOutputStream() throws IOException {
-        return delegate.getOutputStream();
-    }
-
-    @Override
-    protected int available() throws IOException {
-        return delegate.available();
-    }
-
-    @Override
-    protected void close() throws IOException {
-        delegate.close();
-    }
-
-    @Override
-    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
-        delegate.setOption(name, value);
-    }
-
-    @Override
-    protected <T> T getOption(SocketOption<T> name) throws IOException {
-        return delegate.getOption(name);
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
     }
 
     @Override
@@ -214,16 +173,6 @@
     }
 
     @Override
-    protected Set<SocketOption<?>> supportedOptions() {
-        return delegate.supportedOptions();
-    }
-
-    @Override
-    protected boolean supportsUrgentData () {
-        return delegate.supportsUrgentData();
-    }
-
-    @Override
     public void setOption(int opt, Object val) throws SocketException {
         delegate.setOption(opt, val);
 
@@ -234,11 +183,6 @@
         optionsMap.put(opt, val);
     }
 
-    @Override
-    public Object getOption(int optID) throws SocketException {
-        return delegate.getOption(optID);
-    }
-
     private Socket privilegedDoTunnel(final String urlString,
                                       final int timeout)
         throws IOException
@@ -299,35 +243,4 @@
         else
             return delegate.getPort();
     }
-
-
-    @Override
-    protected FileDescriptor getFileDescriptor() {
-        return delegate.getFileDescriptor();
-    }
-
-    @Override
-    protected void shutdownInput() throws IOException {
-        delegate.shutdownInput();
-    }
-
-    @Override
-    protected void shutdownOutput() throws IOException {
-        delegate.shutdownOutput();
-    }
-
-    @Override
-    protected int getLocalPort() {
-        return delegate.getLocalPort();
-    }
-
-    @Override
-    protected void sendUrgentData(int data) throws IOException {
-        delegate.sendUrgentData(data);
-    }
-
-    @Override
-    public SocketImpl delegate() {
-        return delegate;
-    }
 }
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Sat Feb 09 19:16:30 2019 +0000
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Sun Feb 10 09:54:31 2019 +0000
@@ -553,7 +553,7 @@
             impl.accept(si);
             try {
                 // a custom impl has accepted the connection with a NIO SocketImpl
-                if ((si instanceof NioSocketImpl) && !(impl instanceof NioSocketImpl)) {
+                if (!(impl instanceof NioSocketImpl) && (si instanceof NioSocketImpl)) {
                     ((NioSocketImpl) si).postCustomAccept();
                 }
             } finally {
@@ -566,8 +566,8 @@
             return;
         }
 
-        if (si instanceof SocketImpl.DelegatingImpl)
-            si = ((SocketImpl.DelegatingImpl) si).delegate();
+        if (si instanceof DelegatingSocketImpl)
+            si = ((DelegatingSocketImpl) si).delegate();
 
         // ServerSocket or Socket is using NIO SocketImpl
         if (impl instanceof NioSocketImpl || si instanceof NioSocketImpl) {
--- a/src/java.base/share/classes/java/net/SocketImpl.java	Sat Feb 09 19:16:30 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
@@ -326,14 +326,6 @@
     }
 
     /**
-     * Implemented by SocksSocketImpl and HttpConnectSocketImpl to show
-     * they delegate to a "real" impl. Accessible through delegate().
-     */
-    interface DelegatingImpl {
-        SocketImpl delegate();
-    }
-
-    /**
      * Returns the address and port of this socket as a {@code String}.
      *
      * @return  a string representation of this socket.
--- a/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sat Feb 09 19:16:30 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
@@ -44,7 +44,7 @@
  * Note this class should <b>NOT</b> be public.
  */
 
-class SocksSocketImpl extends SocketImpl implements SocksConsts, SocketImpl.DelegatingImpl {
+class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
     private String server = null;
     private int serverPort = DEFAULT_PORT;
     private InetSocketAddress external_address;
@@ -52,15 +52,13 @@
     private Socket cmdsock = null;
     private InputStream cmdIn = null;
     private OutputStream cmdOut = null;
-    private final SocketImpl delegate;
 
     SocksSocketImpl(SocketImpl delegate) {
-        Objects.requireNonNull(delegate);
-        this.delegate = delegate;
+        super(delegate);
     }
 
     SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
-        this.delegate = delegate;
+        super(delegate);
         SocketAddress a = proxy.address();
         if (a instanceof InetSocketAddress) {
             InetSocketAddress ad = (InetSocketAddress) a;
@@ -82,7 +80,7 @@
     private synchronized void privilegedConnect(final String host,
                                               final int port,
                                               final int timeout)
-         throws IOException
+        throws IOException
     {
         try {
             AccessController.doPrivileged(
@@ -254,18 +252,13 @@
     }
 
     @Override
-    protected void create(boolean stream) throws IOException {
-        delegate.create(stream);
-    }
-
-    @Override
     protected void connect(String host, int port) throws IOException {
-        delegate.connect(host, port);
+        connect(new InetSocketAddress(host, port), 0);
     }
 
     @Override
     protected void connect(InetAddress address, int port) throws IOException {
-        delegate.connect(address, port);
+        connect(new InetSocketAddress(address, port), 0);
     }
 
     @Override
@@ -276,8 +269,7 @@
 
     @Override
     void setServerSocket(ServerSocket soc) {
-        delegate.serverSocket = soc;
-        super.setServerSocket(soc);
+        throw new InternalError("should not get here");
     }
 
     /**
@@ -551,41 +543,13 @@
     }
 
     @Override
-    protected void bind(InetAddress host, int port) throws IOException {
-        delegate.bind(host, port);
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
     }
 
     @Override
-    protected void listen(int backlog) throws IOException {
-        delegate.listen(backlog);
-    }
-
-    /**
-     * Accept the connection onto the given SocketImpl
-     *
-     * @param      s   the accepted connection.
-     * @throws IOException
-     */
-    @Override
-    protected void accept(SocketImpl s) throws IOException {
-        if (s instanceof SocketImpl.DelegatingImpl)
-            s = ((SocketImpl.DelegatingImpl)s).delegate();
-        delegate.accept(s);
-    }
-
-    @Override
-    protected InputStream getInputStream() throws IOException {
-        return delegate.getInputStream();
-    }
-
-    @Override
-    protected OutputStream getOutputStream() throws IOException {
-        return delegate.getOutputStream();
-    }
-
-    @Override
-    protected int available() throws IOException {
-        return delegate.available();
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
     }
 
     /**
@@ -602,16 +566,6 @@
             return delegate.getInetAddress();
     }
 
-    @Override
-    protected void shutdownOutput() throws IOException {
-        delegate.shutdownOutput();
-    }
-
-    @Override
-    protected FileDescriptor getFileDescriptor() {
-        return delegate.getFileDescriptor();
-    }
-
     /**
      * Returns the value of this socket's {@code port} field.
      *
@@ -627,16 +581,6 @@
     }
 
     @Override
-    protected void sendUrgentData(int data) throws IOException {
-        delegate.sendUrgentData(data);
-    }
-
-    @Override
-    protected void shutdownInput() throws IOException {
-        delegate.shutdownInput();
-    }
-
-    @Override
     protected void close() throws IOException {
         if (cmdsock != null)
             cmdsock.close();
@@ -649,47 +593,7 @@
     }
 
     @Override
-    public void setOption(int optID, Object value) throws SocketException {
-        delegate.setOption(optID, value);
-    }
-
-    @Override
-    public Object getOption(int optID) throws SocketException {
-        return delegate.getOption(optID);
-    }
-
-    @Override
-    protected boolean supportsUrgentData () {
-        return delegate.supportsUrgentData();
-    }
-
-    @Override
-    protected int getLocalPort() {
-        return delegate.getLocalPort();
-    }
-
-    @Override
-    protected <T> void setOption(SocketOption<T> name, T value) throws IOException {
-        delegate.setOption(name, value);
-    }
-
-    @Override
-    protected <T> T getOption(SocketOption<T> name) throws IOException {
-        return delegate.getOption(name);
-    }
-
-    @Override
     void reset() throws IOException {
         delegate.reset();
     }
-
-    @Override
-    protected Set<SocketOption<?>> supportedOptions() {
-        return delegate.supportedOptions();
-    }
-
-    @Override
-    public SocketImpl delegate() {
-        return delegate;
-    }
 }
--- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Sat Feb 09 19:16:30 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
@@ -80,7 +80,7 @@
  * an application continues to call read or available after a reset.
  */
 
-public class NioSocketImpl extends SocketImpl {
+public final class NioSocketImpl extends SocketImpl {
     private static final NativeDispatcher nd = new SocketDispatcher();
 
     // The maximum number of bytes to read/write per syscall to avoid needing