I think there's a little discrepancy between the javadoc and the actual implementation of NetworkInterface getByInetAddress()
. The javadoc seems to suggest that getByInetAddress would return null if no match was found, yet the implementation either returns a match, either throws a SocketException.
JavaDoc
public static NetworkInterface getByInetAddress(InetAddress addr)
throws SocketException
Returns: A NetworkInterface or null if there is no network interface with the specified IP address.
Implementation
public static NetworkInterface getByInetAddress (InetAddress addr)
throws SocketException
{
if (networkInterfaces == null)
networkInterfaces = getRealNetworkInterfaces ();
for (Enumeration interfaces = networkInterfaces.elements ();
interfaces.hasMoreElements (); )
{
NetworkInterface tmp = (NetworkInterface) interfaces.nextElement ();
for (Enumeration addresses = tmp.inetAddresses.elements ();
addresses.hasMoreElements (); )
{
if (addr.equals ((InetAddress) addresses.nextElement ()))
return tmp;
}
}
throw new SocketException (
"no network interface is bound to such an IP address");
}
I suggest to either catch the exception and treat it as an answer from a 3rd party, either re-implement it using the getNetworkInterfaces()
method.