@Test public void testToInetAddress() throws UnknownHostException { InetAddress inetAddr = InetAddress.getByName("195.127.0.11"); Assert.assertEquals(inetAddr, ipUtils.toInetAddress(ipDec)); Assert.assertEquals(inetAddr, ipUtils.toInetAddress(ipHex)); Assert.assertEquals(inetAddr, ipUtils.toInetAddress(ipOct)); Assert.assertEquals(inetAddr, ipUtils.toInetAddress(ipBin)); Assert.assertEquals(inetAddr, ipUtils.toInetAddress(ipNum)); }
public InetAddress toInetAddress(String ip) { if (isDecimalIpAddress(ip)) { return InetAddresses.forString(ip); } Matcher matcherBin = BIN_PATTERN.matcher(ip); if (matcherBin.matches()) { String bin = matcherBin.group(); int numericIpValue = Integer.parseUnsignedInt(bin, 2); return InetAddresses.fromInteger(numericIpValue); } Matcher matcherOct = OCTAL_PATTERN.matcher(ip); if (matcherOct.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherOct.group(i), 8); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } Matcher matcherHex = HEX_PATTERN.matcher(ip); if (matcherHex.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherHex.group(i), 16); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } if (isNumericIpAddress(ip)) { return InetAddresses.fromInteger(Integer.parseUnsignedInt(ip)); } return null; }
IpUtils { public InetAddress toInetAddress(String ip) { if (isDecimalIpAddress(ip)) { return InetAddresses.forString(ip); } Matcher matcherBin = BIN_PATTERN.matcher(ip); if (matcherBin.matches()) { String bin = matcherBin.group(); int numericIpValue = Integer.parseUnsignedInt(bin, 2); return InetAddresses.fromInteger(numericIpValue); } Matcher matcherOct = OCTAL_PATTERN.matcher(ip); if (matcherOct.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherOct.group(i), 8); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } Matcher matcherHex = HEX_PATTERN.matcher(ip); if (matcherHex.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherHex.group(i), 16); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } if (isNumericIpAddress(ip)) { return InetAddresses.fromInteger(Integer.parseUnsignedInt(ip)); } return null; } }
IpUtils { public InetAddress toInetAddress(String ip) { if (isDecimalIpAddress(ip)) { return InetAddresses.forString(ip); } Matcher matcherBin = BIN_PATTERN.matcher(ip); if (matcherBin.matches()) { String bin = matcherBin.group(); int numericIpValue = Integer.parseUnsignedInt(bin, 2); return InetAddresses.fromInteger(numericIpValue); } Matcher matcherOct = OCTAL_PATTERN.matcher(ip); if (matcherOct.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherOct.group(i), 8); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } Matcher matcherHex = HEX_PATTERN.matcher(ip); if (matcherHex.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherHex.group(i), 16); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } if (isNumericIpAddress(ip)) { return InetAddresses.fromInteger(Integer.parseUnsignedInt(ip)); } return null; }  }
IpUtils { public InetAddress toInetAddress(String ip) { if (isDecimalIpAddress(ip)) { return InetAddresses.forString(ip); } Matcher matcherBin = BIN_PATTERN.matcher(ip); if (matcherBin.matches()) { String bin = matcherBin.group(); int numericIpValue = Integer.parseUnsignedInt(bin, 2); return InetAddresses.fromInteger(numericIpValue); } Matcher matcherOct = OCTAL_PATTERN.matcher(ip); if (matcherOct.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherOct.group(i), 8); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } Matcher matcherHex = HEX_PATTERN.matcher(ip); if (matcherHex.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherHex.group(i), 16); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } if (isNumericIpAddress(ip)) { return InetAddresses.fromInteger(Integer.parseUnsignedInt(ip)); } return null; }  boolean isIpAddress(String ip); boolean isDecimalIpAddress(String ip); boolean isBinaryIpAddress(String ip); boolean isOctalEncodedIpAddress(String ip); boolean isHexEncodedIpAddress(String ip); boolean isNumericIpAddress(String ip); InetAddress toInetAddress(String ip); }
IpUtils { public InetAddress toInetAddress(String ip) { if (isDecimalIpAddress(ip)) { return InetAddresses.forString(ip); } Matcher matcherBin = BIN_PATTERN.matcher(ip); if (matcherBin.matches()) { String bin = matcherBin.group(); int numericIpValue = Integer.parseUnsignedInt(bin, 2); return InetAddresses.fromInteger(numericIpValue); } Matcher matcherOct = OCTAL_PATTERN.matcher(ip); if (matcherOct.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherOct.group(i), 8); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } Matcher matcherHex = HEX_PATTERN.matcher(ip); if (matcherHex.matches()) { String delim = ""; StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 4; i++) { int dec = Integer.parseInt(matcherHex.group(i), 16); sb.append(delim).append(dec); delim = "."; } return InetAddresses.forString(sb.toString()); } if (isNumericIpAddress(ip)) { return InetAddresses.fromInteger(Integer.parseUnsignedInt(ip)); } return null; }  boolean isIpAddress(String ip); boolean isDecimalIpAddress(String ip); boolean isBinaryIpAddress(String ip); boolean isOctalEncodedIpAddress(String ip); boolean isHexEncodedIpAddress(String ip); boolean isNumericIpAddress(String ip); InetAddress toInetAddress(String ip);  }