IPTools { public static boolean isIpAddressLocalhost(InetAddress addr) { if (addr == null) return false; if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true; try { return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false; } } private  IPTools(); static boolean isIPv4Address(final String address); static boolean isIPv6StdAddress(final String address); static boolean isIPv6HexCompressedAddress(final String address); static boolean isIPv6Address(final String address); static InetAddress getLocalIPv4Address(); static ArrayList<InetAddress> getLocalIPv4Addresses(); static boolean isIpAddressLocalhost(InetAddress addr); static boolean isIpAddressLocalNetwork(InetAddress addr);  }
@Test public void testLocalAddresses() throws UnknownHostException { assertTrue(IPTools.isIpAddressLocalhost(InetAddress.getByName("127.0.0.1"))); assertFalse(IPTools.isIpAddressLocalhost(InetAddress.getByName("8.8.8.8"))); }