Merge niosocketimpl-branch
authoralanb
Sun, 17 Feb 2019 12:21:11 +0000
branchniosocketimpl-branch
changeset 57191 de9dd71ef18c
parent 57190 4964feb6d75d (diff)
parent 53785 d230a0406623 (current diff)
child 57199 88a41734ddbe
Merge
src/hotspot/share/gc/g1/dirtyCardQueue.cpp
src/hotspot/share/gc/g1/dirtyCardQueue.hpp
src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/output_html.properties
src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/output_text.properties
src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/output_unknown.properties
src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/output_xml.properties
test/hotspot/jtreg/serviceability/dcmd/framework/TEST.properties
test/jdk/sun/security/krb5/auto/rcache_usemd5.sh
test/jdk/sun/security/krb5/tools/ktarg.sh
test/jdk/sun/security/krb5/tools/ktcheck.sh
test/jdk/sun/security/krb5/tools/ktmissing.sh
test/jdk/sun/security/krb5/tools/ktzero.sh
test/jdk/sun/security/pkcs11/fips/CipherTest.java
test/jdk/sun/security/pkcs11/fips/ClientJSSEServerJSSE.java
test/jdk/sun/security/pkcs11/fips/ImportKeyStore.java
test/jdk/sun/security/pkcs11/fips/JSSEClient.java
test/jdk/sun/security/pkcs11/fips/JSSEServer.java
test/jdk/sun/security/pkcs11/fips/TestTLS12.java
test/jdk/sun/security/pkcs11/fips/TrustManagerTest.java
test/jdk/sun/security/pkcs11/fips/TrustManagerTest.policy
test/jdk/sun/security/pkcs11/fips/cert8.db
test/jdk/sun/security/pkcs11/fips/certs/anchor.cer
test/jdk/sun/security/pkcs11/fips/certs/ca.cer
test/jdk/sun/security/pkcs11/fips/certs/server.cer
test/jdk/sun/security/pkcs11/fips/fips.cfg
test/jdk/sun/security/pkcs11/fips/key3.db
test/jdk/sun/security/pkcs11/fips/keystore
test/jdk/sun/security/pkcs11/fips/secmod.db
test/jdk/sun/security/pkcs11/fips/truststore
--- a/src/java.base/share/classes/java/lang/System.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/lang/System.java	Sun Feb 17 12:21:11 2019 +0000
@@ -2172,6 +2172,12 @@
             public void blockedOn(Interruptible b) {
                 Thread.blockedOn(b);
             }
+            public void setNativeTid(long tid) {
+                Thread.setNativeTid(tid);
+            }
+            public long nativeTid() {
+                return Thread.nativeTid();
+            }
             public void registerShutdownHook(int slot, boolean registerShutdownInProgress, Runnable hook) {
                 Shutdown.add(slot, registerShutdownInProgress, hook);
             }
--- a/src/java.base/share/classes/java/lang/Thread.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/lang/Thread.java	Sun Feb 17 12:21:11 2019 +0000
@@ -241,6 +241,18 @@
     }
 
     /**
+     * Native thread id, cached here for use for threads are blocked in I/O
+     * operations.
+     */
+    private long nativeTid;
+    static void setNativeTid(long tid) {
+        Thread.currentThread().nativeTid = tid;
+    }
+    static long nativeTid() {
+        return Thread.currentThread().nativeTid;
+    }
+
+    /**
      * The minimum priority that a thread can have.
      */
     public static final int MIN_PRIORITY = 1;
--- a/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -30,12 +30,16 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
 import sun.net.ConnectionResetException;
 import sun.net.NetHooks;
+import sun.net.PlatformSocketImpl;
 import sun.net.ResourceManager;
 import sun.net.util.SocketExceptions;
 
@@ -46,7 +50,7 @@
  *
  * @author  Steven B. Byrne
  */
-abstract class AbstractPlainSocketImpl extends SocketImpl {
+abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl {
     /* instance variable for SO_TIMEOUT */
     int timeout;   // timeout in millisec
     // traffic class
@@ -450,15 +454,17 @@
 
     /**
      * Accepts connections.
-     * @param s the connection
+     * @param si the socket impl
      */
-    protected void accept(SocketImpl s) throws IOException {
+    protected void accept(SocketImpl si) throws IOException {
+        si.fd = new FileDescriptor();
         acquireFD();
         try {
-            socketAccept(s);
+            socketAccept(si);
         } finally {
             releaseFD();
         }
+        SocketCleanable.register(si.fd);
     }
 
     /**
@@ -470,8 +476,14 @@
                 throw new IOException("Socket Closed");
             if (shut_rd)
                 throw new IOException("Socket input is shutdown");
-            if (socketInputStream == null)
-                socketInputStream = new SocketInputStream(this);
+            if (socketInputStream == null) {
+                PrivilegedExceptionAction<SocketInputStream> pa = () -> new SocketInputStream(this);
+                try {
+                    socketInputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketInputStream;
     }
@@ -489,8 +501,14 @@
                 throw new IOException("Socket Closed");
             if (shut_wr)
                 throw new IOException("Socket output is shutdown");
-            if (socketOutputStream == null)
-                socketOutputStream = new SocketOutputStream(this);
+            if (socketOutputStream == null) {
+                PrivilegedExceptionAction<SocketOutputStream> pa = () -> new SocketOutputStream(this);
+                try {
+                    socketOutputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketOutputStream;
     }
@@ -714,6 +732,45 @@
         socketClose0(false);
     }
 
+
+    @Override
+    public void postCustomAccept() {
+        assert fd.valid() && localport != 0 && address != null && port != 0;
+        stream = true;
+
+        // assume the custom SocketImpl didn't register a cleaner
+        SocketCleanable.register(fd);
+    }
+
+    @Override
+    public void copyTo(SocketImpl si) {
+        if (si instanceof AbstractPlainSocketImpl) {
+            try {
+                si.close();
+            } catch (IOException ignore) { }
+
+            // this SocketImpl should be connected
+            assert fd.valid() && localport != 0 && address != null && port != 0;
+
+            AbstractPlainSocketImpl psi = (AbstractPlainSocketImpl) si;
+
+            // copy fields
+            psi.stream = this.stream;
+            psi.fd = this.fd;
+            psi.localport = this.localport;
+            psi.address = this.address;
+            psi.port = this.port;
+
+            // reset fields; do not reset timeout
+            psi.closePending = false;
+            psi.connectionReset = false;
+            psi.shut_rd = false;
+            psi.shut_wr = false;
+        } else {
+            throw new RuntimeException("not implemented");
+        }
+    }
+
     abstract void socketCreate(boolean isServer) throws IOException;
     abstract void socketConnect(InetAddress address, int port, int timeout)
         throws IOException;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/java/net/DelegatingSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.net;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Objects;
+import java.util.Set;
+
+import sun.net.PlatformSocketImpl;
+
+/**
+ * A SocketImpl that delegates all methods to another SocketImpl.
+ */
+
+class DelegatingSocketImpl extends SocketImpl {
+    protected final SocketImpl delegate;
+
+    DelegatingSocketImpl(SocketImpl delegate) {
+        assert delegate instanceof PlatformSocketImpl;
+        this.delegate = Objects.requireNonNull(delegate);
+    }
+
+    final SocketImpl delegate() {
+        return delegate;
+    }
+
+    @Override
+    protected FileDescriptor getFileDescriptor() {
+        return delegate.getFileDescriptor();
+    }
+
+    @Override
+    protected InetAddress getInetAddress() {
+        return delegate.getInetAddress();
+    }
+
+    @Override
+    protected int getPort() {
+        return delegate.getPort();
+    }
+
+    @Override
+    protected int getLocalPort() {
+        return delegate.getLocalPort();
+    }
+
+    @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
+    protected void connect(SocketAddress address, int timeout) throws IOException {
+        delegate.connect(address, timeout);
+    }
+
+    @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);
+    }
+
+    @Override
+    protected void accept(SocketImpl s) throws IOException {
+        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();
+    }
+
+    @Override
+    protected void close() throws IOException {
+        delegate.close();
+    }
+
+    @Override
+    protected boolean supportsUrgentData() {
+        return delegate.supportsUrgentData();
+    }
+
+    @Override
+    protected void sendUrgentData(int data) throws IOException {
+        delegate.sendUrgentData(data);
+    }
+
+    @Override
+    protected Set<SocketOption<?>> supportedOptions() {
+        return delegate.supportedOptions();
+    }
+
+    @Override
+    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
+        delegate.setOption(opt, value);
+    }
+
+    @Override
+    protected <T> T getOption(SocketOption<T> opt) throws IOException {
+        return delegate.getOption(opt);
+    }
+
+    @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 void shutdownInput() throws IOException {
+        delegate.shutdownInput();
+    }
+
+    @Override
+    protected void shutdownOutput() throws IOException {
+        delegate.shutdownOutput();
+    }
+}
--- a/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -25,13 +25,18 @@
 
 package java.net;
 
+import java.io.FileDescriptor;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
+import sun.nio.ch.NioSocketImpl;
+
 /**
  * Basic SocketImpl that relies on the internal HTTP protocol handler
  * implementation to perform the HTTP tunneling and authentication. The
@@ -41,7 +46,7 @@
  * @since 1.8
  */
 
-/*package*/ class HttpConnectSocketImpl extends PlainSocketImpl {
+/*package*/ class HttpConnectSocketImpl extends DelegatingSocketImpl {
 
     private static final String httpURLClazzStr =
                                   "sun.net.www.protocol.http.HttpURLConnection";
@@ -75,13 +80,14 @@
             throw new InternalError("Should not reach here", x);
         }
     }
-
-    HttpConnectSocketImpl(String server, int port) {
-        this.server = server;
-        this.port = port;
+    HttpConnectSocketImpl(SocketImpl delegate) {
+        super(delegate);
+        this.server = null;
+        throw new InternalError();
     }
 
-    HttpConnectSocketImpl(Proxy proxy) {
+    HttpConnectSocketImpl(Proxy proxy, SocketImpl delegate) {
+        super(delegate);
         SocketAddress a = proxy.address();
         if ( !(a instanceof InetSocketAddress) )
             throw new IllegalArgumentException("Unsupported address type");
@@ -92,6 +98,27 @@
     }
 
     @Override
+    protected void connect(String host, int port) throws IOException {
+        connect(new InetSocketAddress(host, port), 0);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        connect(new InetSocketAddress(address, port), 0);
+    }
+
+    @Override
+    void setSocket(Socket socket) {
+        delegate.socket = socket;
+        super.setSocket(socket);
+    }
+
+    @Override
+    void setServerSocket(ServerSocket socket) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
     protected void connect(SocketAddress endpoint, int timeout)
         throws IOException
     {
@@ -117,21 +144,37 @@
         close();
 
         // update the Sockets impl to the impl from the http Socket
-        AbstractPlainSocketImpl psi = (AbstractPlainSocketImpl) httpSocket.impl;
-        this.getSocket().impl = psi;
+        SocketImpl si = httpSocket.impl;
+        ((SocketImpl) this).getSocket().setImpl(si);
 
         // best effort is made to try and reset options previously set
         Set<Map.Entry<Integer,Object>> options = optionsMap.entrySet();
         try {
             for(Map.Entry<Integer,Object> entry : options) {
-                psi.setOption(entry.getKey(), entry.getValue());
+                si.setOption(entry.getKey(), entry.getValue());
             }
         } catch (IOException x) {  /* gulp! */  }
     }
 
+
+    @Override
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
+    void reset() throws IOException {
+        delegate.reset();
+    }
+
     @Override
     public void setOption(int opt, Object val) throws SocketException {
-        super.setOption(opt, val);
+        delegate.setOption(opt, val);
 
         if (external_address != null)
             return;  // we're connected, just return
@@ -163,7 +206,10 @@
         URL destURL = new URL(urlString);
         HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
         conn.setConnectTimeout(connectTimeout);
-        conn.setReadTimeout(this.timeout);
+        int timeout = (int) getOption(SocketOptions.SO_TIMEOUT);
+        if (timeout > 0) {
+            conn.setReadTimeout(timeout);
+        }
         conn.connect();
         doTunneling(conn);
         try {
@@ -187,7 +233,7 @@
         if (external_address != null)
             return external_address.getAddress();
         else
-            return super.getInetAddress();
+            return delegate.getInetAddress();
     }
 
     @Override
@@ -195,6 +241,6 @@
         if (external_address != null)
             return external_address.getPort();
         else
-            return super.getPort();
+            return delegate.getPort();
     }
 }
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Sun Feb 17 12:21:11 2019 +0000
@@ -25,9 +25,6 @@
 
 package java.net;
 
-import jdk.internal.access.JavaNetSocketAccess;
-import jdk.internal.access.SharedSecrets;
-
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.lang.reflect.Constructor;
@@ -38,6 +35,10 @@
 import java.util.Set;
 import java.util.Collections;
 
+import jdk.internal.access.JavaNetSocketAccess;
+import jdk.internal.access.SharedSecrets;
+import sun.net.PlatformSocketImpl;
+
 /**
  * This class implements server sockets. A server socket waits for
  * requests to come in over the network. It performs some operation
@@ -294,9 +295,7 @@
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = new SocksSocketImpl();
+            impl = SocketImpl.createPlatformSocketImpl(true);
         }
         if (impl != null)
             impl.setServerSocket(this);
@@ -542,41 +541,91 @@
      * @spec JSR-51
      */
     protected final void implAccept(Socket s) throws IOException {
-        SocketImpl si = null;
-        try {
-            if (s.impl == null)
-              s.setImpl();
-            else {
-                s.impl.reset();
+        SocketImpl impl = getImpl();
+        SocketImpl si = s.impl;
+
+        // Socket does not have a SocketImpl
+        if (si == null) {
+            // create a SocketImpl and accept the connection
+            si = Socket.createImpl();
+            assert !(si instanceof DelegatingSocketImpl);
+            impl.accept(si);
+            try {
+                // a custom impl has accepted the connection with a platform SocketImpl
+                if (!(impl instanceof PlatformSocketImpl) && (si instanceof PlatformSocketImpl)) {
+                    ((PlatformSocketImpl) si).postCustomAccept();
+                }
+            } finally {
+                securityCheckAccept(si);  // closes si if permission check fails
             }
-            si = s.impl;
-            s.impl = null;
-            si.address = new InetAddress();
-            si.fd = new FileDescriptor();
-            getImpl().accept(si);
-            SocketCleanable.register(si.fd);   // raw fd has been set
+
+            // bind Socket to the SocketImpl and update socket state
+            s.setImpl(si);
+            s.postAccept();
+            return;
+        }
+
+        // Socket has a SOCKS or HTTP SocketImpl
+        if (si instanceof DelegatingSocketImpl) {
+            si = ((DelegatingSocketImpl) si).delegate();
+            assert si instanceof PlatformSocketImpl;
+        }
 
-            SecurityManager security = System.getSecurityManager();
-            if (security != null) {
-                security.checkAccept(si.getInetAddress().getHostAddress(),
-                                     si.getPort());
+        // ServerSocket or Socket (or both) have a platform SocketImpl
+        if (impl instanceof PlatformSocketImpl || si instanceof PlatformSocketImpl) {
+            // create a new platform SocketImpl and accept the connection
+            var nsi = SocketImpl.createPlatformSocketImpl(false);
+            impl.accept(nsi);
+            try {
+                // a custom impl has accepted the connection
+                if (!(impl instanceof PlatformSocketImpl)) {
+                    nsi.postCustomAccept();
+                }
+            } finally {
+                securityCheckAccept(nsi);  // closes nsi if permission check fails
             }
-        } catch (IOException e) {
-            if (si != null)
+
+            // copy state to the existing SocketImpl and update socket state
+            nsi.copyTo(si);
+            s.postAccept();
+            return;
+        }
+
+        // ServerSocket and Socket bound to custom SocketImpls
+        s.impl = null; // break connection to impl
+        boolean completed = false;
+        try {
+            si.reset();
+            si.fd = new FileDescriptor();
+            si.address = new InetAddress();
+            impl.accept(si);
+            securityCheckAccept(si);  // closes si if permission check fails
+            completed = true;
+        } finally {
+            if (!completed)
                 si.reset();
-            s.impl = si;
-            throw e;
-        } catch (SecurityException e) {
-            if (si != null)
-                si.reset();
-            s.impl = si;
-            throw e;
+            s.impl = si;  // restore connection to impl
         }
-        s.impl = si;
         s.postAccept();
     }
 
     /**
+     * Invokes the security manager's checkAccept method. If the permission
+     * check fails then it closes the SocketImpl.
+     */
+    private void securityCheckAccept(SocketImpl si) throws IOException {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            try {
+                sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
+            } catch (SecurityException se) {
+                si.close();
+                throw se;
+            }
+        }
+    }
+
+    /**
      * Closes this socket.
      *
      * Any thread currently blocked in {@link #accept()} will throw
--- a/src/java.base/share/classes/java/net/Socket.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/Socket.java	Sun Feb 17 12:21:11 2019 +0000
@@ -28,9 +28,10 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
 import java.nio.channels.SocketChannel;
 import java.security.AccessController;
-import java.security.PrivilegedExceptionAction;
 import java.security.PrivilegedAction;
 import java.util.Set;
 import java.util.Collections;
@@ -76,6 +77,22 @@
     private boolean oldImpl = false;
 
     /**
+     * Socket input/output streams
+     */
+    private volatile InputStream in;
+    private volatile OutputStream out;
+    private static final VarHandle IN, OUT;
+    static {
+        try {
+            MethodHandles.Lookup l = MethodHandles.lookup();
+            IN = l.findVarHandle(Socket.class, "in", InputStream.class);
+            OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
+        } catch (Exception e) {
+            throw new InternalError(e);
+        }
+    }
+
+    /**
      * Creates an unconnected socket, with the
      * system-default type of SocketImpl.
      *
@@ -137,13 +154,15 @@
                     security.checkConnect(epoint.getAddress().getHostAddress(),
                                   epoint.getPort());
             }
-            impl = type == Proxy.Type.SOCKS ? new SocksSocketImpl(p)
-                                            : new HttpConnectSocketImpl(p);
+
+            SocketImpl si = SocketImpl.createPlatformSocketImpl(false);
+            impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, si)
+                                              : new HttpConnectSocketImpl(p, si);
             impl.setSocket(this);
         } else {
             if (p == Proxy.NO_PROXY) {
                 if (factory == null) {
-                    impl = new PlainSocketImpl();
+                    impl = SocketImpl.createPlatformSocketImpl(false);
                     impl.setSocket(this);
                 } else
                     setImpl();
@@ -491,6 +510,20 @@
         });
     }
 
+    static SocketImpl createImpl() {
+        SocketImplFactory factory = Socket.factory;
+        if (factory != null) {
+            return factory.createSocketImpl();
+        } else {
+            return SocketImpl.createPlatformSocketImpl(false);
+        }
+    }
+
+    void setImpl(SocketImpl si) {
+         impl = si;
+         impl.setSocket(this);
+    }
+
     /**
      * Sets impl to the system-default type of SocketImpl.
      * @since 1.4
@@ -500,15 +533,13 @@
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = new SocksSocketImpl();
+            SocketImpl si = SocketImpl.createPlatformSocketImpl(false);
+            impl = new SocksSocketImpl(si);
         }
         if (impl != null)
             impl.setSocket(this);
     }
 
-
     /**
      * Get the {@code SocketImpl} attached to this socket, creating
      * it if necessary.
@@ -907,18 +938,42 @@
             throw new SocketException("Socket is not connected");
         if (isInputShutdown())
             throw new SocketException("Socket input is shutdown");
-        InputStream is = null;
-        try {
-            is = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<>() {
-                    public InputStream run() throws IOException {
-                        return impl.getInputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
+        InputStream in = this.in;
+        if (in == null) {
+            // wrap the input stream so that the close method closes this socket
+            in = new SocketInputStream(this, impl.getInputStream());
+            if (!IN.compareAndSet(this, null, in)) {
+                in = this.in;
+            }
+        }
+        return in;
+    }
+
+    private static class SocketInputStream extends InputStream {
+        private final Socket parent;
+        private final InputStream in;
+        SocketInputStream(Socket parent, InputStream in) {
+            this.parent = parent;
+            this.in = in;
         }
-        return is;
+        @Override
+        public int read() throws IOException {
+            byte[] a = new byte[1];
+            int n = read(a, 0, 1);
+            return (n > 0) ? (a[0] & 0xff) : -1;
+        }
+        @Override
+        public int read(byte b[], int off, int len) throws IOException {
+            return in.read(b, off, len);
+        }
+        @Override
+        public int available() throws IOException {
+            return in.available();
+        }
+        @Override
+        public void close() throws IOException {
+            parent.close();
+        }
     }
 
     /**
@@ -946,18 +1001,37 @@
             throw new SocketException("Socket is not connected");
         if (isOutputShutdown())
             throw new SocketException("Socket output is shutdown");
-        OutputStream os = null;
-        try {
-            os = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<>() {
-                    public OutputStream run() throws IOException {
-                        return impl.getOutputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
+        OutputStream out = this.out;
+        if (out == null) {
+            // wrap the output stream so that the close method closes this socket
+            out = new SocketOutputStream(this, impl.getOutputStream());
+            if (!OUT.compareAndSet(this, null, out)) {
+                out = this.out;
+            }
         }
-        return os;
+        return out;
+    }
+
+    private static class SocketOutputStream extends OutputStream {
+        private final Socket parent;
+        private final OutputStream out;
+        SocketOutputStream(Socket parent, OutputStream out) {
+            this.parent = parent;
+            this.out = out;
+        }
+        @Override
+        public void write(int b) throws IOException {
+            byte[] a = new byte[] { (byte) b };
+            write(a, 0, 1);
+        }
+        @Override
+        public void write(byte b[], int off, int len) throws IOException {
+            out.write(b, off, len);
+        }
+        @Override
+        public void close() throws IOException {
+            parent.close();
+        }
     }
 
     /**
@@ -1381,9 +1455,8 @@
         try {
             getImpl().setOption(SocketOptions.IP_TOS, tc);
         } catch (SocketException se) {
-            // not supported if socket already connected
-            // Solaris returns error in such cases
-            if(!isConnected())
+            // may not be supported to change when socket is connected
+            if (!isConnected())
                 throw se;
         }
     }
@@ -1757,7 +1830,14 @@
      * @since 9
      */
     public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
-        getImpl().setOption(name, value);
+        try {
+            getImpl().setOption(name, value);
+        } catch (SocketException se) {
+            // may not be supported to change when socket is connected
+            if (name != StandardSocketOptions.IP_TOS || !isConnected()) {
+                throw se;
+            }
+        }
         return this;
     }
 
--- a/src/java.base/share/classes/java/net/SocketImpl.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/SocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -25,12 +25,18 @@
 
 package java.net;
 
+import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.FileDescriptor;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.Set;
 
+import sun.net.NetProperties;
+import sun.net.PlatformSocketImpl;
+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
@@ -43,6 +49,26 @@
  * @since   1.0
  */
 public abstract class SocketImpl implements SocketOptions {
+    private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl();
+
+    private static boolean usePlainSocketImpl() {
+        PrivilegedAction<String> pa = () -> NetProperties.get("jdk.net.usePlainSocketImpl");
+        String s = AccessController.doPrivileged(pa);
+        return (s != null) && !s.equalsIgnoreCase("false");
+    }
+
+    /**
+     * Creates a instance of platform's SocketImpl
+     */
+    @SuppressWarnings("unchecked")
+    static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) {
+        if (USE_PLAINSOCKETIMPL) {
+            return (S) new PlainSocketImpl();
+        } else {
+            return (S) new NioSocketImpl(server);
+        }
+    }
+
     /**
      * The actual Socket object.
      */
--- a/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -23,24 +23,28 @@
  * questions.
  */
 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;
 import sun.net.spi.DefaultProxySelector;
 import sun.net.www.ParseUtil;
+import sun.nio.ch.NioSocketImpl;
 
 /**
  * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
- * This is a subclass of PlainSocketImpl.
  * Note this class should <b>NOT</b> be public.
  */
 
-class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
+class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
     private String server = null;
     private int serverPort = DEFAULT_PORT;
     private InetSocketAddress external_address;
@@ -49,11 +53,12 @@
     private InputStream cmdIn = null;
     private OutputStream cmdOut = null;
 
-    SocksSocketImpl() {
-        // Nothing needed
+    SocksSocketImpl(SocketImpl delegate) {
+        super(delegate);
     }
 
-    SocksSocketImpl(Proxy proxy) {
+    SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
+        super(delegate);
         SocketAddress a = proxy.address();
         if (a instanceof InetSocketAddress) {
             InetSocketAddress ad = (InetSocketAddress) a;
@@ -75,7 +80,7 @@
     private synchronized void privilegedConnect(final String host,
                                               final int port,
                                               final int timeout)
-         throws IOException
+        throws IOException
     {
         try {
             AccessController.doPrivileged(
@@ -94,7 +99,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 {
@@ -111,16 +116,23 @@
     private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException {
         int len = data.length;
         int received = 0;
-        while (received < len) {
-            int count;
-            try {
-                count = ((SocketInputStream)in).read(data, received, len - received, remainingMillis(deadlineMillis));
-            } catch (SocketTimeoutException e) {
-                throw new SocketTimeoutException("Connect timed out");
+        int originalTimeout = (int) getOption(SocketOptions.SO_TIMEOUT);
+        try {
+            while (received < len) {
+                int count;
+                int remaining = remainingMillis(deadlineMillis);
+                setOption(SocketOptions.SO_TIMEOUT, remaining);
+                try {
+                    count = in.read(data, received, len - received);
+                } catch (SocketTimeoutException e) {
+                    throw new SocketTimeoutException("Connect timed out");
+                }
+                if (count < 0)
+                    throw new SocketException("Malformed reply from SOCKS server");
+                received += count;
             }
-            if (count < 0)
-                throw new SocketException("Malformed reply from SOCKS server");
-            received += count;
+        } finally {
+            setOption(SocketOptions.SO_TIMEOUT, originalTimeout);
         }
         return received;
     }
@@ -239,6 +251,27 @@
         }
     }
 
+    @Override
+    protected void connect(String host, int port) throws IOException {
+        connect(new InetSocketAddress(host, port), 0);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        connect(new InetSocketAddress(address, port), 0);
+    }
+
+    @Override
+    void setSocket(Socket soc) {
+        delegate.socket = soc;
+        super.setSocket(soc);
+    }
+
+    @Override
+    void setServerSocket(ServerSocket soc) {
+        throw new InternalError("should not get here");
+    }
+
     /**
      * Connects the Socks Socket to the specified endpoint. It will first
      * connect to the SOCKS proxy and negotiate the access. If the proxy
@@ -290,7 +323,7 @@
                 /*
                  * No default proxySelector --> direct connection
                  */
-                super.connect(epoint, remainingMillis(deadlineMillis));
+                delegate.connect(epoint, remainingMillis(deadlineMillis));
                 return;
             }
             URI uri;
@@ -313,13 +346,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;
                 }
 
@@ -509,7 +542,15 @@
         external_address = epoint;
     }
 
+    @Override
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
+    }
 
+    @Override
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
+    }
 
     /**
      * Returns the value of this socket's {@code address} field.
@@ -522,7 +563,7 @@
         if (external_address != null)
             return external_address.getAddress();
         else
-            return super.getInetAddress();
+            return delegate.getInetAddress();
     }
 
     /**
@@ -536,7 +577,7 @@
         if (external_address != null)
             return external_address.getPort();
         else
-            return super.getPort();
+            return delegate.getPort();
     }
 
     @Override
@@ -544,10 +585,15 @@
         if (cmdsock != null)
             cmdsock.close();
         cmdsock = null;
-        super.close();
+        delegate.close();
     }
 
     private String getUserName() {
         return StaticProperty.userName();
     }
+
+    @Override
+    void reset() throws IOException {
+        delegate.reset();
+    }
 }
--- a/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/jdk/internal/access/JavaLangAccess.java	Sun Feb 17 12:21:11 2019 +0000
@@ -107,6 +107,16 @@
     void blockedOn(Interruptible b);
 
     /**
+     * Set the current thread's native ID
+     */
+    void setNativeTid(long tid);
+
+    /**
+     * Returns the current thread's native ID
+     */
+    long nativeTid();
+
+    /**
      * Registers a shutdown hook.
      *
      * It is expected that this method with registerShutdownInProgress=true
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/sun/net/PlatformSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package sun.net;
+
+import java.io.IOException;
+import java.net.SocketImpl;
+
+/**
+ * Implemented by the platform's SocketImpl implementations.
+ */
+
+public interface PlatformSocketImpl {
+
+    /**
+     * Invoked by ServerSocket to fix up the SocketImpl state after a connection
+     * is accepted by a custom SocketImpl
+     */
+    void postCustomAccept() throws IOException;
+
+    /**
+     * Copy the state from this connected SocketImpl to a target SocketImpl. If
+     * the target SocketImpl is not a newly created SocketImpl then it is first
+     * closed to release any resources. The target SocketImpl becomes the owner
+     * of the file descriptor, this SocketImpl is marked as closed and should
+     * be discarded.
+     */
+    void copyTo(SocketImpl si);
+}
--- a/src/java.base/share/classes/sun/nio/ch/IOStatus.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/IOStatus.java	Sun Feb 17 12:21:11 2019 +0000
@@ -81,4 +81,12 @@
         return ((n > EOF) || (n < UNSUPPORTED_CASE));
     }
 
+    /**
+     * Returns true if the error code is UNAVAILABLE or INTERRUPTED, the
+     * error codes to indicate that an I/O operation can be retried.
+     */
+    static boolean okayToRetry(long n) {
+        return (n == IOStatus.UNAVAILABLE) || (n == IOStatus.INTERRUPTED);
+    }
+
 }
--- a/src/java.base/share/classes/sun/nio/ch/Net.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/Net.java	Sun Feb 17 12:21:11 2019 +0000
@@ -310,6 +310,12 @@
     static final ExtendedSocketOptions extendedOptions =
             ExtendedSocketOptions.getInstance();
 
+    static void setSocketOption(FileDescriptor fd, SocketOption<?> name, Object value)
+        throws IOException
+    {
+        setSocketOption(fd, Net.UNSPEC, name, value);
+    }
+
     static void setSocketOption(FileDescriptor fd, ProtocolFamily family,
                                 SocketOption<?> name, Object value)
         throws IOException
@@ -372,8 +378,13 @@
         setIntOption0(fd, mayNeedConversion, key.level(), key.name(), arg, isIPv6);
     }
 
-    static Object getSocketOption(FileDescriptor fd, ProtocolFamily family,
-                                  SocketOption<?> name)
+    static Object getSocketOption(FileDescriptor fd, SocketOption<?> name)
+        throws IOException
+    {
+        return getSocketOption(fd, Net.UNSPEC, name);
+    }
+
+    static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, SocketOption<?> name)
         throws IOException
     {
         Class<?> type = name.type();
@@ -426,8 +437,7 @@
         return socket(UNSPEC, stream);
     }
 
-    static FileDescriptor socket(ProtocolFamily family, boolean stream)
-        throws IOException {
+    static FileDescriptor socket(ProtocolFamily family, boolean stream) throws IOException {
         boolean preferIPv6 = isIPv6Available() &&
             (family != StandardProtocolFamily.INET);
         return IOUtil.newFD(socket0(preferIPv6, stream, false, fastLoopback));
@@ -524,6 +534,10 @@
     static native int poll(FileDescriptor fd, int events, long timeout)
         throws IOException;
 
+    static int pollNow(FileDescriptor fd, int events) throws IOException {
+        return poll(fd, events, 0);
+    }
+
     /**
      * Polls a connecting socket to test if the connection has been established.
      *
@@ -535,6 +549,10 @@
     public static native int pollConnect(FileDescriptor fd, long timeout)
         throws IOException;
 
+    static int pollConnectNow(FileDescriptor fd) throws IOException {
+        return pollConnect(fd, 0);
+    }
+
     /**
      * Return the number of bytes in the socket input buffer.
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -0,0 +1,1271 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.nio.ch;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+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;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketImpl;
+import java.net.SocketOption;
+import java.net.SocketTimeoutException;
+import java.net.StandardProtocolFamily;
+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;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+
+import jdk.internal.access.SharedSecrets;
+import jdk.internal.ref.CleanerFactory;
+import sun.net.ConnectionResetException;
+import sun.net.NetHooks;
+import sun.net.PlatformSocketImpl;
+import sun.net.ResourceManager;
+import sun.net.ext.ExtendedSocketOptions;
+import sun.net.util.SocketExceptions;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.concurrent.TimeUnit.NANOSECONDS;
+
+/**
+ * NIO based SocketImpl.
+ *
+ * This implementation attempts to be compatible with legacy PlainSocketImpl,
+ * including behavior and exceptions that are not specified by SocketImpl.
+ *
+ * The underlying socket used by this SocketImpl is initially configured
+ * blocking. If a connect, accept or read is attempted with a timeout then the
+ * socket is changed to non-blocking mode. When in non-blocking mode, operations
+ * that don't complete immediately will poll the socket.
+ */
+
+public final class NioSocketImpl extends SocketImpl implements PlatformSocketImpl {
+    private static final NativeDispatcher nd = new SocketDispatcher(true);
+
+    // The maximum number of bytes to read/write per syscall to avoid needing
+    // a huge buffer from the temporary buffer cache
+    private static final int MAX_BUFFER_SIZE = 128 * 1024;
+
+    // true if this is a SocketImpl for a ServerSocket
+    private final boolean server;
+
+    // Lock held when reading (also used when accepting or connecting)
+    private final ReentrantLock readLock = new ReentrantLock();
+
+    // Lock held when writing
+    private final ReentrantLock writeLock = new ReentrantLock();
+
+    // The stateLock for read/changing state
+    private final Object stateLock = new Object();
+    private static final int ST_NEW = 0;
+    private static final int ST_UNCONNECTED = 1;
+    private static final int ST_CONNECTING = 2;
+    private static final int ST_CONNECTED = 3;
+    private static final int ST_CLOSING = 4;
+    private static final int ST_CLOSED = 5;
+    private volatile int state;  // need stateLock to change
+
+    // set by SocketImpl.create, protected by stateLock
+    private boolean stream;
+    private FileDescriptorCloser closer;
+
+    // lazily set to true when the socket is configured non-blocking
+    private volatile boolean nonBlocking;
+
+    // used by connect/read/write/accept, protected by stateLock
+    private long readerThread;
+    private long writerThread;
+
+    // used when SO_REUSEADDR is emulated
+    private boolean isReuseAddress;
+
+    // cached value of IPV6_TCLASS or IP_TOS socket option
+    private int trafficClass;
+
+    // read or accept timeout in millis
+    private volatile int timeout;
+
+    // flags to indicate if the connection is shutdown for input and output
+    private volatile boolean isInputClosed;
+    private volatile boolean isOutputClosed;
+
+    /**
+     * Creates a instance of this SocketImpl.
+     * @param server true if this is a SocketImpl for a ServerSocket
+     */
+    public NioSocketImpl(boolean server) {
+        this.server = server;
+    }
+
+    /**
+     * Returns true if the socket is open.
+     */
+    private boolean isOpen() {
+        return state < ST_CLOSING;
+    }
+
+    /**
+     * Throws SocketException if the socket is not open.
+     */
+    private void ensureOpen() throws SocketException {
+        if (state >= ST_CLOSING)
+            throw new SocketException("Socket closed");
+    }
+
+    /**
+     * Throws SocketException if the socket is not open and connected.
+     */
+    private void ensureOpenAndConnected() throws SocketException {
+        int state = this.state;
+        if (state < ST_CONNECTED)
+            throw new SocketException("Not connected");
+        if (state > ST_CONNECTED)
+            throw new SocketException("Socket closed");
+    }
+
+    /**
+     * Disables the current thread for scheduling purposes until the socket is
+     * ready for I/O, or is asynchronously closed, for up to the specified
+     * waiting time.
+     * @throws IOException if an I/O error occurs
+     */
+    private void park(FileDescriptor fd, int event, long nanos) throws IOException {
+        long millis;
+        if (nanos == 0) {
+            millis = -1;
+        } else {
+            millis = MILLISECONDS.convert(nanos, NANOSECONDS);
+        }
+        Net.poll(fd, event, millis);
+    }
+
+    /**
+     * Disables the current thread for scheduling purposes until the socket is
+     * ready for I/O or is asynchronously closed.
+     * @throws IOException if an I/O error occurs
+     */
+    private void park(FileDescriptor fd, int event) throws IOException {
+        park(fd, event, 0);
+    }
+
+    /**
+     * Ensures that the socket is configured non-blocking when a timeout is specified.
+     * @throws IOException if there is an I/O error changing the blocking mode
+     */
+    private void configureNonBlockingIfNeeded(FileDescriptor fd, int timeout)
+        throws IOException
+    {
+        if (timeout > 0 && !nonBlocking) {
+            assert readLock.isHeldByCurrentThread() || writeLock.isHeldByCurrentThread();
+            IOUtil.configureBlocking(fd, false);
+            nonBlocking = true;
+        }
+    }
+
+    /**
+     * Marks the beginning of a read operation that might block.
+     * @throws SocketException if the socket is closed or not connected
+     */
+    private FileDescriptor beginRead() throws SocketException {
+        synchronized (stateLock) {
+            ensureOpenAndConnected();
+            readerThread = NativeThread.current();
+            return fd;
+        }
+    }
+
+    /**
+     * Marks the end of a read operation that may have blocked.
+     * @throws SocketException is the socket is closed
+     */
+    private void endRead(boolean completed) throws SocketException {
+        synchronized (stateLock) {
+            readerThread = 0;
+            int state = this.state;
+            if (state == ST_CLOSING)
+                stateLock.notifyAll();
+            if (!completed && state >= ST_CLOSING)
+                throw new SocketException("Socket closed");
+        }
+    }
+
+    /**
+     * Try to read bytes from the socket into the given byte array.
+     */
+    private int tryRead(FileDescriptor fd, byte[] b, int off, int len)
+        throws IOException
+    {
+        ByteBuffer dst = Util.getTemporaryDirectBuffer(len);
+        assert dst.position() == 0;
+        try {
+            int n = nd.read(fd, ((DirectBuffer)dst).address(), len);
+            if (n > 0) {
+                dst.get(b, off, n);
+            }
+            return n;
+        } finally{
+            Util.offerFirstTemporaryDirectBuffer(dst);
+        }
+    }
+
+    /**
+     * Reads bytes from the socket into the given byte array.
+     * @return the number of bytes read
+     * @throws IOException if the socket is closed or an I/O occurs
+     * @throws SocketTimeoutException if the read timeout elapses
+     */
+    private int read(byte[] b, int off, int len) throws IOException {
+        readLock.lock();
+        try {
+            int timeout = this.timeout;
+            int n = 0;
+            FileDescriptor fd = beginRead();
+            try {
+                if (isInputClosed)
+                    return IOStatus.EOF;
+                configureNonBlockingIfNeeded(fd, timeout);
+                n = tryRead(fd, b, off, len);
+                if (IOStatus.okayToRetry(n) && isOpen()) {
+                    if (timeout > 0) {
+                        // read with timeout
+                        long nanos = NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+                        do {
+                            long startTime = System.nanoTime();
+                            park(fd, Net.POLLIN, nanos);
+                            n = tryRead(fd, b, off, len);
+                            if (n == IOStatus.UNAVAILABLE) {
+                                nanos -= System.nanoTime() - startTime;
+                                if (nanos <= 0)
+                                    throw new SocketTimeoutException("Read timed out");
+                            }
+                        } while (n == IOStatus.UNAVAILABLE && isOpen());
+                    } else {
+                        // read, no timeout
+                        do {
+                            park(fd, Net.POLLIN);
+                            n = tryRead(fd, b, off, len);
+                        } while (IOStatus.okayToRetry(n) && isOpen());
+                    }
+                }
+                return n;
+            } finally {
+                endRead(n > 0);
+            }
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
+     * Marks the beginning of a write operation that might block.
+     * @throws SocketException if the socket is closed or not connected
+     */
+    private FileDescriptor beginWrite() throws SocketException {
+        synchronized (stateLock) {
+            ensureOpenAndConnected();
+            writerThread = NativeThread.current();
+            return fd;
+        }
+    }
+
+    /**
+     * Marks the end of a write operation that may have blocked.
+     * @throws SocketException is the socket is closed
+     */
+    private void endWrite(boolean completed) throws SocketException {
+        synchronized (stateLock) {
+            writerThread = 0;
+            int state = this.state;
+            if (state == ST_CLOSING)
+                stateLock.notifyAll();
+            if (!completed && state >= ST_CLOSING)
+                throw new SocketException("Socket closed");
+        }
+    }
+
+    /**
+     * Try to write a sequence of bytes to this socket from the given byte array
+     */
+    private int tryWrite(FileDescriptor fd, byte[] b, int off, int len)
+        throws IOException
+    {
+        ByteBuffer src = Util.getTemporaryDirectBuffer(len);
+        assert src.position() == 0;
+        try {
+            src.put(b, off, len);
+            return nd.write(fd, ((DirectBuffer)src).address(), len);
+        } finally {
+            Util.offerFirstTemporaryDirectBuffer(src);
+        }
+    }
+
+    /**
+     * Writes a sequence of bytes to this socket from the given byte array.
+     * @return the number of bytes written
+     * @throws IOException if the socket is closed or an I/O occurs
+     */
+    private int write(byte[] b, int off, int len) throws IOException {
+        writeLock.lock();
+        try {
+            int n = 0;
+            FileDescriptor fd = beginWrite();
+            try {
+                n = tryWrite(fd, b, off, len);
+                while (IOStatus.okayToRetry(n) && isOpen()) {
+                    park(fd, Net.POLLOUT);
+                    n = tryWrite(fd, b, off, len);
+                }
+                return n;
+            } finally {
+                endWrite(n > 0);
+            }
+        } finally {
+            writeLock.unlock();
+        }
+    }
+
+    /**
+     * Creates the socket.
+     * @param stream {@code true} for a streams socket
+     */
+    @Override
+    protected void create(boolean stream) throws IOException {
+        synchronized (stateLock) {
+            assert state == ST_NEW;
+            if (!stream)
+                ResourceManager.beforeUdpCreate();
+            FileDescriptor fd;
+            try {
+                if (server) {
+                    assert stream;
+                    fd = Net.serverSocket(true);
+                } else {
+                    fd = Net.socket(stream);
+                }
+            } catch (IOException ioe) {
+                if (!stream)
+                    ResourceManager.afterUdpClose();
+                throw ioe;
+            }
+            this.fd = fd;
+            this.stream = stream;
+            this.closer = FileDescriptorCloser.create(this);
+            this.state = ST_UNCONNECTED;
+        }
+    }
+
+    /**
+     * For use by ServerSocket to set the state and other fields after a
+     * connection is accepted by a ServerSocket using a custom SocketImpl.
+     * The protected fields defined by SocketImpl should be set.
+     */
+    @Override
+    public void postCustomAccept() throws IOException {
+        synchronized (stateLock) {
+            assert state == ST_NEW;
+            assert fd.valid() && localport != 0 && address != null && port != 0;
+            IOUtil.configureBlocking(fd, true);
+            stream = true;
+            closer = FileDescriptorCloser.create(this);
+            state = ST_CONNECTED;
+        }
+    }
+
+    /**
+     * For use by ServerSocket to copy the state from this connected SocketImpl
+     * to a target SocketImpl. If the target SocketImpl is not a newly created
+     * SocketImpl then it is first closed to release any resources. The target
+     * SocketImpl becomes the owner of the file descriptor, this SocketImpl
+     * is marked as closed and should be discarded.
+     */
+    @Override
+    public void copyTo(SocketImpl si) {
+        if (si instanceof NioSocketImpl) {
+            NioSocketImpl nsi = (NioSocketImpl) si;
+            if (nsi.state != ST_NEW) {
+                try {
+                    nsi.close();
+                } catch (IOException ignore) { }
+            }
+            synchronized (nsi.stateLock) {
+                assert nsi.state == ST_NEW || nsi.state == ST_CLOSED;
+                synchronized (this.stateLock) {
+                    // this SocketImpl should be connected
+                    assert state == ST_CONNECTED && fd.valid()
+                        && localport != 0 && address != null && port != 0;
+
+                    // copy fields
+                    nsi.stream = this.stream;
+                    nsi.fd = this.fd;
+                    nsi.localport = this.localport;
+                    nsi.address = this.address;
+                    nsi.port = this.port;
+
+                    // reset fields; do not reset timeout
+                    nsi.nonBlocking = false;
+                    nsi.isInputClosed = false;
+                    nsi.isOutputClosed = false;
+                    nsi.isReuseAddress = false;
+                    nsi.state = ST_CONNECTED;
+
+                    // GC'ing of this impl should not close the file descriptor
+                    this.closer.disable();
+                    this.state = ST_CLOSED;
+
+                    // create new closer to execute when nsi is GC'ed
+                    nsi.closer = FileDescriptorCloser.create(nsi);
+                }
+            }
+        } else {
+            synchronized (this.stateLock) {
+                // this SocketImpl should be connected
+                assert state == ST_CONNECTED && fd.valid()
+                        && localport != 0 && address != null && port != 0;
+
+                // set fields in foreign impl
+                setSocketImplFields(si, fd, localport, address, port);
+
+                // disable closer to prevent GC'ing of this impl from
+                // closing the file descriptor
+                this.closer.disable();
+                this.state = ST_CLOSED;
+            }
+        }
+    }
+
+    /**
+     * Marks the beginning of a connect operation that might block.
+     * @throws SocketException if the socket is closed or already connected
+     */
+    private FileDescriptor beginConnect(InetAddress address, int port)
+        throws IOException
+    {
+        synchronized (stateLock) {
+            int state = this.state;
+            if (state != ST_UNCONNECTED) {
+                if (state == ST_CONNECTING)
+                    throw new SocketException("Connection in progress");
+                if (state == ST_CONNECTED)
+                    throw new SocketException("Already connected");
+                if (state >= ST_CLOSING)
+                    throw new SocketException("Socket closed");
+                assert false;
+            }
+            this.state = ST_CONNECTING;
+
+            // invoke beforeTcpConnect hook if not already bound
+            if (localport == 0) {
+                NetHooks.beforeTcpConnect(fd, address, port);
+            }
+
+            // save the remote address/port
+            this.address = address;
+            this.port = port;
+
+            readerThread = NativeThread.current();
+            return fd;
+        }
+    }
+
+    /**
+     * Marks the end of a connect operation that may have blocked.
+     * @throws SocketException is the socket is closed
+     */
+    private void endConnect(boolean completed) throws IOException {
+        synchronized (stateLock) {
+            readerThread = 0;
+            int state = this.state;
+            if (state == ST_CLOSING)
+                stateLock.notifyAll();
+            if (completed && state == ST_CONNECTING) {
+                this.state = ST_CONNECTED;
+                localport = Net.localAddress(fd).getPort();
+            } else if (!completed && state >= ST_CLOSING) {
+                throw new SocketException("Socket closed");
+            }
+        }
+    }
+
+    /**
+     * Connect the socket. Closes the socket if connection cannot be established.
+     * @throws IllegalArgumentException if the address is not an InetSocketAddress
+     * @throws UnknownHostException if the InetSocketAddress is not resolved
+     * @throws IOException if the connection cannot be established
+     */
+    private void implConnect(SocketAddress remote, int millis) throws IOException {
+        if (!(remote instanceof InetSocketAddress))
+            throw new IllegalArgumentException("Unsupported address type");
+        InetSocketAddress isa = (InetSocketAddress) remote;
+        if (isa.isUnresolved()) {
+            throw new UnknownHostException(isa.getHostName());
+        }
+
+        InetAddress address = isa.getAddress();
+        if (address.isAnyLocalAddress())
+            address = InetAddress.getLocalHost();
+        int port = isa.getPort();
+
+        ReentrantLock connectLock = readLock;
+        try {
+            connectLock.lock();
+            try {
+                boolean connected = false;
+                FileDescriptor fd = beginConnect(address, port);
+                try {
+                    configureNonBlockingIfNeeded(fd, millis);
+                    int n = Net.connect(fd, address, port);
+                    if (IOStatus.okayToRetry(n) && isOpen()) {
+                        if (millis > 0) {
+                            // connect with timeout
+                            assert nonBlocking;
+                            long nanos = NANOSECONDS.convert(millis, MILLISECONDS);
+                            do {
+                                long startTime = System.nanoTime();
+                                park(fd, Net.POLLOUT, nanos);
+                                n = Net.pollConnectNow(fd);
+                                if (n == 0) {
+                                    nanos -= System.nanoTime() - startTime;
+                                    if (nanos <= 0)
+                                        throw new SocketTimeoutException("Connect timed out");
+                                }
+                            } while (n == 0 && isOpen());
+                        } else {
+                            // connect, no timeout
+                            do {
+                                park(fd, Net.POLLOUT);
+                                n = Net.pollConnectNow(fd);
+                            } while (n == 0 && isOpen());
+                        }
+                    }
+                    connected = (n > 0) && isOpen();
+                } finally {
+                    endConnect(connected);
+                }
+            } finally {
+                connectLock.unlock();
+            }
+        } catch (IOException ioe) {
+            close();
+            throw SocketExceptions.of(ioe, isa);
+        }
+    }
+
+    @Override
+    protected void connect(String host, int port) throws IOException {
+        implConnect(new InetSocketAddress(host, port), timeout);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        implConnect(new InetSocketAddress(address, port), timeout);
+    }
+
+    @Override
+    protected void connect(SocketAddress address, int timeout) throws IOException {
+        implConnect(address, timeout);
+    }
+
+    @Override
+    protected void bind(InetAddress host, int port) throws IOException {
+        synchronized (stateLock) {
+            ensureOpen();
+            if (localport != 0)
+                throw new SocketException("Already bound");
+            NetHooks.beforeTcpBind(fd, host, port);
+            Net.bind(fd, host, port);
+            // set the address field to the address specified to the method to
+            // keep compatibility with PlainSocketImpl. When binding to 0.0.0.0
+            // then the actual local address will be ::0 when IPv6 is enabled.
+            address = host;
+            localport = Net.localAddress(fd).getPort();
+        }
+    }
+
+    @Override
+    protected void listen(int backlog) throws IOException {
+        synchronized (stateLock) {
+            ensureOpen();
+            if (localport == 0)
+                throw new SocketException("Not bound");
+            Net.listen(fd, backlog < 1 ? 50 : backlog);
+        }
+    }
+
+    /**
+     * Marks the beginning of an accept operation that might block.
+     * @throws SocketException if the socket is closed
+     */
+    private FileDescriptor beginAccept() throws SocketException {
+        synchronized (stateLock) {
+            ensureOpen();
+            if (!stream)
+                throw new SocketException("Not a stream socket");
+            if (localport == 0)
+                throw new SocketException("Not bound");
+            readerThread = NativeThread.current();
+            return fd;
+        }
+    }
+
+    /**
+     * Marks the end of an accept operation that may have blocked.
+     * @throws SocketException is the socket is closed
+     */
+    private void endAccept(boolean completed) throws SocketException {
+        synchronized (stateLock) {
+            int state = this.state;
+            readerThread = 0;
+            if (state == ST_CLOSING)
+                stateLock.notifyAll();
+            if (!completed && state >= ST_CLOSING)
+                throw new SocketException("Socket closed");
+        }
+    }
+
+    @Override
+    protected void accept(SocketImpl si) throws IOException {
+        // accept a connection
+        FileDescriptor newfd = new FileDescriptor();
+        InetSocketAddress[] isaa = new InetSocketAddress[1];
+
+        ReentrantLock acceptLock = readLock;
+        acceptLock.lock();
+        try {
+            int n = 0;
+            FileDescriptor fd = beginAccept();
+            try {
+                int timeout = this.timeout;
+                configureNonBlockingIfNeeded(fd, timeout);
+                n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
+                if (IOStatus.okayToRetry(n) && isOpen()) {
+                    if (timeout > 0) {
+                        // accept with timeout
+                        assert nonBlocking;
+                        long nanos = NANOSECONDS.convert(timeout, TimeUnit.MILLISECONDS);
+                        do {
+                            long startTime = System.nanoTime();
+                            park(fd, Net.POLLIN, nanos);
+                            n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
+                            if (n == IOStatus.UNAVAILABLE) {
+                                nanos -= System.nanoTime() - startTime;
+                                if (nanos <= 0)
+                                    throw new SocketTimeoutException("Accept timed out");
+                            }
+                        } while (n == IOStatus.UNAVAILABLE && isOpen());
+                    } else {
+                        // accept, no timeout
+                        do {
+                            park(fd, Net.POLLIN);
+                            n = ServerSocketChannelImpl.accept0(fd, newfd, isaa);
+                        } while (IOStatus.okayToRetry(n) && isOpen());
+                    }
+                }
+            } finally {
+                endAccept(n > 0);
+                assert IOStatus.check(n);
+            }
+        } finally {
+            acceptLock.unlock();
+        }
+
+        // get local address and configure accepted socket to blocking mode
+        InetSocketAddress localAddress;
+        try {
+            localAddress = Net.localAddress(newfd);
+            IOUtil.configureBlocking(newfd, true);
+        } catch (IOException ioe) {
+            nd.close(newfd);
+            throw ioe;
+        }
+
+        // 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());
+        }
+    }
+
+    @Override
+    protected InputStream getInputStream() {
+        return new InputStream() {
+            // EOF or connection reset detected, not thread safe
+            private boolean eof, reset;
+            @Override
+            public int read() throws IOException {
+                byte[] a = new byte[1];
+                int n = read(a, 0, 1);
+                return (n > 0) ? (a[0] & 0xff) : -1;
+            }
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                Objects.checkFromIndexSize(off, len, b.length);
+                if (eof) {
+                    return -1;
+                } else if (reset) {
+                    throw new SocketException("Connection reset");
+                } else if (len == 0) {
+                    return 0;
+                } else {
+                    try {
+                        // read up to MAX_BUFFER_SIZE bytes
+                        int size = Math.min(len, MAX_BUFFER_SIZE);
+                        int n = NioSocketImpl.this.read(b, off, size);
+                        if (n == -1)
+                            eof = true;
+                        return n;
+                    } catch (ConnectionResetException e) {
+                        reset = true;
+                        throw new SocketException("Connection reset");
+                    } catch (SocketTimeoutException e) {
+                        throw e;
+                    } catch (IOException ioe) {
+                        throw new SocketException(ioe.getMessage());
+                    }
+                }
+            }
+            @Override
+            public int available() throws IOException {
+                return NioSocketImpl.this.available();
+            }
+            @Override
+            public void close() throws IOException {
+                NioSocketImpl.this.close();
+            }
+        };
+    }
+
+    @Override
+    protected OutputStream getOutputStream() {
+        return new OutputStream() {
+            @Override
+            public void write(int b) throws IOException {
+                byte[] a = new byte[]{(byte) b};
+                write(a, 0, 1);
+            }
+            @Override
+            public void write(byte[] b, int off, int len) throws IOException {
+                Objects.checkFromIndexSize(off, len, b.length);
+                if (len > 0) {
+                    try {
+                        int pos = off;
+                        int end = off + len;
+                        while (pos < end) {
+                            // write up to MAX_BUFFER_SIZE bytes
+                            int size = Math.min((end - pos), MAX_BUFFER_SIZE);
+                            int n = NioSocketImpl.this.write(b, pos, size);
+                            pos += n;
+                        }
+                    } catch (IOException ioe) {
+                        throw new SocketException(ioe.getMessage());
+                    }
+                }
+            }
+
+            @Override
+            public void close() throws IOException {
+                NioSocketImpl.this.close();
+            }
+        };
+    }
+
+    @Override
+    protected int available() throws IOException {
+        readLock.lock();
+        try {
+            ensureOpenAndConnected();
+            if (isInputClosed) {
+                return 0;
+            } else {
+                return Net.available(fd);
+            }
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
+     * Closes the socket, signalling and waiting for blocking I/O operations
+     * to complete.
+     */
+    @Override
+    protected void close() throws IOException {
+        boolean interrupted = false;
+
+        synchronized (stateLock) {
+            int state = this.state;
+            if (state >= ST_CLOSING)
+                return;
+            if (state == ST_NEW) {
+                // stillborn
+                this.state = ST_CLOSED;
+                return;
+            }
+            this.state = ST_CLOSING;
+            assert fd != null && closer != null;
+
+            // shutdown output when linger interval not set
+            try {
+                var SO_LINGER = StandardSocketOptions.SO_LINGER;
+                if ((int) Net.getSocketOption(fd, SO_LINGER) != 0) {
+                    Net.shutdown(fd, Net.SHUT_WR);
+                }
+            } catch (IOException ignore) { }
+
+            // interrupt and wait for kernel threads to complete I/O operations
+            long reader = readerThread;
+            long writer = writerThread;
+            if (reader != 0 || writer != 0) {
+                nd.preClose(fd);
+
+                if (reader != 0)
+                    NativeThread.signal(reader);
+                if (writer != 0)
+                    NativeThread.signal(writer);
+
+                // wait for blocking I/O operations to end
+                while (readerThread != 0 || writerThread != 0) {
+                    try {
+                        stateLock.wait();
+                    } catch (InterruptedException e) {
+                        interrupted = true;
+                    }
+                }
+            }
+
+            // close file descriptor
+            try {
+                closer.run();
+            } finally {
+                this.state = ST_CLOSED;
+            }
+        }
+
+        // restore interrupt status
+        if (interrupted)
+            Thread.currentThread().interrupt();
+    }
+
+    @Override
+    protected Set<SocketOption<?>> supportedOptions() {
+        Set<SocketOption<?>> options = new HashSet<>();
+        options.addAll(super.supportedOptions());
+        if (server) {
+            options.addAll(ExtendedSocketOptions.serverSocketOptions());
+        } else {
+            options.addAll(ExtendedSocketOptions.clientSocketOptions());
+        }
+        if (Net.isReusePortAvailable())
+            options.add(StandardSocketOptions.SO_REUSEPORT);
+        return Collections.unmodifiableSet(options);
+    }
+
+    @Override
+    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
+        if (!supportedOptions().contains(opt))
+            throw new UnsupportedOperationException("'" + opt + "' not supported");
+        synchronized (stateLock) {
+            ensureOpen();
+            if (opt == StandardSocketOptions.IP_TOS) {
+                // maps to IP_TOS or IPV6_TCLASS
+                int i = (int) value;
+                Net.setSocketOption(fd, family(), opt, i);
+                trafficClass = i;
+            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
+                boolean b = (boolean) value;
+                if (Net.useExclusiveBind()) {
+                    isReuseAddress = b;
+                } else {
+                    Net.setSocketOption(fd, opt, b);
+                }
+            } else {
+                // option does not need special handling
+                Net.setSocketOption(fd, opt, value);
+            }
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    protected <T> T getOption(SocketOption<T> opt) throws IOException {
+        if (!supportedOptions().contains(opt))
+            throw new UnsupportedOperationException("'" + opt + "' not supported");
+        synchronized (stateLock) {
+            ensureOpen();
+            if (opt == StandardSocketOptions.IP_TOS) {
+                return (T) Integer.valueOf(trafficClass);
+            } else if (opt == StandardSocketOptions.SO_REUSEADDR) {
+                if (Net.useExclusiveBind()) {
+                    return (T) Boolean.valueOf(isReuseAddress);
+                } else {
+                    return (T) Net.getSocketOption(fd, opt);
+                }
+            } else {
+                // option does not need special handling
+                return (T) Net.getSocketOption(fd, opt);
+            }
+        }
+    }
+
+    private boolean booleanValue(Object value, String desc) throws SocketException {
+        if (!(value instanceof Boolean))
+            throw new SocketException("Bad value for " + desc);
+        return (boolean) value;
+    }
+
+    private int intValue(Object value, String desc) throws SocketException {
+        if (!(value instanceof Integer))
+            throw new SocketException("Bad value for " + desc);
+        return (int) value;
+    }
+
+    @Override
+    public void setOption(int opt, Object value) throws SocketException {
+        synchronized (stateLock) {
+            ensureOpen();
+            try {
+                switch (opt) {
+                case SO_LINGER: {
+                    // the value is "false" to disable, or linger interval to enable
+                    int i;
+                    if (value instanceof Boolean && ((boolean) value) == false) {
+                        i = -1;
+                    } else {
+                        i = intValue(value, "SO_LINGER");
+                    }
+                    Net.setSocketOption(fd, StandardSocketOptions.SO_LINGER, i);
+                    break;
+                }
+                case SO_TIMEOUT: {
+                    int i = intValue(value, "SO_TIMEOUT");
+                    if (i < 0)
+                        throw new IllegalArgumentException("timeout < 0");
+                    timeout = i;
+                    break;
+                }
+                case IP_TOS: {
+                    int i = intValue(value, "IP_TOS");
+                    Net.setSocketOption(fd, family(), StandardSocketOptions.IP_TOS, i);
+                    trafficClass = i;
+                    break;
+                }
+                case TCP_NODELAY: {
+                    boolean b = booleanValue(value, "TCP_NODELAY");
+                    Net.setSocketOption(fd, StandardSocketOptions.TCP_NODELAY, b);
+                    break;
+                }
+                case SO_SNDBUF: {
+                    int i = intValue(value, "SO_SNDBUF");
+                    if (i <= 0)
+                        throw new SocketException("SO_SNDBUF <= 0");
+                    Net.setSocketOption(fd, StandardSocketOptions.SO_SNDBUF, i);
+                    break;
+                }
+                case SO_RCVBUF: {
+                    int i = intValue(value, "SO_RCVBUF");
+                    if (i <= 0)
+                        throw new SocketException("SO_RCVBUF <= 0");
+                    Net.setSocketOption(fd, StandardSocketOptions.SO_RCVBUF, i);
+                    break;
+                }
+                case SO_KEEPALIVE: {
+                    boolean b = booleanValue(value, "SO_KEEPALIVE");
+                    Net.setSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE, b);
+                    break;
+                }
+                case SO_OOBINLINE: {
+                    boolean b = booleanValue(value, "SO_OOBINLINE");
+                    Net.setSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE, b);
+                    break;
+                }
+                case SO_REUSEADDR: {
+                    boolean b = booleanValue(value, "SO_REUSEADDR");
+                    if (Net.useExclusiveBind()) {
+                        isReuseAddress = b;
+                    } else {
+                        Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEADDR, b);
+                    }
+                    break;
+                }
+                case SO_REUSEPORT: {
+                    if (!Net.isReusePortAvailable())
+                        throw new SocketException("SO_REUSEPORT not supported");
+                    boolean b = booleanValue(value, "SO_REUSEPORT");
+                    Net.setSocketOption(fd, StandardSocketOptions.SO_REUSEPORT, b);
+                    break;
+                }
+                default:
+                    throw new SocketException("Unknown option " + opt);
+                }
+            } catch (SocketException e) {
+                throw e;
+            } catch (IllegalArgumentException | IOException e) {
+                throw new SocketException(e.getMessage());
+            }
+        }
+    }
+
+    @Override
+    public Object getOption(int opt) throws SocketException {
+        synchronized (stateLock) {
+            ensureOpen();
+            try {
+                switch (opt) {
+                case SO_TIMEOUT:
+                    return timeout;
+                case TCP_NODELAY:
+                    return Net.getSocketOption(fd, StandardSocketOptions.TCP_NODELAY);
+                case SO_OOBINLINE:
+                    return Net.getSocketOption(fd, ExtendedSocketOption.SO_OOBINLINE);
+                case SO_LINGER: {
+                    // return "false" when disabled, linger interval when enabled
+                    int i = (int) Net.getSocketOption(fd, StandardSocketOptions.SO_LINGER);
+                    if (i == -1) {
+                        return Boolean.FALSE;
+                    } else {
+                        return i;
+                    }
+                }
+                case SO_REUSEADDR:
+                    if (Net.useExclusiveBind()) {
+                        return isReuseAddress;
+                    } else {
+                        return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEADDR);
+                    }
+                case SO_BINDADDR:
+                    return Net.localAddress(fd).getAddress();
+                case SO_SNDBUF:
+                    return Net.getSocketOption(fd, StandardSocketOptions.SO_SNDBUF);
+                case SO_RCVBUF:
+                    return Net.getSocketOption(fd, StandardSocketOptions.SO_RCVBUF);
+                case IP_TOS:
+                    return trafficClass;
+                case SO_KEEPALIVE:
+                    return Net.getSocketOption(fd, StandardSocketOptions.SO_KEEPALIVE);
+                case SO_REUSEPORT:
+                    if (!Net.isReusePortAvailable())
+                        throw new SocketException("SO_REUSEPORT not supported");
+                    return Net.getSocketOption(fd, StandardSocketOptions.SO_REUSEPORT);
+                default:
+                    throw new SocketException("Unknown option " + opt);
+                }
+            } catch (SocketException e) {
+                throw e;
+            } catch (IllegalArgumentException | IOException e) {
+                throw new SocketException(e.getMessage());
+            }
+        }
+    }
+
+    @Override
+    protected void shutdownInput() throws IOException {
+        synchronized (stateLock) {
+            ensureOpenAndConnected();
+            if (!isInputClosed) {
+                Net.shutdown(fd, Net.SHUT_RD);
+                long reader = readerThread;
+                if (reader != 0)
+                    NativeThread.signal(reader);
+                isInputClosed = true;
+            }
+        }
+    }
+
+    @Override
+    protected void shutdownOutput() throws IOException {
+        synchronized (stateLock) {
+            ensureOpenAndConnected();
+            if (!isOutputClosed) {
+                Net.shutdown(fd, Net.SHUT_WR);
+                long writer = writerThread;
+                if (writer != 0)
+                    NativeThread.signal(writer);
+                isOutputClosed = true;
+            }
+        }
+    }
+
+    @Override
+    protected boolean supportsUrgentData() {
+        return true;
+    }
+
+    @Override
+    protected void sendUrgentData(int data) throws IOException {
+        writeLock.lock();
+        try {
+            int n = 0;
+            FileDescriptor fd = beginWrite();
+            try {
+                do {
+                    n = Net.sendOOB(fd, (byte) data);
+                } while (n == IOStatus.INTERRUPTED && isOpen());
+                if (n == IOStatus.UNAVAILABLE) {
+                    throw new RuntimeException("not implemented yet");
+                }
+            } finally {
+                endWrite(n > 0);
+            }
+        } finally {
+            writeLock.unlock();
+        }
+    }
+
+    /**
+     * A task that closes a SocketImpl's file descriptor. The task runs when the
+     * SocketImpl is explicitly closed and when the SocketImpl becomes phantom
+     * reachable.
+     */
+    private static class FileDescriptorCloser implements Runnable {
+        private static final VarHandle CLOSED;
+        static {
+            try {
+                MethodHandles.Lookup l = MethodHandles.lookup();
+                CLOSED = l.findVarHandle(FileDescriptorCloser.class,
+                                         "closed",
+                                         boolean.class);
+            } catch (Exception e) {
+                throw new InternalError(e);
+            }
+        }
+
+        private final FileDescriptor fd;
+        private final boolean stream;
+        private volatile boolean closed;
+
+        FileDescriptorCloser(FileDescriptor fd, boolean stream) {
+            this.fd = fd;
+            this.stream = stream;
+        }
+
+        static FileDescriptorCloser create(NioSocketImpl impl) {
+            assert Thread.holdsLock(impl.stateLock);
+            var closer = new FileDescriptorCloser(impl.fd, impl.stream);
+            CleanerFactory.cleaner().register(impl, closer);
+            return closer;
+        }
+
+        @Override
+        public void run() {
+            if (CLOSED.compareAndSet(this, false, true)) {
+                try {
+                    nd.close(fd);
+                } catch (IOException ioe) {
+                    throw new RuntimeException(ioe);
+                } finally {
+                    if (!stream) {
+                        // decrement
+                        ResourceManager.afterUdpClose();
+                    }
+                }
+            }
+        }
+
+        boolean disable() {
+            return CLOSED.compareAndSet(this, false, true);
+        }
+    }
+
+    /**
+     * Returns the socket protocol family
+     */
+    private static ProtocolFamily family() {
+        if (Net.isIPv6Available()) {
+            return StandardProtocolFamily.INET6;
+        } else {
+            return StandardProtocolFamily.INET;
+        }
+    }
+
+    /**
+     * Returns the native file descriptor
+     */
+    private static int fdVal(FileDescriptor fd) {
+        int fdVal = SharedSecrets.getJavaIOFileDescriptorAccess().get(fd);
+        assert fdVal == IOUtil.fdVal(fd);
+        return fdVal;
+    }
+
+    /**
+     * 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);
+    }
+}
--- a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java	Sun Feb 17 12:21:11 2019 +0000
@@ -529,7 +529,7 @@
     // Returns 1 on success, or IOStatus.UNAVAILABLE (if non-blocking and no
     // connections are pending) or IOStatus.INTERRUPTED.
     //
-    private native int accept0(FileDescriptor ssfd,
+    static native int accept0(FileDescriptor ssfd,
                                FileDescriptor newfd,
                                InetSocketAddress[] isaa)
         throws IOException;
--- a/src/java.base/unix/classes/sun/nio/ch/NativeThread.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/unix/classes/sun/nio/ch/NativeThread.java	Sun Feb 17 12:21:11 2019 +0000
@@ -25,7 +25,6 @@
 
 package sun.nio.ch;
 
-
 // Signalling operations on native threads
 //
 // On some operating systems (e.g., Linux), closing a channel while another
@@ -33,23 +32,35 @@
 // thread to be released.  This class provides access to the native threads
 // upon which Java threads are built, and defines a simple signal mechanism
 // that can be used to release a native thread from a blocking I/O operation.
-// On systems that do not require this type of signalling, the current() method
-// always returns -1 and the signal(long) method has no effect.
 
+import jdk.internal.access.JavaLangAccess;
+import jdk.internal.access.SharedSecrets;
 
 public class NativeThread {
+    private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
 
-    // Returns an opaque token representing the native thread underlying the
-    // invoking Java thread.  On systems that do not require signalling, this
-    // method always returns -1.
-    //
-    public static native long current();
+    /**
+     * Returns the current thread's ID.
+     */
+    public static long current() {
+        long tid = JLA.nativeTid();
+        if (tid == 0) {
+            tid = current0();
+            JLA.setNativeTid(tid);
+        }
+        return tid;
+    }
 
-    // Signals the given native thread so as to release it from a blocking I/O
-    // operation.  On systems that do not require signalling, this method has
-    // no effect.
-    //
-    public static native void signal(long nt);
+    /**
+     * Signals the given thread.
+     */
+    public static void signal(long tid) {
+        signal0(tid);
+    }
+
+    private static native long current0();
+
+    private static native void signal0(long tid);
 
     private static native void init();
 
--- a/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java	Sun Feb 17 12:21:11 2019 +0000
@@ -34,9 +34,22 @@
  */
 
 class SocketDispatcher extends NativeDispatcher {
+    private final boolean detectConnectionReset;
+
+    SocketDispatcher(boolean detectConnectionReset) {
+        this.detectConnectionReset = detectConnectionReset;
+    }
+
+    SocketDispatcher() {
+        this(false);
+    }
 
     int read(FileDescriptor fd, long address, int len) throws IOException {
-        return FileDispatcherImpl.read0(fd, address, len);
+        if (detectConnectionReset) {
+            return read0(fd, address, len);
+        } else {
+            return FileDispatcherImpl.read0(fd, address, len);
+        }
     }
 
     long readv(FileDescriptor fd, long address, int len) throws IOException {
@@ -58,4 +71,20 @@
     void preClose(FileDescriptor fd) throws IOException {
         FileDispatcherImpl.preClose0(fd);
     }
+
+    // -- Native methods --
+
+    /**
+     * Reads up to len bytes from a socket with special handling for "connection
+     * reset".
+     *
+     * @throws sun.net.ConnectionResetException if connection reset is detected
+     * @throws IOException if another I/O error occurs
+     */
+    private static native int read0(FileDescriptor fd, long address, int len)
+        throws IOException;
+
+    static {
+        IOUtil.load();
+    }
 }
--- a/src/java.base/unix/native/libnio/ch/NativeThread.c	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/unix/native/libnio/ch/NativeThread.c	Sun Feb 17 12:21:11 2019 +0000
@@ -77,7 +77,7 @@
 }
 
 JNIEXPORT jlong JNICALL
-Java_sun_nio_ch_NativeThread_current(JNIEnv *env, jclass cl)
+Java_sun_nio_ch_NativeThread_current0(JNIEnv *env, jclass cl)
 {
 #ifdef __solaris__
     return (jlong)thr_self();
@@ -87,7 +87,7 @@
 }
 
 JNIEXPORT void JNICALL
-Java_sun_nio_ch_NativeThread_signal(JNIEnv *env, jclass cl, jlong thread)
+Java_sun_nio_ch_NativeThread_signal0(JNIEnv *env, jclass cl, jlong thread)
 {
     int ret;
 #ifdef __solaris__
--- a/src/java.base/unix/native/libnio/ch/Net.c	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/unix/native/libnio/ch/Net.c	Sun Feb 17 12:21:11 2019 +0000
@@ -830,6 +830,7 @@
             break;
         case EADDRINUSE:  /* Fall through */
         case EADDRNOTAVAIL:
+        case EACCES:
             xn = JNU_JAVANETPKG "BindException";
             break;
         default:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/unix/native/libnio/ch/SocketDispatcher.c	Sun Feb 17 12:21:11 2019 +0000
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ #include <sys/types.h>
+ #include <unistd.h>
+
+ #include "jni.h"
+ #include "jni_util.h"
+ #include "jlong.h"
+ #include "nio.h"
+ #include "nio_util.h"
+ #include "sun_nio_ch_SocketDispatcher.h"
+
+ JNIEXPORT jint JNICALL
+ Java_sun_nio_ch_SocketDispatcher_read0(JNIEnv *env, jclass clazz,
+                                        jobject fdo, jlong address, jint len)
+ {
+     jint fd = fdval(env, fdo);
+     void *buf = (void *)jlong_to_ptr(address);
+     jint n = read(fd, buf, len);
+     if ((n == -1) && (errno == ECONNRESET || errno == EPIPE)) {
+         JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
+         return IOS_THROWN;
+     } else {
+         return convertReturnVal(env, n, JNI_TRUE);
+     }
+ }
--- a/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/java.base/windows/classes/sun/nio/ch/SocketDispatcher.java	Sun Feb 17 12:21:11 2019 +0000
@@ -32,12 +32,11 @@
  * for read and write operations.
  */
 
-class SocketDispatcher extends NativeDispatcher
-{
+class SocketDispatcher extends NativeDispatcher {
 
-    static {
-        IOUtil.load();
-    }
+    SocketDispatcher() { }
+
+    SocketDispatcher(boolean ignore) { }
 
     int read(FileDescriptor fd, long address, int len) throws IOException {
         return read0(fd, address, len);
@@ -63,7 +62,8 @@
         close0(fd);
     }
 
-    //-- Native methods
+    // -- Native methods --
+
     static native int read0(FileDescriptor fd, long address, int len)
         throws IOException;
 
@@ -79,4 +79,8 @@
     static native void preClose0(FileDescriptor fd) throws IOException;
 
     static native void close0(FileDescriptor fd) throws IOException;
+
+    static {
+        IOUtil.load();
+    }
 }
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketInputStreamInstrumentor.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketInputStreamInstrumentor.java	Sun Feb 17 12:21:11 2019 +0000
@@ -27,15 +27,14 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Socket;
 
 import jdk.jfr.events.SocketReadEvent;
 
 /**
  * See {@link JITracer} for an explanation of this code.
  */
-@JIInstrumentationTarget("java.net.SocketInputStream")
-@JITypeMapping(from = "jdk.jfr.internal.instrument.SocketInputStreamInstrumentor$AbstractPlainSocketImpl",
-            to = "java.net.AbstractPlainSocketImpl")
+@JIInstrumentationTarget("java.net.Socket$SocketInputStream")
 final class SocketInputStreamInstrumentor {
 
     private SocketInputStreamInstrumentor() {
@@ -43,30 +42,28 @@
 
     @SuppressWarnings("deprecation")
     @JIInstrumentationMethod
-    int read(byte b[], int off, int length, int timeout) throws IOException {
+    public int read(byte b[], int off, int length) throws IOException {
         SocketReadEvent event = SocketReadEvent.EVENT.get();
         if (!event.isEnabled()) {
-            return read(b, off, length, timeout);
+            return read(b, off, length);
         }
         int bytesRead = 0;
         try {
             event.begin();
-            bytesRead = read(b, off, length, timeout);
+            bytesRead = read(b, off, length);
         } finally {
             event.end();
             if (event.shouldCommit()) {
-                String hostString  = impl.address.toString();
-                int delimiterIndex = hostString.lastIndexOf('/');
-
-                event.host      = hostString.substring(0, delimiterIndex);
-                event.address   = hostString.substring(delimiterIndex + 1);
-                event.port      = impl.port;
+                InetAddress remote = parent.getInetAddress();
+                event.host = remote.getHostName();
+                event.address = remote.getHostAddress();
+                event.port = parent.getPort();
                 if (bytesRead < 0) {
                     event.endOfStream = true;
                 } else {
                     event.bytesRead = bytesRead;
                 }
-                event.timeout   = timeout;
+                event.timeout = parent.getSoTimeout();
 
                 event.commit();
                 event.reset();
@@ -75,14 +72,6 @@
         return bytesRead;
     }
 
-    private AbstractPlainSocketImpl impl = null;
-
-    void silenceFindBugsUnwrittenField(InetAddress dummy) {
-        impl.address = dummy;
-    }
-
-    static class AbstractPlainSocketImpl {
-        InetAddress address;
-        int port;
-    }
+    // private field in java.net.Socket$SocketInputStream
+    private Socket parent;
 }
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketOutputStreamInstrumentor.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketOutputStreamInstrumentor.java	Sun Feb 17 12:21:11 2019 +0000
@@ -27,15 +27,14 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Socket;
 
 import jdk.jfr.events.SocketWriteEvent;
 
 /**
  * See {@link JITracer} for an explanation of this code.
  */
-@JIInstrumentationTarget("java.net.SocketOutputStream")
-@JITypeMapping(from = "jdk.jfr.internal.instrument.SocketOutputStreamInstrumentor$AbstractPlainSocketImpl",
-            to = "java.net.AbstractPlainSocketImpl")
+@JIInstrumentationTarget("java.net.Socket$SocketOutputStream")
 final class SocketOutputStreamInstrumentor {
 
     private SocketOutputStreamInstrumentor() {
@@ -43,27 +42,25 @@
 
     @SuppressWarnings("deprecation")
     @JIInstrumentationMethod
-    private void socketWrite(byte b[], int off, int len) throws IOException {
+    public void write(byte b[], int off, int len) throws IOException {
         SocketWriteEvent event = SocketWriteEvent.EVENT.get();
         if (!event.isEnabled()) {
-            socketWrite(b, off, len);
+            write(b, off, len);
             return;
         }
         int bytesWritten = 0;
         try {
             event.begin();
-            socketWrite(b, off, len);
+            write(b, off, len);
             bytesWritten = len;
         } finally {
             event.end() ;
             if (event.shouldCommit()) {
-                String hostString  = impl.address.toString();
-                int delimiterIndex = hostString.lastIndexOf('/');
-
-                event.host         = hostString.substring(0, delimiterIndex);
-                event.address      = hostString.substring(delimiterIndex + 1);
-                event.port         = impl.port;
-                event.bytesWritten = bytesWritten < 0 ? 0 : bytesWritten;
+                InetAddress remote = parent.getInetAddress();
+                event.host = remote.getHostName();
+                event.address = remote.getHostAddress();
+                event.port = parent.getPort();
+                event.bytesWritten = bytesWritten;
 
                 event.commit();
                 event.reset();
@@ -71,14 +68,6 @@
         }
     }
 
-    private AbstractPlainSocketImpl impl = null;
-
-    void silenceFindBugsUnwrittenField(InetAddress dummy) {
-        impl.address = dummy;
-    }
-
-    static class AbstractPlainSocketImpl {
-        InetAddress address;
-        int port;
-    }
+    // private field in java.net.Socket$SocketOutputStream
+    private Socket parent;
 }
--- a/test/jdk/ProblemList.txt	Sat Feb 16 21:15:33 2019 +0100
+++ b/test/jdk/ProblemList.txt	Sun Feb 17 12:21:11 2019 +0000
@@ -562,6 +562,9 @@
 
 java/net/ServerSocket/AcceptInheritHandle.java                  8211854 aix-ppc64
 
+java/net/Inet6Address/B6206527.java                             8216417 macosx-all
+java/net/ipv6tests/B6521014.java                                8216417 macosx-all
+
 ############################################################################
 
 # jdk_nio
@@ -870,3 +873,4 @@
 # jdk_jfr
 
 jdk/jfr/event/io/TestInstrumentation.java                       8202142    generic-all
+jdk/jfr/event/io/EvilInstrument.java                            0000000    generic-all
--- a/test/jdk/java/net/Socket/asyncClose/BrokenPipe.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/test/jdk/java/net/Socket/asyncClose/BrokenPipe.java	Sun Feb 17 12:21:11 2019 +0000
@@ -69,7 +69,7 @@
              * replace this by catching a more specific exception.
              */
             String text = ioe.getMessage();
-            if (text.toLowerCase().indexOf("closed") >= 0) {
+            if (text.toLowerCase().indexOf("Socket closed") >= 0) {
                 throw ioe;
             }
         } finally {
--- a/test/jdk/jdk/jfr/event/io/TestInstrumentation.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/test/jdk/jdk/jfr/event/io/TestInstrumentation.java	Sun Feb 17 12:21:11 2019 +0000
@@ -105,14 +105,14 @@
         "java/io/FileOutputStream::write::(I)V",
         "java/io/FileOutputStream::write::([B)V",
         "java/io/FileOutputStream::write::([BII)V",
-        "java/net/SocketInputStream::read::()I",
-        "java/net/SocketInputStream::read::([B)I",
-        "java/net/SocketInputStream::read::([BII)I",
-        "java/net/SocketInputStream::close::()V",
-        "java/net/SocketOutputStream::write::(I)V",
-        "java/net/SocketOutputStream::write::([B)V",
-        "java/net/SocketOutputStream::write::([BII)V",
-        "java/net/SocketOutputStream::close::()V",
+        "java/net/Socket$SocketInputStream::read::()I",
+        "java/net/Socket$SocketInputStream::read::([B)I",
+        "java/net/Socket$SocketInputStream::read::([BII)I",
+        "java/net/Socket$SocketInputStream::close::()V",
+        "java/net/Socket$SocketOutputStream::write::(I)V",
+        "java/net/Socket$SocketOutputStream::write::([B)V",
+        "java/net/Socket$SocketOutputStream::write::([BII)V",
+        "java/net/Socket$SocketOutputStream::close::()V",
         "java/nio/channels/FileChannel::read::([Ljava/nio/ByteBuffer;)J",
         "java/nio/channels/FileChannel::write::([Ljava/nio/ByteBuffer;)J",
         "java/nio/channels/SocketChannel::open::()Ljava/nio/channels/SocketChannel;",
--- a/test/micro/org/openjdk/bench/java/net/SocketReadWrite.java	Sat Feb 16 21:15:33 2019 +0100
+++ b/test/micro/org/openjdk/bench/java/net/SocketReadWrite.java	Sun Feb 17 12:21:11 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,86 +22,206 @@
  */
 package org.openjdk.bench.java.net;
 
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-import org.openjdk.jmh.annotations.BenchmarkMode;
-import org.openjdk.jmh.annotations.Benchmark;
-import org.openjdk.jmh.annotations.Mode;
-import org.openjdk.jmh.annotations.OutputTimeUnit;
-import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.Setup;
-import org.openjdk.jmh.annotations.State;
-import org.openjdk.jmh.annotations.TearDown;
+/**
+ * Tests socket read/write.
+ */
 
-/**
- * Tests the overheads of I/O API.
- * This test is known to depend heavily on network conditions and paltform.
- */
 @BenchmarkMode(Mode.Throughput)
-@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@OutputTimeUnit(TimeUnit.SECONDS)
 @State(Scope.Thread)
 public class SocketReadWrite {
 
-    private OutputStream os;
-    private InputStream is;
-    private ServerSocket ss;
-    private Socket s1, s2;
-    private ReadThread rt;
+    static final InetAddress address = InetAddress.getLoopbackAddress();
+    public static final int TIMEOUT = 10000;
+
+    static class EchoServer implements Runnable {
+        final ServerSocket ss;
+        final int port;
+        final CountDownLatch startedLatch;
+        final int size;
+        final boolean timeout;
+        List<ServerThread> threads = new ArrayList<>();
+        volatile boolean isDone = false;
+
+        public EchoServer(CountDownLatch await, int size, boolean timeout) throws IOException {
+            this.size = size;
+            this.timeout = timeout;
+            ss = new ServerSocket(0);
+            port = ss.getLocalPort();
+            this.startedLatch = await;
+        }
+
+        @Override
+        public void run() {
+            startedLatch.countDown();
+            while (!isDone) {
+                try {
+                    Socket s = ss.accept();
+                    s.setTcpNoDelay(true);
+                    if (timeout) {
+                        s.setSoTimeout(TIMEOUT);
+                    }
+                    ServerThread st = new ServerThread(s, size);
+                    threads.add(st);
+                    new Thread(st).start();
+                } catch (IOException e) {
+                    if (!isDone) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        }
+
+        synchronized void close() throws IOException {
+            if (!isDone) {
+                isDone = true;
+                ss.close();
+                for (ServerThread st : threads) {
+                    st.close();
+                }
+            }
+        }
+
+        static EchoServer instance = null;
+
+        static synchronized EchoServer startServer(int size, boolean timeout) throws IOException {
+            if (instance == null) {
+                CountDownLatch started = new CountDownLatch(1);
+                EchoServer s = new EchoServer(started, size, timeout);
+                new Thread(s).start();
+                try {
+                    started.await(); // wait until server thread started
+                } catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+                instance = s;
+            }
+            return instance;
+        }
+
+        static class ServerThread implements Runnable {
+
+            final Socket s;
+            final InputStream in;
+            final OutputStream out;
+            final int size;
+            volatile boolean isDone = false;
+
+            ServerThread(Socket s, int size) throws IOException {
+                this.s = s;
+                this.size = size;
+                in = s.getInputStream();
+                out = s.getOutputStream();
+            }
+
+            @Override
+            public void run() {
+                if (size == 1) {
+                    while (!isDone) {
+                        try {
+                            int b = this.in.read();
+                            out.write(b);
+                        } catch (IOException e) {
+                            if (!isDone) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                } else {
+                    byte[] a = new byte[size];
+                    while (!isDone) {
+                        try {
+                            readN(a, size, this.in);
+                            out.write(a);
+                        } catch (IOException e) {
+                            if (!isDone) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
+                }
+            }
+
+            public void close() throws IOException {
+                isDone = true;
+                s.close();
+            }
+
+        }
+    }
+
+    static void readN(byte[] array, int size, InputStream in) throws IOException {
+        int nread = 0;
+        while (size > 0) {
+            int n = in.read(array, nread, size);
+            if (n < 0) throw new RuntimeException();
+            nread += n;
+            size -= n;
+        }
+    }
+
+    EchoServer server;
+
+    @Param({"1", "1024", "8192", "64000", "128000"})
+    public int size;
+
+    @Param({"false", "true"})
+    public boolean timeout;
+
+    Socket s;
+    InputStream in;
+    OutputStream out;
+    byte[] array;
 
     @Setup
-    public void beforeRun() throws IOException {
-        InetAddress iaddr = InetAddress.getLocalHost();
-
-        ss = new ServerSocket(0);
-        s1 = new Socket(iaddr, ss.getLocalPort());
-        s2 = ss.accept();
-
-        os = s1.getOutputStream();
-        is = s2.getInputStream();
-
-        rt = new ReadThread(is);
-        rt.start();
+    public void setup() throws IOException {
+        server = EchoServer.startServer(size, timeout);
+        int port = server.port;
+        s = new Socket(address, port);
+        s.setTcpNoDelay(true);
+        if (timeout) {
+            s.setSoTimeout(TIMEOUT);
+        }
+        in = s.getInputStream();
+        out = s.getOutputStream();
+        array = new byte[size];
     }
 
     @TearDown
-    public void afterRun() throws IOException, InterruptedException {
-        os.write(0);
-        os.close();
-        is.close();
-        s1.close();
-        s2.close();
-        ss.close();
-        rt.join();
+    public void tearDown() throws IOException {
+        server.close();
+        s.close();
     }
 
     @Benchmark
-    public void test() throws IOException {
-        os.write((byte) 4711);
-    }
-
-    static class ReadThread extends Thread {
-        private InputStream is;
-
-        public ReadThread(InputStream is) {
-            this.is = is;
+    public void echo() throws IOException {
+        if (size == 1) {
+            out.write((byte) 47);
+            int c = in.read();
+        } else {
+            out.write(array);
+            readN(array, size, in);
         }
 
-        public void run() {
-            try {
-                while (is.read() > 0);
-            } catch (SocketException ex) {
-                // ignore - most likely "socket closed", which means shutdown
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
     }
-
 }