src/java.base/share/classes/sun/nio/ch/SocketAdaptor.java
branchunixdomainchannels
changeset 58801 119ac9128c1b
parent 58614 29624901d8bc
equal deleted inserted replaced
58799:eb491334113f 58801:119ac9128c1b
    51 
    51 
    52 class SocketAdaptor
    52 class SocketAdaptor
    53     extends Socket
    53     extends Socket
    54 {
    54 {
    55     // The channel being adapted
    55     // The channel being adapted
    56     private final SocketChannelImpl sc;
    56     private final InetSocketChannelImpl sc;
    57 
    57 
    58     // Timeout "option" value for reads
    58     // Timeout "option" value for reads
    59     private volatile int timeout;
    59     private volatile int timeout;
    60 
    60 
    61     private SocketAdaptor(SocketChannelImpl sc) throws SocketException {
    61     private SocketAdaptor(InetSocketChannelImpl sc) throws SocketException {
    62         super(DummySocketImpl.create());
    62         super(DummySocketImpl.create());
    63         this.sc = sc;
    63         this.sc = sc;
    64     }
    64     }
    65 
    65 
    66     static Socket create(SocketChannelImpl sc) {
    66     static Socket create(InetSocketChannelImpl sc) {
    67         PrivilegedExceptionAction<Socket> pa = () -> new SocketAdaptor(sc);
    67         PrivilegedExceptionAction<Socket> pa = () -> new SocketAdaptor(sc);
    68         try {
    68         try {
    69             return AccessController.doPrivileged(pa);
    69             return AccessController.doPrivileged(pa);
    70         } catch (PrivilegedActionException pae) {
    70         } catch (PrivilegedActionException pae) {
    71             throw new InternalError("Should not reach here", pae);
    71             throw new InternalError("Should not reach here", pae);
   104         }
   104         }
   105     }
   105     }
   106 
   106 
   107     @Override
   107     @Override
   108     public InetAddress getInetAddress() {
   108     public InetAddress getInetAddress() {
   109         InetSocketAddress remote = sc.remoteAddress();
   109         InetSocketAddress remote = (InetSocketAddress)sc.remoteAddress();
   110         if (remote == null) {
   110         if (remote == null) {
   111             return null;
   111             return null;
   112         } else {
   112         } else {
   113             return remote.getAddress();
   113             return remote.getAddress();
   114         }
   114         }
   115     }
   115     }
   116 
   116 
   117     @Override
   117     @Override
   118     public InetAddress getLocalAddress() {
   118     public InetAddress getLocalAddress() {
   119         if (sc.isOpen()) {
   119         if (sc.isOpen()) {
   120             InetSocketAddress local = sc.localAddress();
   120             InetSocketAddress local = (InetSocketAddress)sc.localAddress();
   121             if (local != null) {
   121             if (local != null) {
   122                 return Net.getRevealedLocalAddress(local).getAddress();
   122                 return Net.getRevealedLocalAddress(local).getAddress();
   123             }
   123             }
   124         }
   124         }
   125         return new InetSocketAddress(0).getAddress();
   125         return new InetSocketAddress(0).getAddress();
   126     }
   126     }
   127 
   127 
   128     @Override
   128     @Override
   129     public int getPort() {
   129     public int getPort() {
   130         InetSocketAddress remote = sc.remoteAddress();
   130         InetSocketAddress remote = (InetSocketAddress)sc.remoteAddress();
   131         if (remote == null) {
   131         if (remote == null) {
   132             return 0;
   132             return 0;
   133         } else {
   133         } else {
   134             return remote.getPort();
   134             return remote.getPort();
   135         }
   135         }
   136     }
   136     }
   137 
   137 
   138     @Override
   138     @Override
   139     public int getLocalPort() {
   139     public int getLocalPort() {
   140         InetSocketAddress local = sc.localAddress();
   140         InetSocketAddress local = (InetSocketAddress)sc.localAddress();
   141         if (local == null) {
   141         if (local == null) {
   142             return -1;
   142             return -1;
   143         } else {
   143         } else {
   144             return local.getPort();
   144             return local.getPort();
   145         }
   145         }
   150         return sc.remoteAddress();
   150         return sc.remoteAddress();
   151     }
   151     }
   152 
   152 
   153     @Override
   153     @Override
   154     public SocketAddress getLocalSocketAddress() {
   154     public SocketAddress getLocalSocketAddress() {
   155         InetSocketAddress local = sc.localAddress();
   155         InetSocketAddress local = (InetSocketAddress)sc.localAddress();
   156         if (local != null) {
   156         if (local != null) {
   157             return Net.getRevealedLocalAddress(local);
   157             return Net.getRevealedLocalAddress(local);
   158         } else {
   158         } else {
   159             return null;
   159             return null;
   160         }
   160         }