src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
changeset 48761 74c1fa26435a
parent 47216 71c04702a3d5
child 49001 ce06058197a4
equal deleted inserted replaced
48760:25725c11c296 48761:74c1fa26435a
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.io.*;
    28 import java.io.IOException;
    29 import java.net.*;
    29 import java.io.InputStream;
    30 import java.nio.*;
    30 import java.io.OutputStream;
    31 import java.nio.channels.*;
    31 import java.net.InetAddress;
       
    32 import java.net.InetSocketAddress;
       
    33 import java.net.Socket;
       
    34 import java.net.SocketAddress;
       
    35 import java.net.SocketException;
       
    36 import java.net.SocketImpl;
       
    37 import java.net.SocketOption;
       
    38 import java.net.SocketTimeoutException;
       
    39 import java.net.StandardSocketOptions;
       
    40 import java.nio.ByteBuffer;
       
    41 import java.nio.channels.Channels;
       
    42 import java.nio.channels.ClosedChannelException;
       
    43 import java.nio.channels.IllegalBlockingModeException;
       
    44 import java.nio.channels.SocketChannel;
    32 import java.security.AccessController;
    45 import java.security.AccessController;
    33 import java.security.PrivilegedExceptionAction;
    46 import java.security.PrivilegedExceptionAction;
    34 import java.util.concurrent.TimeUnit;
    47 import java.util.concurrent.TimeUnit;
    35 
    48 
    36 // Make a socket channel look like a socket.
    49 // Make a socket channel look like a socket.
    43 //
    56 //
    44 // The methods in this class are defined in exactly the same order as in
    57 // The methods in this class are defined in exactly the same order as in
    45 // java.net.Socket so as to simplify tracking future changes to that class.
    58 // java.net.Socket so as to simplify tracking future changes to that class.
    46 //
    59 //
    47 
    60 
    48 public class SocketAdaptor
    61 class SocketAdaptor
    49     extends Socket
    62     extends Socket
    50 {
    63 {
    51 
    64 
    52     // The channel being adapted
    65     // The channel being adapted
    53     private final SocketChannelImpl sc;
    66     private final SocketChannelImpl sc;
    87         synchronized (sc.blockingLock()) {
   100         synchronized (sc.blockingLock()) {
    88             if (!sc.isBlocking())
   101             if (!sc.isBlocking())
    89                 throw new IllegalBlockingModeException();
   102                 throw new IllegalBlockingModeException();
    90 
   103 
    91             try {
   104             try {
    92 
       
    93                 if (timeout == 0) {
   105                 if (timeout == 0) {
    94                     sc.connect(remote);
   106                     sc.connect(remote);
    95                     return;
   107                     return;
    96                 }
   108                 }
    97 
   109 
   117                             } catch (IOException x) { }
   129                             } catch (IOException x) { }
   118                             throw new SocketTimeoutException();
   130                             throw new SocketTimeoutException();
   119                         }
   131                         }
   120                     }
   132                     }
   121                 } finally {
   133                 } finally {
   122                     if (sc.isOpen())
   134                     try {
   123                         sc.configureBlocking(true);
   135                         sc.configureBlocking(true);
       
   136                     } catch (ClosedChannelException e) { }
   124                 }
   137                 }
   125 
   138 
   126             } catch (Exception x) {
   139             } catch (Exception x) {
   127                 Net.translateException(x, true);
   140                 Net.translateException(x, true);
   128             }
   141             }
   186             throws IOException
   199             throws IOException
   187         {
   200         {
   188             synchronized (sc.blockingLock()) {
   201             synchronized (sc.blockingLock()) {
   189                 if (!sc.isBlocking())
   202                 if (!sc.isBlocking())
   190                     throw new IllegalBlockingModeException();
   203                     throw new IllegalBlockingModeException();
       
   204 
   191                 if (timeout == 0)
   205                 if (timeout == 0)
   192                     return sc.read(bb);
   206                     return sc.read(bb);
       
   207 
   193                 sc.configureBlocking(false);
   208                 sc.configureBlocking(false);
   194 
       
   195                 try {
   209                 try {
   196                     int n;
   210                     int n;
   197                     if ((n = sc.read(bb)) != 0)
   211                     if ((n = sc.read(bb)) != 0)
   198                         return n;
   212                         return n;
   199                     long timeoutNanos =
   213                     long timeoutNanos =
   211                         timeoutNanos -= System.nanoTime() - startTime;
   225                         timeoutNanos -= System.nanoTime() - startTime;
   212                         if (timeoutNanos <= 0)
   226                         if (timeoutNanos <= 0)
   213                             throw new SocketTimeoutException();
   227                             throw new SocketTimeoutException();
   214                     }
   228                     }
   215                 } finally {
   229                 } finally {
   216                     if (sc.isOpen())
   230                     try {
   217                         sc.configureBlocking(true);
   231                         sc.configureBlocking(true);
       
   232                     } catch (ClosedChannelException e) { }
   218                 }
   233                 }
   219 
       
   220             }
   234             }
   221         }
   235         }
   222     }
   236     }
   223 
   237 
   224     private InputStream socketInputStream = null;
   238     private InputStream socketInputStream = null;