113 * bound to this network interface
114 * @see #inetAddresses()
115 */
116 public Enumeration<InetAddress> getInetAddresses() {
117 return enumerationFromArray(getCheckedInetAddresses());
118 }
119
120 /**
121 * Get a Stream of all or a subset of the InetAddresses bound to this
122 * network interface.
123 * <p>
124 * If there is a security manager, its {@code checkConnect}
125 * method is called for each InetAddress. Only InetAddresses where
126 * the {@code checkConnect} doesn't throw a SecurityException will be
127 * returned in the Stream. However, if the caller has the
128 * {@link NetPermission}("getNetworkInformation") permission, then all
129 * InetAddresses are returned.
130 *
131 * @return a Stream object with all or a subset of the InetAddresses
132 * bound to this network interface
133 * @since 1.9
134 */
135 public Stream<InetAddress> inetAddresses() {
136 return streamFromArray(getCheckedInetAddresses());
137 }
138
139 private InetAddress[] getCheckedInetAddresses() {
140 InetAddress[] local_addrs = new InetAddress[addrs.length];
141 boolean trusted = true;
142
143 SecurityManager sec = System.getSecurityManager();
144 if (sec != null) {
145 try {
146 sec.checkPermission(new NetPermission("getNetworkInformation"));
147 } catch (SecurityException e) {
148 trusted = false;
149 }
150 }
151 int i = 0;
152 for (int j = 0; j < addrs.length; j++) {
153 try {
191 * Get an Enumeration with all the subinterfaces (also known as virtual
192 * interfaces) attached to this network interface.
193 * <p>
194 * For instance eth0:1 will be a subinterface to eth0.
195 *
196 * @return an Enumeration object with all of the subinterfaces
197 * of this network interface
198 * @see #subInterfaces()
199 * @since 1.6
200 */
201 public Enumeration<NetworkInterface> getSubInterfaces() {
202 return enumerationFromArray(childs);
203 }
204
205 /**
206 * Get a Stream of all subinterfaces (also known as virtual
207 * interfaces) attached to this network interface.
208 *
209 * @return a Stream object with all of the subinterfaces
210 * of this network interface
211 * @since 1.9
212 */
213 public Stream<NetworkInterface> subInterfaces() {
214 return streamFromArray(childs);
215 }
216
217 /**
218 * Returns the parent NetworkInterface of this interface if this is
219 * a subinterface, or {@code null} if it is a physical
220 * (non virtual) interface or has no parent.
221 *
222 * @return The {@code NetworkInterface} this interface is attached to.
223 * @since 1.6
224 */
225 public NetworkInterface getParent() {
226 return parent;
227 }
228
229 /**
230 * Returns the index of this network interface. The index is an integer greater
231 * or equal to zero, or {@code -1} for unknown. This is a system specific value
345
346 return enumerationFromArray(netifs);
347 }
348
349 /**
350 * Returns a {@code Stream} of all the interfaces on this machine. The
351 * {@code Stream} contains at least one interface, possibly representing a
352 * loopback interface that only supports communication between entities on
353 * this machine.
354 *
355 * @apiNote this method can be used in combination with
356 * {@link #inetAddresses()}} to obtain a stream of all IP addresses for
357 * this node, for example:
358 * <pre> {@code
359 * Stream<InetAddress> addrs = NetworkInterface.networkInterfaces()
360 * .flatMap(NetworkInterface::inetAddresses);
361 * }</pre>
362 *
363 * @return a Stream of NetworkInterfaces found on this machine
364 * @exception SocketException if an I/O error occurs.
365 * @since 1.9
366 */
367 public static Stream<NetworkInterface> networkInterfaces()
368 throws SocketException {
369 NetworkInterface[] netifs = getAll();
370 assert netifs != null && netifs.length > 0;
371
372 return streamFromArray(netifs);
373 }
374
375 private static <T> Enumeration<T> enumerationFromArray(T[] a) {
376 return new Enumeration<>() {
377 int i = 0;
378
379 @Override
380 public T nextElement() {
381 if (i < a.length) {
382 return a[i++];
383 } else {
384 throw new NoSuchElementException();
385 }
|
113 * bound to this network interface
114 * @see #inetAddresses()
115 */
116 public Enumeration<InetAddress> getInetAddresses() {
117 return enumerationFromArray(getCheckedInetAddresses());
118 }
119
120 /**
121 * Get a Stream of all or a subset of the InetAddresses bound to this
122 * network interface.
123 * <p>
124 * If there is a security manager, its {@code checkConnect}
125 * method is called for each InetAddress. Only InetAddresses where
126 * the {@code checkConnect} doesn't throw a SecurityException will be
127 * returned in the Stream. However, if the caller has the
128 * {@link NetPermission}("getNetworkInformation") permission, then all
129 * InetAddresses are returned.
130 *
131 * @return a Stream object with all or a subset of the InetAddresses
132 * bound to this network interface
133 * @since 9
134 */
135 public Stream<InetAddress> inetAddresses() {
136 return streamFromArray(getCheckedInetAddresses());
137 }
138
139 private InetAddress[] getCheckedInetAddresses() {
140 InetAddress[] local_addrs = new InetAddress[addrs.length];
141 boolean trusted = true;
142
143 SecurityManager sec = System.getSecurityManager();
144 if (sec != null) {
145 try {
146 sec.checkPermission(new NetPermission("getNetworkInformation"));
147 } catch (SecurityException e) {
148 trusted = false;
149 }
150 }
151 int i = 0;
152 for (int j = 0; j < addrs.length; j++) {
153 try {
191 * Get an Enumeration with all the subinterfaces (also known as virtual
192 * interfaces) attached to this network interface.
193 * <p>
194 * For instance eth0:1 will be a subinterface to eth0.
195 *
196 * @return an Enumeration object with all of the subinterfaces
197 * of this network interface
198 * @see #subInterfaces()
199 * @since 1.6
200 */
201 public Enumeration<NetworkInterface> getSubInterfaces() {
202 return enumerationFromArray(childs);
203 }
204
205 /**
206 * Get a Stream of all subinterfaces (also known as virtual
207 * interfaces) attached to this network interface.
208 *
209 * @return a Stream object with all of the subinterfaces
210 * of this network interface
211 * @since 9
212 */
213 public Stream<NetworkInterface> subInterfaces() {
214 return streamFromArray(childs);
215 }
216
217 /**
218 * Returns the parent NetworkInterface of this interface if this is
219 * a subinterface, or {@code null} if it is a physical
220 * (non virtual) interface or has no parent.
221 *
222 * @return The {@code NetworkInterface} this interface is attached to.
223 * @since 1.6
224 */
225 public NetworkInterface getParent() {
226 return parent;
227 }
228
229 /**
230 * Returns the index of this network interface. The index is an integer greater
231 * or equal to zero, or {@code -1} for unknown. This is a system specific value
345
346 return enumerationFromArray(netifs);
347 }
348
349 /**
350 * Returns a {@code Stream} of all the interfaces on this machine. The
351 * {@code Stream} contains at least one interface, possibly representing a
352 * loopback interface that only supports communication between entities on
353 * this machine.
354 *
355 * @apiNote this method can be used in combination with
356 * {@link #inetAddresses()}} to obtain a stream of all IP addresses for
357 * this node, for example:
358 * <pre> {@code
359 * Stream<InetAddress> addrs = NetworkInterface.networkInterfaces()
360 * .flatMap(NetworkInterface::inetAddresses);
361 * }</pre>
362 *
363 * @return a Stream of NetworkInterfaces found on this machine
364 * @exception SocketException if an I/O error occurs.
365 * @since 9
366 */
367 public static Stream<NetworkInterface> networkInterfaces()
368 throws SocketException {
369 NetworkInterface[] netifs = getAll();
370 assert netifs != null && netifs.length > 0;
371
372 return streamFromArray(netifs);
373 }
374
375 private static <T> Enumeration<T> enumerationFromArray(T[] a) {
376 return new Enumeration<>() {
377 int i = 0;
378
379 @Override
380 public T nextElement() {
381 if (i < a.length) {
382 return a[i++];
383 } else {
384 throw new NoSuchElementException();
385 }
|