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

Print this page
rev 14282 : 8044773: Refactor jdk.net API so that it can be moved out of the base module
Reviewed-by:


  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.nio.ByteBuffer;
  29 import java.nio.channels.*;
  30 import java.net.SocketOption;
  31 import java.net.StandardSocketOptions;
  32 import java.net.SocketAddress;
  33 import java.net.InetSocketAddress;
  34 import java.io.IOException;
  35 import java.io.FileDescriptor;
  36 import java.util.Set;
  37 import java.util.HashSet;
  38 import java.util.Collections;
  39 import java.util.concurrent.*;
  40 import java.util.concurrent.locks.*;
  41 import sun.net.NetHooks;
  42 import sun.net.ExtendedOptionsImpl;
  43 
  44 /**
  45  * Base implementation of AsynchronousSocketChannel
  46  */
  47 
  48 abstract class AsynchronousSocketChannelImpl
  49     extends AsynchronousSocketChannel
  50     implements Cancellable, Groupable
  51 {
  52     protected final FileDescriptor fd;
  53 
  54     // protects state, localAddress, and remoteAddress
  55     protected final Object stateLock = new Object();
  56 
  57     protected volatile InetSocketAddress localAddress;
  58     protected volatile InetSocketAddress remoteAddress;
  59 
  60     // State, increases monotonically
  61     static final int ST_UNINITIALIZED = -1;
  62     static final int ST_UNCONNECTED = 0;


 495             }
 496             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
 497         } finally {
 498             end();
 499         }
 500     }
 501 
 502     private static class DefaultOptionsHolder {
 503         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
 504 
 505         private static Set<SocketOption<?>> defaultOptions() {
 506             HashSet<SocketOption<?>> set = new HashSet<>(5);
 507             set.add(StandardSocketOptions.SO_SNDBUF);
 508             set.add(StandardSocketOptions.SO_RCVBUF);
 509             set.add(StandardSocketOptions.SO_KEEPALIVE);
 510             set.add(StandardSocketOptions.SO_REUSEADDR);
 511             if (Net.isReusePortAvailable()) {
 512                 set.add(StandardSocketOptions.SO_REUSEPORT);
 513             }
 514             set.add(StandardSocketOptions.TCP_NODELAY);
 515             if (ExtendedOptionsImpl.flowSupported()) {
 516                 set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
 517             }
 518             return Collections.unmodifiableSet(set);
 519         }
 520     }
 521 
 522     @Override
 523     public final Set<SocketOption<?>> supportedOptions() {
 524         return DefaultOptionsHolder.defaultOptions;
 525     }
 526 
 527     @Override
 528     public final SocketAddress getRemoteAddress() throws IOException {
 529         if (!isOpen())
 530             throw new ClosedChannelException();
 531         return remoteAddress;
 532     }
 533 
 534     @Override
 535     public final AsynchronousSocketChannel shutdownInput() throws IOException {
 536         try {
 537             begin();




  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.nio.ByteBuffer;
  29 import java.nio.channels.*;
  30 import java.net.SocketOption;
  31 import java.net.StandardSocketOptions;
  32 import java.net.SocketAddress;
  33 import java.net.InetSocketAddress;
  34 import java.io.IOException;
  35 import java.io.FileDescriptor;
  36 import java.util.Set;
  37 import java.util.HashSet;
  38 import java.util.Collections;
  39 import java.util.concurrent.*;
  40 import java.util.concurrent.locks.*;
  41 import sun.net.NetHooks;
  42 import sun.net.ext.ExtendedSocketOptions;
  43 
  44 /**
  45  * Base implementation of AsynchronousSocketChannel
  46  */
  47 
  48 abstract class AsynchronousSocketChannelImpl
  49     extends AsynchronousSocketChannel
  50     implements Cancellable, Groupable
  51 {
  52     protected final FileDescriptor fd;
  53 
  54     // protects state, localAddress, and remoteAddress
  55     protected final Object stateLock = new Object();
  56 
  57     protected volatile InetSocketAddress localAddress;
  58     protected volatile InetSocketAddress remoteAddress;
  59 
  60     // State, increases monotonically
  61     static final int ST_UNINITIALIZED = -1;
  62     static final int ST_UNCONNECTED = 0;


 495             }
 496             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
 497         } finally {
 498             end();
 499         }
 500     }
 501 
 502     private static class DefaultOptionsHolder {
 503         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
 504 
 505         private static Set<SocketOption<?>> defaultOptions() {
 506             HashSet<SocketOption<?>> set = new HashSet<>(5);
 507             set.add(StandardSocketOptions.SO_SNDBUF);
 508             set.add(StandardSocketOptions.SO_RCVBUF);
 509             set.add(StandardSocketOptions.SO_KEEPALIVE);
 510             set.add(StandardSocketOptions.SO_REUSEADDR);
 511             if (Net.isReusePortAvailable()) {
 512                 set.add(StandardSocketOptions.SO_REUSEPORT);
 513             }
 514             set.add(StandardSocketOptions.TCP_NODELAY);
 515             ExtendedSocketOptions extendedOptions =
 516                     ExtendedSocketOptions.getInstance();
 517             set.addAll(extendedOptions.options());
 518             return Collections.unmodifiableSet(set);
 519         }
 520     }
 521 
 522     @Override
 523     public final Set<SocketOption<?>> supportedOptions() {
 524         return DefaultOptionsHolder.defaultOptions;
 525     }
 526 
 527     @Override
 528     public final SocketAddress getRemoteAddress() throws IOException {
 529         if (!isOpen())
 530             throw new ClosedChannelException();
 531         return remoteAddress;
 532     }
 533 
 534     @Override
 535     public final AsynchronousSocketChannel shutdownInput() throws IOException {
 536         try {
 537             begin();