Remove support for accepting connection with foreign impls niosocketimpl-branch
authoralanb
Thu, 04 Apr 2019 18:52:22 +0100
branchniosocketimpl-branch
changeset 57304 18e98037d493
parent 57301 66c3aaf31f2b
child 57305 b2c5d70eecf6
Remove support for accepting connection with foreign impls
src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
--- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Wed Apr 03 10:47:10 2019 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Thu Apr 04 18:52:22 2019 +0100
@@ -31,7 +31,6 @@
 import java.io.OutputStream;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
-import java.lang.reflect.Field;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.ProtocolFamily;
@@ -44,9 +43,6 @@
 import java.net.StandardSocketOptions;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Objects;
@@ -701,10 +697,13 @@
 
     /**
      * Accepts a new connection so that the given SocketImpl is connected to
-     * the peer.
+     * the peer. The SocketImpl must be a newly created NioSocketImpl.
      */
     @Override
     protected void accept(SocketImpl si) throws IOException {
+        NioSocketImpl nsi = (NioSocketImpl) si;
+        assert !nsi.server && nsi.state == ST_NEW;
+
         FileDescriptor newfd = new FileDescriptor();
         InetSocketAddress[] isaa = new InetSocketAddress[1];
 
@@ -762,24 +761,14 @@
         }
 
         // set the fields
-        InetSocketAddress remoteAddress = isaa[0];
-        if (si instanceof NioSocketImpl) {
-            NioSocketImpl nsi = (NioSocketImpl) si;
-            synchronized (nsi.stateLock) {
-                nsi.fd = newfd;
-                nsi.stream = true;
-                nsi.closer = FileDescriptorCloser.create(nsi);
-                nsi.localport = localAddress.getPort();
-                nsi.address = remoteAddress.getAddress();
-                nsi.port = remoteAddress.getPort();
-                nsi.state = ST_CONNECTED;
-            }
-        } else {
-            // set fields in foreign impl
-            setSocketImplFields(si, newfd,
-                                localAddress.getPort(),
-                                remoteAddress.getAddress(),
-                                remoteAddress.getPort());
+        synchronized (nsi.stateLock) {
+            nsi.fd = newfd;
+            nsi.stream = true;
+            nsi.closer = FileDescriptorCloser.create(nsi);
+            nsi.localport = localAddress.getPort();
+            nsi.address = isaa[0].getAddress();
+            nsi.port = isaa[0].getPort();
+            nsi.state = ST_CONNECTED;
         }
     }
 
@@ -1267,35 +1256,4 @@
             return StandardProtocolFamily.INET;
         }
     }
-
-    /**
-     * Sets the SocketImpl fields to the given values.
-     */
-    private static void setSocketImplFields(SocketImpl si,
-                                            FileDescriptor fd,
-                                            int localport,
-                                            InetAddress address,
-                                            int port)
-    {
-        PrivilegedExceptionAction<Void> pa = () -> {
-            setSocketImplField(si, "fd", fd);
-            setSocketImplField(si, "localport", localport);
-            setSocketImplField(si, "address", address);
-            setSocketImplField(si, "port", port);
-            return null;
-        };
-        try {
-            AccessController.doPrivileged(pa);
-        } catch (PrivilegedActionException pae) {
-            throw new InternalError(pae);
-        }
-    }
-
-    private static void setSocketImplField(SocketImpl si, String name, Object value)
-        throws Exception
-    {
-        Field field = SocketImpl.class.getDeclaredField(name);
-        field.setAccessible(true);
-        field.set(si, value);
-    }
 }