NetUtil { public static boolean isValidIpV4Address(String value) { int periods = 0; int i; int length = value.length(); if (length > 15) { return false; } char c; StringBuilder word = new StringBuilder(); for (i = 0; i < length; i ++) { c = value.charAt(i); if (c == '.') { periods ++; if (periods > 3) { return false; } if (word.length() == 0) { return false; } if (Integer.parseInt(word.toString()) > 255) { return false; } word.delete(0, word.length()); } else if (!Character.isDigit(c)) { return false; } else { if (word.length() > 2) { return false; } word.append(c); } } if (word.length() == 0 || Integer.parseInt(word.toString()) > 255) { return false; } return periods == 3; } private  NetUtil(); static boolean isIpV4StackPreferred(); static byte[] createByteArrayFromIpAddressString(String ipAddressString); static boolean isValidIpV6Address(String ipAddress); @Deprecated static boolean isValidIp4Word(String word); static boolean isValidIpV4Address(String value); static Inet6Address getByName(CharSequence ip); static Inet6Address getByName(CharSequence ip, boolean ipv4Mapped); static String toAddressString(InetAddress ip); static String toAddressString(InetAddress ip, boolean ipv4Mapped); static final Inet4Address LOCALHOST4; static final Inet6Address LOCALHOST6; static final InetAddress LOCALHOST; static final NetworkInterface LOOPBACK_IF; static final int SOMAXCONN; }
@Test public void testIsValidIpV4Address() { for (String host : validIpV4Hosts.keySet()) { assertTrue(NetUtil.isValidIpV4Address(host)); } for (String host : invalidIpV4Hosts.keySet()) { assertFalse(NetUtil.isValidIpV4Address(host)); } }