diff --git a/src/java.base/share/classes/java/net/NetworkInterface.java b/src/java.base/share/classes/java/net/NetworkInterface.java index 1c5edd1c23f..29a07d9b76d 100644 --- a/src/java.base/share/classes/java/net/NetworkInterface.java +++ b/src/java.base/share/classes/java/net/NetworkInterface.java @@ -27,6 +27,7 @@ package java.net; import java.util.Arrays; import java.util.Enumeration; +import java.util.List; import java.util.NoSuchElementException; import java.util.Objects; import java.util.Spliterator; @@ -79,9 +80,9 @@ public final class NetworkInterface { private String name; private String displayName; private int index; - private InetAddress addrs[]; - private InterfaceAddress bindings[]; - private NetworkInterface childs[]; + private InetAddress[] addrs; + private InterfaceAddress[] bindings; + private NetworkInterface[] childs; private NetworkInterface parent = null; private boolean virtual = false; private static final NetworkInterface defaultInterface; @@ -118,87 +119,46 @@ public final class NetworkInterface { } /** - * Get an Enumeration with all, or a subset, of the InetAddresses bound to - * this network interface. + * Get an Enumeration of the InetAddresses bound to this network interface. * * @implNote - * The returned enumeration contains all, or a subset, of the InetAddresses that were - * bound to the interface at the time the {@linkplain #getNetworkInterfaces() + * The returned enumeration contains the InetAddresses that were bound to + * the interface at the time the {@linkplain #getNetworkInterfaces() * interface configuration was read} * - * @return an Enumeration object with all, or a subset, of the InetAddresses - * bound to this network interface + * @return an Enumeration object with the InetAddresses bound to this + * network interface * @see #inetAddresses() */ public Enumeration getInetAddresses() { - return enumerationFromArray(getCheckedInetAddresses()); + return enumerationFromArray(addrs); } /** - * Get a Stream of all, or a subset, of the InetAddresses bound to this - * network interface. + * Get a Stream of the InetAddresses bound to this network interface. * * @implNote - * The stream contains all, or a subset, of the InetAddresses that were - * bound to the interface at the time the {@linkplain #getNetworkInterfaces() + * The stream contains the InetAddresses that were bound to the + * interface at the time the {@linkplain #getNetworkInterfaces() * interface configuration was read} * - * @return a Stream object with all, or a subset, of the InetAddresses - * bound to this network interface + * @return a Stream object with the InetAddresses bound to this network interface * @since 9 */ public Stream inetAddresses() { - return streamFromArray(getCheckedInetAddresses()); - } - - private InetAddress[] getCheckedInetAddresses() { - InetAddress[] local_addrs = new InetAddress[addrs.length]; - boolean trusted = true; - - @SuppressWarnings("removal") - SecurityManager sec = System.getSecurityManager(); - if (sec != null) { - try { - sec.checkPermission(new NetPermission("getNetworkInformation")); - } catch (SecurityException e) { - trusted = false; - } - } - int i = 0; - for (int j = 0; j < addrs.length; j++) { - try { - if (!trusted) { - sec.checkConnect(addrs[j].getHostAddress(), -1); - } - local_addrs[i++] = addrs[j]; - } catch (SecurityException e) { } - } - return Arrays.copyOf(local_addrs, i); + return streamFromArray(addrs); } /** - * Get a List of all, or a subset, of the {@code InterfaceAddresses} - * of this network interface. + * Get a List of the {@code InterfaceAddresses} of this network interface. + * + * @return a {@code List} object with the InterfaceAddress of this + * network interface * - * @return a {@code List} object with all, or a subset, of the - * InterfaceAddress of this network interface * @since 1.6 */ - public java.util.List getInterfaceAddresses() { - java.util.List lst = new java.util.ArrayList<>(1); - if (bindings != null) { - @SuppressWarnings("removal") - SecurityManager sec = System.getSecurityManager(); - for (int j=0; j getInterfaceAddresses() { + return bindings == null ? List.of() : List.of(bindings); } /** @@ -556,18 +516,6 @@ public final class NetworkInterface { * @since 1.6 */ public byte[] getHardwareAddress() throws SocketException { - @SuppressWarnings("removal") - SecurityManager sec = System.getSecurityManager(); - if (sec != null) { - try { - sec.checkPermission(new NetPermission("getNetworkInformation")); - } catch (SecurityException e) { - if (!getInetAddresses().hasMoreElements()) { - // don't have connect permission to any local address - return null; - } - } - } if (isLoopback0(name, index)) { return null; }