src/java.base/share/classes/java/net/SocksSocketImpl.java
branchniosocketimpl-branch
changeset 57167 82874527373e
parent 57112 d7b54daf5e1a
child 57171 d8ed7335dadd
--- a/src/java.base/share/classes/java/net/SocksSocketImpl.java	Fri Feb 08 18:16:53 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocksSocketImpl.java	Fri Feb 08 19:29:14 2019 +0000
@@ -24,11 +24,14 @@
  */
 package java.net;
 
+import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.BufferedOutputStream;
 import java.security.AccessController;
+import java.util.Objects;
+import java.util.Set;
 
 import jdk.internal.util.StaticProperty;
 import sun.net.SocksProxy;
@@ -41,7 +44,7 @@
  * Note this class should <b>NOT</b> be public.
  */
 
-class SocksSocketImpl extends NioSocketImpl implements SocksConsts {
+class SocksSocketImpl extends SocketImpl implements SocksConsts, SocketImpl.DelegatingImpl {
     private String server = null;
     private int serverPort = DEFAULT_PORT;
     private InetSocketAddress external_address;
@@ -49,13 +52,15 @@
     private Socket cmdsock = null;
     private InputStream cmdIn = null;
     private OutputStream cmdOut = null;
+    final SocketImpl delegate;
 
-    SocksSocketImpl() {
-        super(false);
+    SocksSocketImpl(SocketImpl delegate) {
+        Objects.requireNonNull(delegate);
+        this.delegate = delegate;
     }
 
-    SocksSocketImpl(Proxy proxy) {
-        super(false);
+    SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
+        this.delegate = delegate;
         SocketAddress a = proxy.address();
         if (a instanceof InetSocketAddress) {
             InetSocketAddress ad = (InetSocketAddress) a;
@@ -96,7 +101,7 @@
 
     private void superConnectServer(String host, int port,
                                     int timeout) throws IOException {
-        super.connect(new InetSocketAddress(host, port), timeout);
+        delegate.connect(new InetSocketAddress(host, port), timeout);
     }
 
     private static int remainingMillis(long deadlineMillis) throws IOException {
@@ -248,6 +253,33 @@
         }
     }
 
+    @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
+    void setSocket(Socket soc) {
+        delegate.socket = soc;
+        super.setSocket(soc);
+    }
+
+    @Override
+    void setServerSocket(ServerSocket soc) {
+        delegate.serverSocket = soc;
+        super.setServerSocket(soc);
+    }
+
     /**
      * Connects the Socks Socket to the specified endpoint. It will first
      * connect to the SOCKS proxy and negotiate the access. If the proxy
@@ -299,7 +331,7 @@
                 /*
                  * No default proxySelector --> direct connection
                  */
-                super.connect(epoint, remainingMillis(deadlineMillis));
+                delegate.connect(epoint, remainingMillis(deadlineMillis));
                 return;
             }
             URI uri;
@@ -322,13 +354,13 @@
             java.util.Iterator<Proxy> iProxy = null;
             iProxy = sel.select(uri).iterator();
             if (iProxy == null || !(iProxy.hasNext())) {
-                super.connect(epoint, remainingMillis(deadlineMillis));
+                delegate.connect(epoint, remainingMillis(deadlineMillis));
                 return;
             }
             while (iProxy.hasNext()) {
                 p = iProxy.next();
                 if (p == null || p.type() != Proxy.Type.SOCKS) {
-                    super.connect(epoint, remainingMillis(deadlineMillis));
+                    delegate.connect(epoint, remainingMillis(deadlineMillis));
                     return;
                 }
 
@@ -518,7 +550,43 @@
         external_address = epoint;
     }
 
+    @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);
+    }
+
+    /**
+     * 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();
+    }
 
     /**
      * Returns the value of this socket's {@code address} field.
@@ -531,7 +599,17 @@
         if (external_address != null)
             return external_address.getAddress();
         else
-            return super.getInetAddress();
+            return delegate.getInetAddress();
+    }
+
+    @Override
+    protected void shutdownOutput() throws IOException {
+        delegate.shutdownOutput();
+    }
+
+    @Override
+    protected FileDescriptor getFileDescriptor() {
+        return delegate.getFileDescriptor();
     }
 
     /**
@@ -545,7 +623,17 @@
         if (external_address != null)
             return external_address.getPort();
         else
-            return super.getPort();
+            return delegate.getPort();
+    }
+
+    @Override
+    protected void sendUrgentData(int data) throws IOException {
+        delegate.sendUrgentData(data);
+    }
+
+    @Override
+    protected void shutdownInput() throws IOException {
+        delegate.shutdownInput();
     }
 
     @Override
@@ -553,10 +641,74 @@
         if (cmdsock != null)
             cmdsock.close();
         cmdsock = null;
-        super.close();
+        delegate.close();
     }
 
     private String getUserName() {
         return StaticProperty.userName();
     }
+
+    @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;
+    }
+
+    @Override
+    public SocketImpl newInstance() {
+        if (delegate instanceof PlainSocketImpl)
+            return new SocksSocketImpl(new PlainSocketImpl());
+        else if (delegate instanceof NioSocketImpl)
+            return new SocksSocketImpl(new NioSocketImpl(false));
+        throw new InternalError();
+    }
+
+    @Override
+    public void postCustomAccept() {
+        if (delegate instanceof NioSocketImpl) {
+            // TODO ((NioSocketImpl)delegate).postCustomAccept();
+        } else if (delegate instanceof PlainSocketImpl) {
+            ((PlainSocketImpl)delegate).postCustomAccept();
+        } else
+            throw new InternalError();
+    }
 }