< prev index next >

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

Print this page
rev 48993 : imported patch nio

@@ -32,11 +32,10 @@
 import java.net.Socket;
 import java.net.SocketAddress;
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.net.StandardSocketOptions;
-import java.nio.channels.ClosedChannelException;
 import java.nio.channels.IllegalBlockingModeException;
 import java.nio.channels.NotYetBoundException;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
 

@@ -49,11 +48,10 @@
 //
 
 class ServerSocketAdaptor                        // package-private
     extends ServerSocket
 {
-
     // The channel being adapted
     private final ServerSocketChannelImpl ssc;
 
     // Timeout "option" value for accepts
     private volatile int timeout;

@@ -65,17 +63,14 @@
             throw new Error(x);
         }
     }
 
     // ## super will create a useless impl
-    private ServerSocketAdaptor(ServerSocketChannelImpl ssc)
-        throws IOException
-    {
+    private ServerSocketAdaptor(ServerSocketChannelImpl ssc) throws IOException {
         this.ssc = ssc;
     }
 
-
     public void bind(SocketAddress local) throws IOException {
         bind(local, 50);
     }
 
     public void bind(SocketAddress local, int backlog) throws IOException {

@@ -87,62 +82,54 @@
             Net.translateException(x);
         }
     }
 
     public InetAddress getInetAddress() {
-        if (!ssc.isBound())
+        InetSocketAddress local = ssc.localAddress();
+        if (local == null) {
             return null;
-        return Net.getRevealedLocalAddress(ssc.localAddress()).getAddress();
-
+        } else {
+            return Net.getRevealedLocalAddress(local).getAddress();
+        }
     }
 
     public int getLocalPort() {
-        if (!ssc.isBound())
+        InetSocketAddress local = ssc.localAddress();
+        if (local == null) {
             return -1;
-        return Net.asInetSocketAddress(ssc.localAddress()).getPort();
+        } else {
+            return local.getPort();
+        }
     }
-
 
     public Socket accept() throws IOException {
         synchronized (ssc.blockingLock()) {
             try {
                 if (!ssc.isBound())
                     throw new NotYetBoundException();
 
-                if (timeout == 0) {
+                long to = this.timeout;
+                if (to == 0) {
                     // for compatibility reasons: accept connection if available
                     // when configured non-blocking
                     SocketChannel sc = ssc.accept();
                     if (sc == null && !ssc.isBlocking())
                         throw new IllegalBlockingModeException();
                     return sc.socket();
                 }
 
                 if (!ssc.isBlocking())
                     throw new IllegalBlockingModeException();
-                ssc.configureBlocking(false);
-                try {
-                    SocketChannel sc;
-                    if ((sc = ssc.accept()) != null)
-                        return sc.socket();
-                    long to = timeout;
                     for (;;) {
-                        if (!ssc.isOpen())
-                            throw new ClosedChannelException();
                         long st = System.currentTimeMillis();
-                        int result = ssc.poll(Net.POLLIN, to);
-                        if (result > 0 && ((sc = ssc.accept()) != null))
-                            return sc.socket();
+                    if (ssc.pollAccept(to))
+                        return ssc.accept().socket();
                         to -= System.currentTimeMillis() - st;
                         if (to <= 0)
                             throw new SocketTimeoutException();
                     }
-                } finally {
-                    try {
-                        ssc.configureBlocking(true);
-                    } catch (ClosedChannelException e) { }
-                }
+
             } catch (Exception x) {
                 Net.translateException(x);
                 assert false;
                 return null;            // Never happens
             }

@@ -214,7 +201,6 @@
         } catch (IOException x) {
             Net.translateToSocketException(x);
             return -1;          // Never happens
         }
     }
-
 }
< prev index next >