|
27 | 27 |
|
28 | 28 | import java.util.Arrays;
|
29 | 29 | import java.util.Enumeration;
|
| 30 | +import java.util.List; |
30 | 31 | import java.util.NoSuchElementException;
|
31 | 32 | import java.util.Objects;
|
32 | 33 | import java.util.Spliterator;
|
@@ -79,9 +80,9 @@ public final class NetworkInterface {
|
79 | 80 | private String name;
|
80 | 81 | private String displayName;
|
81 | 82 | private int index;
|
82 |
| - private InetAddress addrs[]; |
83 |
| - private InterfaceAddress bindings[]; |
84 |
| - private NetworkInterface childs[]; |
| 83 | + private InetAddress[] addrs; |
| 84 | + private InterfaceAddress[] bindings; |
| 85 | + private NetworkInterface[] childs; |
85 | 86 | private NetworkInterface parent = null;
|
86 | 87 | private boolean virtual = false;
|
87 | 88 | private static final NetworkInterface defaultInterface;
|
@@ -118,87 +119,46 @@ public String getName() {
|
118 | 119 | }
|
119 | 120 |
|
120 | 121 | /**
|
121 |
| - * Get an Enumeration with all, or a subset, of the InetAddresses bound to |
122 |
| - * this network interface. |
| 122 | + * Get an Enumeration of the InetAddresses bound to this network interface. |
123 | 123 | *
|
124 | 124 | * @implNote
|
125 |
| - * The returned enumeration contains all, or a subset, of the InetAddresses that were |
126 |
| - * bound to the interface at the time the {@linkplain #getNetworkInterfaces() |
| 125 | + * The returned enumeration contains the InetAddresses that were bound to |
| 126 | + * the interface at the time the {@linkplain #getNetworkInterfaces() |
127 | 127 | * interface configuration was read}
|
128 | 128 | *
|
129 |
| - * @return an Enumeration object with all, or a subset, of the InetAddresses |
130 |
| - * bound to this network interface |
| 129 | + * @return an Enumeration object with the InetAddresses bound to this |
| 130 | + * network interface |
131 | 131 | * @see #inetAddresses()
|
132 | 132 | */
|
133 | 133 | public Enumeration<InetAddress> getInetAddresses() {
|
134 |
| - return enumerationFromArray(getCheckedInetAddresses()); |
| 134 | + return enumerationFromArray(addrs); |
135 | 135 | }
|
136 | 136 |
|
137 | 137 | /**
|
138 |
| - * Get a Stream of all, or a subset, of the InetAddresses bound to this |
139 |
| - * network interface. |
| 138 | + * Get a Stream of the InetAddresses bound to this network interface. |
140 | 139 | *
|
141 | 140 | * @implNote
|
142 |
| - * The stream contains all, or a subset, of the InetAddresses that were |
143 |
| - * bound to the interface at the time the {@linkplain #getNetworkInterfaces() |
| 141 | + * The stream contains the InetAddresses that were bound to the |
| 142 | + * interface at the time the {@linkplain #getNetworkInterfaces() |
144 | 143 | * interface configuration was read}
|
145 | 144 | *
|
146 |
| - * @return a Stream object with all, or a subset, of the InetAddresses |
147 |
| - * bound to this network interface |
| 145 | + * @return a Stream object with the InetAddresses bound to this network interface |
148 | 146 | * @since 9
|
149 | 147 | */
|
150 | 148 | public Stream<InetAddress> inetAddresses() {
|
151 |
| - return streamFromArray(getCheckedInetAddresses()); |
152 |
| - } |
153 |
| - |
154 |
| - private InetAddress[] getCheckedInetAddresses() { |
155 |
| - InetAddress[] local_addrs = new InetAddress[addrs.length]; |
156 |
| - boolean trusted = true; |
157 |
| - |
158 |
| - @SuppressWarnings("removal") |
159 |
| - SecurityManager sec = System.getSecurityManager(); |
160 |
| - if (sec != null) { |
161 |
| - try { |
162 |
| - sec.checkPermission(new NetPermission("getNetworkInformation")); |
163 |
| - } catch (SecurityException e) { |
164 |
| - trusted = false; |
165 |
| - } |
166 |
| - } |
167 |
| - int i = 0; |
168 |
| - for (int j = 0; j < addrs.length; j++) { |
169 |
| - try { |
170 |
| - if (!trusted) { |
171 |
| - sec.checkConnect(addrs[j].getHostAddress(), -1); |
172 |
| - } |
173 |
| - local_addrs[i++] = addrs[j]; |
174 |
| - } catch (SecurityException e) { } |
175 |
| - } |
176 |
| - return Arrays.copyOf(local_addrs, i); |
| 149 | + return streamFromArray(addrs); |
177 | 150 | }
|
178 | 151 |
|
179 | 152 | /**
|
180 |
| - * Get a List of all, or a subset, of the {@code InterfaceAddresses} |
181 |
| - * of this network interface. |
| 153 | + * Get a List of the {@code InterfaceAddresses} of this network interface. |
| 154 | + * |
| 155 | + * @return a {@code List} object with the InterfaceAddress of this |
| 156 | + * network interface |
182 | 157 | *
|
183 |
| - * @return a {@code List} object with all, or a subset, of the |
184 |
| - * InterfaceAddress of this network interface |
185 | 158 | * @since 1.6
|
186 | 159 | */
|
187 |
| - public java.util.List<InterfaceAddress> getInterfaceAddresses() { |
188 |
| - java.util.List<InterfaceAddress> lst = new java.util.ArrayList<>(1); |
189 |
| - if (bindings != null) { |
190 |
| - @SuppressWarnings("removal") |
191 |
| - SecurityManager sec = System.getSecurityManager(); |
192 |
| - for (int j=0; j<bindings.length; j++) { |
193 |
| - try { |
194 |
| - if (sec != null) { |
195 |
| - sec.checkConnect(bindings[j].getAddress().getHostAddress(), -1); |
196 |
| - } |
197 |
| - lst.add(bindings[j]); |
198 |
| - } catch (SecurityException e) { } |
199 |
| - } |
200 |
| - } |
201 |
| - return lst; |
| 160 | + public List<InterfaceAddress> getInterfaceAddresses() { |
| 161 | + return bindings == null ? List.of() : List.of(bindings); |
202 | 162 | }
|
203 | 163 |
|
204 | 164 | /**
|
@@ -556,18 +516,6 @@ public boolean supportsMulticast() throws SocketException {
|
556 | 516 | * @since 1.6
|
557 | 517 | */
|
558 | 518 | public byte[] getHardwareAddress() throws SocketException {
|
559 |
| - @SuppressWarnings("removal") |
560 |
| - SecurityManager sec = System.getSecurityManager(); |
561 |
| - if (sec != null) { |
562 |
| - try { |
563 |
| - sec.checkPermission(new NetPermission("getNetworkInformation")); |
564 |
| - } catch (SecurityException e) { |
565 |
| - if (!getInetAddresses().hasMoreElements()) { |
566 |
| - // don't have connect permission to any local address |
567 |
| - return null; |
568 |
| - } |
569 |
| - } |
570 |
| - } |
571 | 519 | if (isLoopback0(name, index)) {
|
572 | 520 | return null;
|
573 | 521 | }
|
|
0 commit comments