src/java.base/share/classes/java/net/SocketImpl.java
branchniosocketimpl-branch
changeset 57167 82874527373e
parent 47478 438e0c9f2f17
child 57171 d8ed7335dadd
--- a/src/java.base/share/classes/java/net/SocketImpl.java	Fri Feb 08 18:16:53 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocketImpl.java	Fri Feb 08 19:29:14 2019 +0000
@@ -25,12 +25,15 @@
 
 package java.net;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.FileDescriptor;
+import java.io.*;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Set;
 
+import sun.net.NetProperties;
+import sun.nio.ch.NioSocketImpl;
+
 /**
  * The abstract class {@code SocketImpl} is a common superclass
  * of all classes that actually implement sockets. It is used to
@@ -69,6 +72,33 @@
      */
     protected int localport;
 
+    private static boolean useNioSocketImpl = getUseNioSocketImpl();
+
+    // A simple way to override the socketimpl by creating a file in $user.dir
+    private static boolean getUseNioSocketImpl() {
+        // temporary for testing only
+        try {
+            return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
+                @Override
+                public Boolean run() throws Exception {
+                    String s = NetProperties.get("jdk.net.socketimpl.default");
+                    return (s == null || !s.equalsIgnoreCase("classic"));
+                }
+            });
+        } catch (PrivilegedActionException e) {
+            return false;
+        }
+    }
+
+    static SocketImpl getDefaultSocketImpl(boolean server) {
+        if (useNioSocketImpl) {
+            return new NioSocketImpl(server);
+        } else {
+            return new PlainSocketImpl();
+        }
+    }
+
+
     /**
      * Creates either a stream or a datagram socket.
      *
@@ -297,6 +327,28 @@
     }
 
     /**
+     * Implemented by SocksSocketImpl and HttpConnectSocketImpl to show
+     * they delegate to a "real" impl. Accessible through delegate().
+     */
+    interface DelegatingImpl {
+        SocketImpl delegate();
+        SocketImpl newInstance();
+        void postCustomAccept();
+
+        default void copyTo(SocketImpl dest) {
+            SocketImpl src = delegate();
+            if (dest instanceof DelegatingImpl)
+                dest = ((DelegatingImpl)dest).delegate();
+            if (src instanceof NioSocketImpl) {
+                ((NioSocketImpl)src).copyTo(dest);
+            } else if (src instanceof PlainSocketImpl) {
+                ((PlainSocketImpl)src).copyTo(dest);
+            } else
+                throw new InternalError();
+        }
+    }
+
+    /**
      * Returns the address and port of this socket as a {@code String}.
      *
      * @return  a string representation of this socket.