8234103: DatagramSocketImpl::socket is not needed
authorpconcannon
Wed, 20 Nov 2019 10:13:10 +0000
changeset 59200 a686b67a59d9
parent 59199 a096dfdcabde
child 59201 b24f4caa1411
8234103: DatagramSocketImpl::socket is not needed Summary: DatagramSocketImpl has a socket field that links back to the DatagramSocket. This is only used to figure out whether multicasting is supported or not. This fix replaces it with a boolean isMulticast. Reviewed-by: alanb, chegar, dfuchs
src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
src/java.base/share/classes/java/net/DatagramSocket.java
src/java.base/share/classes/java/net/DatagramSocketImpl.java
src/java.base/unix/classes/java/net/DefaultDatagramSocketImplFactory.java
src/java.base/unix/classes/java/net/PlainDatagramSocketImpl.java
src/java.base/windows/classes/java/net/DefaultDatagramSocketImplFactory.java
src/java.base/windows/classes/java/net/DualStackPlainDatagramSocketImpl.java
src/java.base/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java
--- a/src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Wed Nov 20 10:13:10 2019 +0000
@@ -54,6 +54,7 @@
     private int trafficClass = 0;
     protected InetAddress connectedAddress = null;
     private int connectedPort = -1;
+    private final boolean isMulticast;
 
     private static final String os =
             GetPropertyAction.privilegedGetProperty("os.name");
@@ -84,6 +85,10 @@
         return isReusePortAvailable;
     }
 
+    AbstractPlainDatagramSocketImpl(boolean isMulticast) {
+        this.isMulticast = isMulticast;
+    }
+
     /**
      * Creates a datagram socket
      */
@@ -406,6 +411,7 @@
         options.add(StandardSocketOptions.SO_SNDBUF);
         options.add(StandardSocketOptions.SO_RCVBUF);
         options.add(StandardSocketOptions.SO_REUSEADDR);
+        options.add(StandardSocketOptions.SO_BROADCAST);
         options.add(StandardSocketOptions.IP_TOS);
         if (isReusePortAvailable())
             options.add(StandardSocketOptions.SO_REUSEPORT);
@@ -418,6 +424,7 @@
         options.add(StandardSocketOptions.SO_SNDBUF);
         options.add(StandardSocketOptions.SO_RCVBUF);
         options.add(StandardSocketOptions.SO_REUSEADDR);
+        options.add(StandardSocketOptions.SO_BROADCAST);
         options.add(StandardSocketOptions.IP_TOS);
         options.add(StandardSocketOptions.IP_MULTICAST_IF);
         options.add(StandardSocketOptions.IP_MULTICAST_TTL);
@@ -430,7 +437,7 @@
 
     @Override
     protected Set<SocketOption<?>> supportedOptions() {
-        if (getDatagramSocket() instanceof MulticastSocket)
+        if (isMulticast)
             return multicastSocketOptions;
         else
             return datagramSocketOptions;
@@ -460,6 +467,8 @@
             setOption(SocketOptions.SO_REUSEADDR, value);
         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
             setOption(SocketOptions.SO_REUSEPORT, value);
+        } else if (name == StandardSocketOptions.SO_BROADCAST) {
+            setOption(SocketOptions.SO_BROADCAST, value);
         } else if (name == StandardSocketOptions.IP_TOS) {
             int i = ((Integer)value).intValue();
             if (i < 0 || i > 255)
@@ -499,6 +508,8 @@
             return (T) getOption(SocketOptions.SO_REUSEADDR);
         } else if (name == StandardSocketOptions.SO_REUSEPORT) {
             return (T) getOption(SocketOptions.SO_REUSEPORT);
+        } else if (name == StandardSocketOptions.SO_BROADCAST) {
+            return (T) getOption(SocketOptions.SO_BROADCAST);
         } else if (name == StandardSocketOptions.IP_TOS) {
             return (T) getOption(SocketOptions.IP_TOS);
         } else if (name == StandardSocketOptions.IP_MULTICAST_IF) {
--- a/src/java.base/share/classes/java/net/DatagramSocket.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/share/classes/java/net/DatagramSocket.java	Wed Nov 20 10:13:10 2019 +0000
@@ -338,7 +338,6 @@
         }
         // creates a udp socket
         impl.create();
-        impl.setDatagramSocket(this);
         created = true;
     }
 
--- a/src/java.base/share/classes/java/net/DatagramSocketImpl.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/share/classes/java/net/DatagramSocketImpl.java	Wed Nov 20 10:13:10 2019 +0000
@@ -48,20 +48,6 @@
      */
     protected FileDescriptor fd;
 
-    /**
-     * The DatagramSocket or MulticastSocket
-     * that owns this impl
-     */
-    DatagramSocket socket;
-
-    void setDatagramSocket(DatagramSocket socket) {
-        this.socket = socket;
-    }
-
-    DatagramSocket getDatagramSocket() {
-        return socket;
-    }
-
     int dataAvailable() {
         // default impl returns zero, which disables the calling
         // functionality
--- a/src/java.base/unix/classes/java/net/DefaultDatagramSocketImplFactory.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/unix/classes/java/net/DefaultDatagramSocketImplFactory.java	Wed Nov 20 10:13:10 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -69,7 +69,7 @@
                 throw new SocketException("can't instantiate DatagramSocketImpl");
             }
         } else {
-            return new java.net.PlainDatagramSocketImpl();
+            return new java.net.PlainDatagramSocketImpl(isMulticast);
         }
     }
 }
--- a/src/java.base/unix/classes/java/net/PlainDatagramSocketImpl.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/unix/classes/java/net/PlainDatagramSocketImpl.java	Wed Nov 20 10:13:10 2019 +0000
@@ -37,6 +37,10 @@
 
 class PlainDatagramSocketImpl extends AbstractPlainDatagramSocketImpl
 {
+    PlainDatagramSocketImpl(boolean isMulticast) {
+        super(isMulticast);
+    }
+
     static {
         init();
     }
--- a/src/java.base/windows/classes/java/net/DefaultDatagramSocketImplFactory.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/windows/classes/java/net/DefaultDatagramSocketImplFactory.java	Wed Nov 20 10:13:10 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -100,7 +100,7 @@
             if (!preferIPv4Stack && !isMulticast)
                 return new DualStackPlainDatagramSocketImpl(exclusiveBind);
             else
-                return new TwoStacksPlainDatagramSocketImpl(exclusiveBind && !isMulticast);
+                return new TwoStacksPlainDatagramSocketImpl(exclusiveBind && !isMulticast, isMulticast);
         }
     }
 }
--- a/src/java.base/windows/classes/java/net/DualStackPlainDatagramSocketImpl.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/windows/classes/java/net/DualStackPlainDatagramSocketImpl.java	Wed Nov 20 10:13:10 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -62,6 +62,7 @@
     private boolean isReuseAddress;
 
     DualStackPlainDatagramSocketImpl(boolean exclBind) {
+        super(false);
         exclusiveBind = exclBind;
     }
 
--- a/src/java.base/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java	Thu Nov 21 11:31:58 2019 +0000
+++ b/src/java.base/windows/classes/java/net/TwoStacksPlainDatagramSocketImpl.java	Wed Nov 20 10:13:10 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -78,7 +78,8 @@
     // emulates SO_REUSEADDR when exclusiveBind is true and socket is bound
     private boolean isReuseAddress;
 
-    TwoStacksPlainDatagramSocketImpl(boolean exclBind) {
+    TwoStacksPlainDatagramSocketImpl(boolean exclBind, boolean isMulticast) {
+        super(isMulticast);
         exclusiveBind = exclBind;
     }