More cleanup of implAccept niosocketimpl-branch
authoralanb
Tue, 05 Mar 2019 10:02:36 +0000
branchniosocketimpl-branch
changeset 57242 c37938e150b7
parent 57240 f0e45a80ab8f
child 57248 48d523dfbdc9
More cleanup of implAccept
src/java.base/share/classes/java/net/ServerSocket.java
src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Mon Mar 04 08:13:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Tue Mar 05 10:02:36 2019 +0000
@@ -574,18 +574,13 @@
 
         // ServerSocket and Socket bound to custom SocketImpls
         s.impl = null; // break connection to impl
-        boolean completed = false;
+        si.reset();
         try {
+            implAccept(si);
+        } catch (Exception e) {
             si.reset();
-            // custom SocketImpl may expect fd/address to be created
-            si.fd = new FileDescriptor();
-            si.address = new InetAddress();
-            impl.accept(si);
-            securityCheckAccept(si);  // closes si if permission check fails
-            completed = true;
+            throw e;
         } finally {
-            if (!completed)
-                si.reset();
             s.impl = si;  // restore connection to impl
         }
         s.postAccept();
@@ -607,6 +602,12 @@
                 " cannot accept a connection with an instance of " + si.getClass());
         }
 
+        // custom SocketImpl may expect fd/address objects to be created
+        if (!(si instanceof PlatformSocketImpl)) {
+            si.fd = new FileDescriptor();
+            si.address = new InetAddress();
+        }
+
         // accept a connection
         impl.accept(si);
 
@@ -619,14 +620,7 @@
             }
         }
 
-        securityCheckAccept(si);  // closes si if permission check fails
-    }
-
-    /**
-     * Invokes the security manager's checkAccept method. If the permission
-     * check fails then it closes the SocketImpl.
-     */
-    private void securityCheckAccept(SocketImpl si) throws IOException {
+        // check permission, close SocketImpl/connection if denied
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             try {
--- a/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Mon Mar 04 08:13:31 2019 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Tue Mar 05 10:02:36 2019 +0000
@@ -151,6 +151,9 @@
      * Throws SocketException if the socket is not open.
      */
     private void ensureOpen() throws SocketException {
+        int state = this.state;
+        if (state == ST_NEW)
+            throw new SocketException("Socket not created");
         if (state >= ST_CLOSING)
             throw new SocketException("Socket closed");
     }