diff -r 52bc64277201 -r d8ed7335dadd src/java.base/share/classes/java/net/ServerSocket.java --- a/src/java.base/share/classes/java/net/ServerSocket.java Sat Feb 09 08:54:02 2019 +0000 +++ b/src/java.base/share/classes/java/net/ServerSocket.java Sat Feb 09 19:16:30 2019 +0000 @@ -38,8 +38,6 @@ import java.util.Set; import java.util.Collections; import sun.nio.ch.NioSocketImpl; -import static java.net.SocketImpl.DelegatingImpl; -import static java.net.SocketImpl.getDefaultSocketImpl; /** * This class implements server sockets. A server socket waits for @@ -299,7 +297,7 @@ } else { // No need to do a checkOldImpl() here, we know it's an up to date // SocketImpl! - impl = new SocksSocketImpl(getDefaultSocketImpl(true)); + impl = SocketImpl.createDefaultSocketImpl(true); } if (impl != null) impl.setServerSocket(this); @@ -528,23 +526,6 @@ return s; } - static void prepareImpl(SocketImpl impl) { - if (!(impl instanceof DelegatingImpl)) - return; - impl = ((DelegatingImpl)impl).delegate(); - if (impl.address == null) - impl.address = new InetAddress(); - if (impl.fd == null) - impl.fd = new FileDescriptor(); - } - - static SocketImpl newInstanceOfType(SocketImpl impl1, SocketImpl impl2) { - if (impl1 instanceof DelegatingImpl) - return ((DelegatingImpl)impl1).newInstance(); - else - return ((DelegatingImpl)impl2).newInstance(); - } - /** * Subclasses of ServerSocket use this method to override accept() * to return their own subclass of socket. So a FooServerSocket @@ -569,14 +550,11 @@ if (si == null) { // create a SocketImpl and accept the connection si = Socket.createImpl(); - prepareImpl(si); - s.setImpl(si); impl.accept(si); - try { - // a custom impl has accepted the connection with a Platform SocketImpl - if (!(impl instanceof DelegatingImpl) && (si instanceof DelegatingImpl)) { - ((DelegatingImpl)si).postCustomAccept(); + // a custom impl has accepted the connection with a NIO SocketImpl + if ((si instanceof NioSocketImpl) && !(impl instanceof NioSocketImpl)) { + ((NioSocketImpl) si).postCustomAccept(); } } finally { securityCheckAccept(si); // closes si if permission check fails @@ -588,18 +566,31 @@ return; } - // ServerSocket or Socket is using NIO or Plain SocketImpl - if (impl instanceof DelegatingImpl || si instanceof DelegatingImpl) { + if (si instanceof SocketImpl.DelegatingImpl) + si = ((SocketImpl.DelegatingImpl) si).delegate(); + // ServerSocket or Socket is using NIO SocketImpl + if (impl instanceof NioSocketImpl || si instanceof NioSocketImpl) { // accept connection via new SocketImpl - - SocketImpl nsi = newInstanceOfType(impl, si); - prepareImpl(nsi); + NioSocketImpl nsi = new NioSocketImpl(false); impl.accept(nsi); securityCheckAccept(nsi); // closes si if permission check fails // copy state to the existing SocketImpl and update socket state - ((DelegatingImpl)nsi).copyTo(si); + nsi.copyTo(si); + s.postAccept(); + return; + } + + // ServerSocket or Socket is using PlainSocketImpl + if (impl instanceof PlainSocketImpl || si instanceof PlainSocketImpl) { + // accept connection via new SocketImpl + PlainSocketImpl psi = new PlainSocketImpl(); + impl.accept(psi); + securityCheckAccept(psi); // closes si if permission check fails + + // copy state to the existing SocketImpl and update socket state + psi.copyTo(si); s.postAccept(); return; } @@ -609,7 +600,8 @@ boolean completed = false; try { si.reset(); - prepareImpl(si); + si.fd = new FileDescriptor(); + si.address = new InetAddress(); impl.accept(si); securityCheckAccept(si); // closes si if permission check fails completed = true;