InetAddressParser { public boolean parse() { byte [] ipaddr = m_addrStr.getBytes(); int addrLen = ipaddr.length; if (addrLen > 18) return false; int offset = 0; int k = 0; char prevDelim = 0x1; AddressElement ae = new AddressElement(); while (offset < addrLen) { String subStr = getAddressElement(ipaddr, offset, ae); byte b = (byte) 0; try { b = Integer.decode(subStr).byteValue(); } catch (NumberFormatException nfe) { m_hostAddrOnly = false; return false; } if ((ae.m_delimiter == '.') || (ae.m_delimiter == '/')) { m_ipaddr[k++] = b; } else if ((ae.m_delimiter == 0) && (prevDelim == '/')) { m_prefixLen = b; if ((m_prefixLen > 32) || (m_prefixLen < 0)) return false; else if (m_prefixLen == 32) m_hostAddrOnly = true; else if (m_prefixLen < 32) m_networkAddrOnly = true; } else if ((ae.m_delimiter == 0) && (prevDelim == '.')) { m_ipaddr[k] = b; m_prefixLen = 8 * (k+1); if (k < 3) m_networkAddrOnly = true; else m_hostAddrOnly = true; } else if (ae.m_delimiter == 0) { m_ipaddr[k] = b; m_prefixLen = 8 * (k+1); if (k < 3) m_networkAddrOnly = true; else m_hostAddrOnly = true; } prevDelim = ae.m_delimiter; offset += ae.m_len; offset += 1; ae.m_len=0; ae.m_delimiter=0; } return true; } InetAddressParser(String ipaddress); boolean parse(); String getAddressElement(byte [] ipaddr, int offset, AddressElement ae); int getPrefixLen(); boolean isHostAddress(); boolean isNetworkAddress(); byte [] getHostAddress(); boolean matches(String provIpAddress, String provHostName); boolean matches(String address); static final byte[] intToAddressBytes(int nCompactAddress); static final String intToAddressString(int nCompactAddress); static final InetAddress intToAddress(int nCompactAddress); static final int addressToInt(byte[] addr); static final int addressToInt(InetAddress addr); static final int addressToInt(String addr);  }
@Test public void testValidIpString(){ InetAddressParser parser = new InetAddressParser("10.4.5.6"); assertTrue(parser.parse()); }