< prev index next >

src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java

Print this page
rev 48993 : imported patch nio

@@ -42,28 +42,21 @@
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.IllegalBlockingModeException;
 import java.nio.channels.SocketChannel;
 import java.security.AccessController;
 import java.security.PrivilegedExceptionAction;
-import java.util.concurrent.TimeUnit;
+import static java.util.concurrent.TimeUnit.*;
 
 // Make a socket channel look like a socket.
 //
-// The only aspects of java.net.Socket-hood that we don't attempt to emulate
-// here are the interrupted-I/O exceptions (which our Solaris implementations
-// attempt to support) and the sending of urgent data.  Otherwise an adapted
-// socket should look enough like a real java.net.Socket to fool most of the
-// developers most of the time, right down to the exception message strings.
-//
 // The methods in this class are defined in exactly the same order as in
 // java.net.Socket so as to simplify tracking future changes to that class.
 //
 
 class SocketAdaptor
     extends Socket
 {
-
     // The channel being adapted
     private final SocketChannelImpl sc;
 
     // Timeout "option" value for reads
     private volatile int timeout;

@@ -100,42 +93,44 @@
         synchronized (sc.blockingLock()) {
             if (!sc.isBlocking())
                 throw new IllegalBlockingModeException();
 
             try {
+                // no timeout
                 if (timeout == 0) {
                     sc.connect(remote);
                     return;
                 }
 
+                // timed connect
                 sc.configureBlocking(false);
                 try {
                     if (sc.connect(remote))
                         return;
-                    long timeoutNanos =
-                        TimeUnit.NANOSECONDS.convert(timeout,
-                            TimeUnit.MILLISECONDS);
+                } finally {
+                    try {
+                        sc.configureBlocking(true);
+                    } catch (ClosedChannelException e) { }
+                }
+
+                long timeoutNanos = NANOSECONDS.convert(timeout, MILLISECONDS);
+                long to = timeout;
                     for (;;) {
-                        if (!sc.isOpen())
-                            throw new ClosedChannelException();
                         long startTime = System.nanoTime();
-
-                        int result = sc.poll(Net.POLLCONN, timeout);
-                        if (result > 0 && sc.finishConnect())
+                    if (sc.pollConnected(to)) {
+                        boolean connected = sc.finishConnect();
+                        assert connected;
                             break;
+                    }
                         timeoutNanos -= System.nanoTime() - startTime;
                         if (timeoutNanos <= 0) {
                             try {
                                 sc.close();
                             } catch (IOException x) { }
                             throw new SocketTimeoutException();
                         }
-                    }
-                } finally {
-                    try {
-                        sc.configureBlocking(true);
-                    } catch (ClosedChannelException e) { }
+                    to = MILLISECONDS.convert(timeoutNanos, NANOSECONDS);
                 }
 
             } catch (Exception x) {
                 Net.translateException(x, true);
             }

@@ -150,15 +145,15 @@
             Net.translateException(x);
         }
     }
 
     public InetAddress getInetAddress() {
-        SocketAddress remote = sc.remoteAddress();
+        InetSocketAddress remote = sc.remoteAddress();
         if (remote == null) {
             return null;
         } else {
-            return ((InetSocketAddress)remote).getAddress();
+            return remote.getAddress();
         }
     }
 
     public InetAddress getLocalAddress() {
         if (sc.isOpen()) {

@@ -169,24 +164,24 @@
         }
         return new InetSocketAddress(0).getAddress();
     }
 
     public int getPort() {
-        SocketAddress remote = sc.remoteAddress();
+        InetSocketAddress remote = sc.remoteAddress();
         if (remote == null) {
             return 0;
         } else {
-            return ((InetSocketAddress)remote).getPort();
+            return remote.getPort();
         }
     }
 
     public int getLocalPort() {
-        SocketAddress local = sc.localAddress();
+        InetSocketAddress local = sc.localAddress();
         if (local == null) {
             return -1;
         } else {
-            return ((InetSocketAddress)local).getPort();
+            return local.getPort();
         }
     }
 
     private class SocketInputStream
         extends ChannelInputStream

@@ -200,38 +195,26 @@
         {
             synchronized (sc.blockingLock()) {
                 if (!sc.isBlocking())
                     throw new IllegalBlockingModeException();
 
-                if (timeout == 0)
+                // no timeout
+                long to = SocketAdaptor.this.timeout;
+                if (to == 0)
                     return sc.read(bb);
 
-                sc.configureBlocking(false);
-                try {
-                    int n;
-                    if ((n = sc.read(bb)) != 0)
-                        return n;
-                    long timeoutNanos =
-                        TimeUnit.NANOSECONDS.convert(timeout,
-                            TimeUnit.MILLISECONDS);
+                // timed read
+                long timeoutNanos = NANOSECONDS.convert(to, MILLISECONDS);
                     for (;;) {
-                        if (!sc.isOpen())
-                            throw new ClosedChannelException();
                         long startTime = System.nanoTime();
-                        int result = sc.poll(Net.POLLIN, timeout);
-                        if (result > 0) {
-                            if ((n = sc.read(bb)) != 0)
-                                return n;
+                    if (sc.pollRead(to)) {
+                        return sc.read(bb);
                         }
                         timeoutNanos -= System.nanoTime() - startTime;
                         if (timeoutNanos <= 0)
                             throw new SocketTimeoutException();
-                    }
-                } finally {
-                    try {
-                        sc.configureBlocking(true);
-                    } catch (ClosedChannelException e) { }
+                    to = MILLISECONDS.convert(timeoutNanos, NANOSECONDS);
                 }
             }
         }
     }
 

@@ -451,7 +434,6 @@
     }
 
     public boolean isOutputShutdown() {
         return !sc.isOutputOpen();
     }
-
 }
< prev index next >