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

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


  16  *
  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.FileDescriptor;
  29 import java.io.IOException;
  30 import java.net.*;
  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.*;
  33 import java.nio.channels.spi.*;
  34 import java.util.*;
  35 import sun.net.ResourceManager;
  36 import sun.net.ExtendedOptionsImpl;
  37 
  38 /**
  39  * An implementation of DatagramChannels.
  40  */
  41 
  42 class DatagramChannelImpl
  43     extends DatagramChannel
  44     implements SelChImpl
  45 {
  46 
  47     // Used to make native read and write calls
  48     private static NativeDispatcher nd = new DatagramDispatcher();
  49 
  50     // Our file descriptor
  51     private final FileDescriptor fd;
  52 
  53     // fd value needed for dev/poll. This value will remain valid
  54     // even after the value in the file descriptor object has been set to -1
  55     private final int fdVal;
  56 


 289             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
 290         }
 291     }
 292 
 293     private static class DefaultOptionsHolder {
 294         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
 295 
 296         private static Set<SocketOption<?>> defaultOptions() {
 297             HashSet<SocketOption<?>> set = new HashSet<>(8);
 298             set.add(StandardSocketOptions.SO_SNDBUF);
 299             set.add(StandardSocketOptions.SO_RCVBUF);
 300             set.add(StandardSocketOptions.SO_REUSEADDR);
 301             if (Net.isReusePortAvailable()) {
 302                 set.add(StandardSocketOptions.SO_REUSEPORT);
 303             }
 304             set.add(StandardSocketOptions.SO_BROADCAST);
 305             set.add(StandardSocketOptions.IP_TOS);
 306             set.add(StandardSocketOptions.IP_MULTICAST_IF);
 307             set.add(StandardSocketOptions.IP_MULTICAST_TTL);
 308             set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
 309             if (ExtendedOptionsImpl.flowSupported()) {
 310                 set.add(jdk.net.ExtendedSocketOptions.SO_FLOW_SLA);
 311             }
 312             return Collections.unmodifiableSet(set);
 313         }
 314     }
 315 
 316     @Override
 317     public final Set<SocketOption<?>> supportedOptions() {
 318         return DefaultOptionsHolder.defaultOptions;
 319     }
 320 
 321     private void ensureOpen() throws ClosedChannelException {
 322         if (!isOpen())
 323             throw new ClosedChannelException();
 324     }
 325 
 326     private SocketAddress sender;       // Set by receive0 (## ugh)
 327 
 328     public SocketAddress receive(ByteBuffer dst) throws IOException {
 329         if (dst.isReadOnly())
 330             throw new IllegalArgumentException("Read-only buffer");
 331         if (dst == null)




  16  *
  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.FileDescriptor;
  29 import java.io.IOException;
  30 import java.net.*;
  31 import java.nio.ByteBuffer;
  32 import java.nio.channels.*;
  33 import java.nio.channels.spi.*;
  34 import java.util.*;
  35 import sun.net.ResourceManager;
  36 import sun.net.ext.ExtendedSocketOptions;
  37 
  38 /**
  39  * An implementation of DatagramChannels.
  40  */
  41 
  42 class DatagramChannelImpl
  43     extends DatagramChannel
  44     implements SelChImpl
  45 {
  46 
  47     // Used to make native read and write calls
  48     private static NativeDispatcher nd = new DatagramDispatcher();
  49 
  50     // Our file descriptor
  51     private final FileDescriptor fd;
  52 
  53     // fd value needed for dev/poll. This value will remain valid
  54     // even after the value in the file descriptor object has been set to -1
  55     private final int fdVal;
  56 


 289             return (T) Net.getSocketOption(fd, Net.UNSPEC, name);
 290         }
 291     }
 292 
 293     private static class DefaultOptionsHolder {
 294         static final Set<SocketOption<?>> defaultOptions = defaultOptions();
 295 
 296         private static Set<SocketOption<?>> defaultOptions() {
 297             HashSet<SocketOption<?>> set = new HashSet<>(8);
 298             set.add(StandardSocketOptions.SO_SNDBUF);
 299             set.add(StandardSocketOptions.SO_RCVBUF);
 300             set.add(StandardSocketOptions.SO_REUSEADDR);
 301             if (Net.isReusePortAvailable()) {
 302                 set.add(StandardSocketOptions.SO_REUSEPORT);
 303             }
 304             set.add(StandardSocketOptions.SO_BROADCAST);
 305             set.add(StandardSocketOptions.IP_TOS);
 306             set.add(StandardSocketOptions.IP_MULTICAST_IF);
 307             set.add(StandardSocketOptions.IP_MULTICAST_TTL);
 308             set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
 309             ExtendedSocketOptions extendedOptions =
 310                     ExtendedSocketOptions.getInstance();
 311             set.addAll(extendedOptions.options());
 312             return Collections.unmodifiableSet(set);
 313         }
 314     }
 315 
 316     @Override
 317     public final Set<SocketOption<?>> supportedOptions() {
 318         return DefaultOptionsHolder.defaultOptions;
 319     }
 320 
 321     private void ensureOpen() throws ClosedChannelException {
 322         if (!isOpen())
 323             throw new ClosedChannelException();
 324     }
 325 
 326     private SocketAddress sender;       // Set by receive0 (## ugh)
 327 
 328     public SocketAddress receive(ByteBuffer dst) throws IOException {
 329         if (dst.isReadOnly())
 330             throw new IllegalArgumentException("Read-only buffer");
 331         if (dst == null)