< prev index next >

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

Print this page
rev 48993 : imported patch nio


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.IOException;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.net.ServerSocket;
  32 import java.net.Socket;
  33 import java.net.SocketAddress;
  34 import java.net.SocketException;
  35 import java.net.SocketTimeoutException;
  36 import java.net.StandardSocketOptions;
  37 import java.nio.channels.ClosedChannelException;
  38 import java.nio.channels.IllegalBlockingModeException;
  39 import java.nio.channels.NotYetBoundException;
  40 import java.nio.channels.ServerSocketChannel;
  41 import java.nio.channels.SocketChannel;
  42 
  43 
  44 // Make a server-socket channel look like a server socket.
  45 //
  46 // The methods in this class are defined in exactly the same order as in
  47 // java.net.ServerSocket so as to simplify tracking future changes to that
  48 // class.
  49 //
  50 
  51 class ServerSocketAdaptor                        // package-private
  52     extends ServerSocket
  53 {
  54 
  55     // The channel being adapted
  56     private final ServerSocketChannelImpl ssc;
  57 
  58     // Timeout "option" value for accepts
  59     private volatile int timeout;
  60 
  61     public static ServerSocket create(ServerSocketChannelImpl ssc) {
  62         try {
  63             return new ServerSocketAdaptor(ssc);
  64         } catch (IOException x) {
  65             throw new Error(x);
  66         }
  67     }
  68 
  69     // ## super will create a useless impl
  70     private ServerSocketAdaptor(ServerSocketChannelImpl ssc)
  71         throws IOException
  72     {
  73         this.ssc = ssc;
  74     }
  75 
  76 
  77     public void bind(SocketAddress local) throws IOException {
  78         bind(local, 50);
  79     }
  80 
  81     public void bind(SocketAddress local, int backlog) throws IOException {
  82         if (local == null)
  83             local = new InetSocketAddress(0);
  84         try {
  85             ssc.bind(local, backlog);
  86         } catch (Exception x) {
  87             Net.translateException(x);
  88         }
  89     }
  90 
  91     public InetAddress getInetAddress() {
  92         if (!ssc.isBound())

  93             return null;
  94         return Net.getRevealedLocalAddress(ssc.localAddress()).getAddress();
  95 

  96     }
  97 
  98     public int getLocalPort() {
  99         if (!ssc.isBound())

 100             return -1;
 101         return Net.asInetSocketAddress(ssc.localAddress()).getPort();


 102     }
 103 
 104 
 105     public Socket accept() throws IOException {
 106         synchronized (ssc.blockingLock()) {
 107             try {
 108                 if (!ssc.isBound())
 109                     throw new NotYetBoundException();
 110 
 111                 if (timeout == 0) {

 112                     // for compatibility reasons: accept connection if available
 113                     // when configured non-blocking
 114                     SocketChannel sc = ssc.accept();
 115                     if (sc == null && !ssc.isBlocking())
 116                         throw new IllegalBlockingModeException();
 117                     return sc.socket();
 118                 }
 119 
 120                 if (!ssc.isBlocking())
 121                     throw new IllegalBlockingModeException();
 122                 ssc.configureBlocking(false);
 123                 try {
 124                     SocketChannel sc;
 125                     if ((sc = ssc.accept()) != null)
 126                         return sc.socket();
 127                     long to = timeout;
 128                     for (;;) {
 129                         if (!ssc.isOpen())
 130                             throw new ClosedChannelException();
 131                         long st = System.currentTimeMillis();
 132                         int result = ssc.poll(Net.POLLIN, to);
 133                         if (result > 0 && ((sc = ssc.accept()) != null))
 134                             return sc.socket();
 135                         to -= System.currentTimeMillis() - st;
 136                         if (to <= 0)
 137                             throw new SocketTimeoutException();
 138                     }
 139                 } finally {
 140                     try {
 141                         ssc.configureBlocking(true);
 142                     } catch (ClosedChannelException e) { }
 143                 }
 144             } catch (Exception x) {
 145                 Net.translateException(x);
 146                 assert false;
 147                 return null;            // Never happens
 148             }
 149         }
 150     }
 151 
 152     public void close() throws IOException {
 153         ssc.close();
 154     }
 155 
 156     public ServerSocketChannel getChannel() {
 157         return ssc;
 158     }
 159 
 160     public boolean isBound() {
 161         return ssc.isBound();
 162     }
 163 


 199 
 200     public void setReceiveBufferSize(int size) throws SocketException {
 201         // size 0 valid for ServerSocketChannel, invalid for ServerSocket
 202         if (size <= 0)
 203             throw new IllegalArgumentException("size cannot be 0 or negative");
 204         try {
 205             ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
 206         } catch (IOException x) {
 207             Net.translateToSocketException(x);
 208         }
 209     }
 210 
 211     public int getReceiveBufferSize() throws SocketException {
 212         try {
 213             return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
 214         } catch (IOException x) {
 215             Net.translateToSocketException(x);
 216             return -1;          // Never happens
 217         }
 218     }
 219 
 220 }


  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.IOException;
  29 import java.net.InetAddress;
  30 import java.net.InetSocketAddress;
  31 import java.net.ServerSocket;
  32 import java.net.Socket;
  33 import java.net.SocketAddress;
  34 import java.net.SocketException;
  35 import java.net.SocketTimeoutException;
  36 import java.net.StandardSocketOptions;

  37 import java.nio.channels.IllegalBlockingModeException;
  38 import java.nio.channels.NotYetBoundException;
  39 import java.nio.channels.ServerSocketChannel;
  40 import java.nio.channels.SocketChannel;
  41 
  42 
  43 // Make a server-socket channel look like a server socket.
  44 //
  45 // The methods in this class are defined in exactly the same order as in
  46 // java.net.ServerSocket so as to simplify tracking future changes to that
  47 // class.
  48 //
  49 
  50 class ServerSocketAdaptor                        // package-private
  51     extends ServerSocket
  52 {

  53     // The channel being adapted
  54     private final ServerSocketChannelImpl ssc;
  55 
  56     // Timeout "option" value for accepts
  57     private volatile int timeout;
  58 
  59     public static ServerSocket create(ServerSocketChannelImpl ssc) {
  60         try {
  61             return new ServerSocketAdaptor(ssc);
  62         } catch (IOException x) {
  63             throw new Error(x);
  64         }
  65     }
  66 
  67     // ## super will create a useless impl
  68     private ServerSocketAdaptor(ServerSocketChannelImpl ssc) throws IOException {


  69         this.ssc = ssc;
  70     }
  71 

  72     public void bind(SocketAddress local) throws IOException {
  73         bind(local, 50);
  74     }
  75 
  76     public void bind(SocketAddress local, int backlog) throws IOException {
  77         if (local == null)
  78             local = new InetSocketAddress(0);
  79         try {
  80             ssc.bind(local, backlog);
  81         } catch (Exception x) {
  82             Net.translateException(x);
  83         }
  84     }
  85 
  86     public InetAddress getInetAddress() {
  87         InetSocketAddress local = ssc.localAddress();
  88         if (local == null) {
  89             return null;
  90         } else {
  91             return Net.getRevealedLocalAddress(local).getAddress();
  92         }
  93     }
  94 
  95     public int getLocalPort() {
  96         InetSocketAddress local = ssc.localAddress();
  97         if (local == null) {
  98             return -1;
  99         } else {
 100             return local.getPort();
 101         }
 102     }

 103 
 104     public Socket accept() throws IOException {
 105         synchronized (ssc.blockingLock()) {
 106             try {
 107                 if (!ssc.isBound())
 108                     throw new NotYetBoundException();
 109 
 110                 long to = this.timeout;
 111                 if (to == 0) {
 112                     // for compatibility reasons: accept connection if available
 113                     // when configured non-blocking
 114                     SocketChannel sc = ssc.accept();
 115                     if (sc == null && !ssc.isBlocking())
 116                         throw new IllegalBlockingModeException();
 117                     return sc.socket();
 118                 }
 119 
 120                 if (!ssc.isBlocking())
 121                     throw new IllegalBlockingModeException();






 122                 for (;;) {


 123                     long st = System.currentTimeMillis();
 124                     if (ssc.pollAccept(to))
 125                         return ssc.accept().socket();

 126                     to -= System.currentTimeMillis() - st;
 127                     if (to <= 0)
 128                         throw new SocketTimeoutException();
 129                 }
 130 




 131             } catch (Exception x) {
 132                 Net.translateException(x);
 133                 assert false;
 134                 return null;            // Never happens
 135             }
 136         }
 137     }
 138 
 139     public void close() throws IOException {
 140         ssc.close();
 141     }
 142 
 143     public ServerSocketChannel getChannel() {
 144         return ssc;
 145     }
 146 
 147     public boolean isBound() {
 148         return ssc.isBound();
 149     }
 150 


 186 
 187     public void setReceiveBufferSize(int size) throws SocketException {
 188         // size 0 valid for ServerSocketChannel, invalid for ServerSocket
 189         if (size <= 0)
 190             throw new IllegalArgumentException("size cannot be 0 or negative");
 191         try {
 192             ssc.setOption(StandardSocketOptions.SO_RCVBUF, size);
 193         } catch (IOException x) {
 194             Net.translateToSocketException(x);
 195         }
 196     }
 197 
 198     public int getReceiveBufferSize() throws SocketException {
 199         try {
 200             return ssc.getOption(StandardSocketOptions.SO_RCVBUF).intValue();
 201         } catch (IOException x) {
 202             Net.translateToSocketException(x);
 203             return -1;          // Never happens
 204         }
 205     }

 206 }
< prev index next >