< prev index next > test/lib/jdk/test/lib/net/IPSupport.java
Print this page
/*
- * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2019, 2021, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
+ import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
private static boolean hasAddress(InetAddress address) {
try (Socket socket = new Socket()) {
socket.bind(new InetSocketAddress(address, 0));
return true;
} catch (SocketException se) {
- return false;
+ try {
+ return NetworkInterface.networkInterfaces()
+ .flatMap(NetworkInterface::inetAddresses)
+ .map(InetAddress::getClass)
+ .filter(clz -> clz.equals(address.getClass()))
+ .findAny().isPresent();
+ } catch (SocketException se2) {
+ return false;
+ }
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
< prev index next >