@Test(expected = IPOctetOutOfRangeException.class) public void shouldThrowAnOctetOutOfRangeWhenOctetOutOfRange() throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { IPv4ToolSet.ipv4BlockToRange("123.234.122.311/32"); }
public static IPv4Range ipv4BlockToRange(String blockString) throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { long hi; long lo; long ipLong; long mask; long i; int mask_bits = 0; final int loMaskLimit = 0; final int hiMaskLimit = 32; List<String> ips = new ArrayList<String>(); Pattern blockPattern = Pattern.compile("^(.*)/(.*)$"); Matcher blockMatch = blockPattern.matcher(blockString); String parseError = String.format("Could not parse IP block \"%s\"", blockString); if (!blockMatch.find()) { throw new IPStringConversionException(parseError); } String ipString = blockMatch.group(1); try { mask_bits = Integer.parseInt(blockMatch.group(2)); if (mask_bits < loMaskLimit || mask_bits > hiMaskLimit) { String format = "/%d must be in range of %d and %d"; throw new IPCidrBlockOutOfRangeException(String.format(format, mask_bits, loMaskLimit, hiMaskLimit)); } mask = mask_bits(mask_bits); ipLong = ip2long(ipString); } catch (IllegalArgumentException e) { String errorMsg = String.format("could not convert \"%s\" to an integer in block \"%s\"", blockMatch.group(2), blockString); throw new IPStringConversionException(errorMsg); } lo = ipLong & mask; hi = (ipLong | ~mask) & 0xFFFFFFFFL; return new IPv4Range(lo, hi, blockString); }
IPv4ToolSet { public static IPv4Range ipv4BlockToRange(String blockString) throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { long hi; long lo; long ipLong; long mask; long i; int mask_bits = 0; final int loMaskLimit = 0; final int hiMaskLimit = 32; List<String> ips = new ArrayList<String>(); Pattern blockPattern = Pattern.compile("^(.*)/(.*)$"); Matcher blockMatch = blockPattern.matcher(blockString); String parseError = String.format("Could not parse IP block \"%s\"", blockString); if (!blockMatch.find()) { throw new IPStringConversionException(parseError); } String ipString = blockMatch.group(1); try { mask_bits = Integer.parseInt(blockMatch.group(2)); if (mask_bits < loMaskLimit || mask_bits > hiMaskLimit) { String format = "/%d must be in range of %d and %d"; throw new IPCidrBlockOutOfRangeException(String.format(format, mask_bits, loMaskLimit, hiMaskLimit)); } mask = mask_bits(mask_bits); ipLong = ip2long(ipString); } catch (IllegalArgumentException e) { String errorMsg = String.format("could not convert \"%s\" to an integer in block \"%s\"", blockMatch.group(2), blockString); throw new IPStringConversionException(errorMsg); } lo = ipLong & mask; hi = (ipLong | ~mask) & 0xFFFFFFFFL; return new IPv4Range(lo, hi, blockString); } }
IPv4ToolSet { public static IPv4Range ipv4BlockToRange(String blockString) throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { long hi; long lo; long ipLong; long mask; long i; int mask_bits = 0; final int loMaskLimit = 0; final int hiMaskLimit = 32; List<String> ips = new ArrayList<String>(); Pattern blockPattern = Pattern.compile("^(.*)/(.*)$"); Matcher blockMatch = blockPattern.matcher(blockString); String parseError = String.format("Could not parse IP block \"%s\"", blockString); if (!blockMatch.find()) { throw new IPStringConversionException(parseError); } String ipString = blockMatch.group(1); try { mask_bits = Integer.parseInt(blockMatch.group(2)); if (mask_bits < loMaskLimit || mask_bits > hiMaskLimit) { String format = "/%d must be in range of %d and %d"; throw new IPCidrBlockOutOfRangeException(String.format(format, mask_bits, loMaskLimit, hiMaskLimit)); } mask = mask_bits(mask_bits); ipLong = ip2long(ipString); } catch (IllegalArgumentException e) { String errorMsg = String.format("could not convert \"%s\" to an integer in block \"%s\"", blockMatch.group(2), blockString); throw new IPStringConversionException(errorMsg); } lo = ipLong & mask; hi = (ipLong | ~mask) & 0xFFFFFFFFL; return new IPv4Range(lo, hi, blockString); }  }
IPv4ToolSet { public static IPv4Range ipv4BlockToRange(String blockString) throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { long hi; long lo; long ipLong; long mask; long i; int mask_bits = 0; final int loMaskLimit = 0; final int hiMaskLimit = 32; List<String> ips = new ArrayList<String>(); Pattern blockPattern = Pattern.compile("^(.*)/(.*)$"); Matcher blockMatch = blockPattern.matcher(blockString); String parseError = String.format("Could not parse IP block \"%s\"", blockString); if (!blockMatch.find()) { throw new IPStringConversionException(parseError); } String ipString = blockMatch.group(1); try { mask_bits = Integer.parseInt(blockMatch.group(2)); if (mask_bits < loMaskLimit || mask_bits > hiMaskLimit) { String format = "/%d must be in range of %d and %d"; throw new IPCidrBlockOutOfRangeException(String.format(format, mask_bits, loMaskLimit, hiMaskLimit)); } mask = mask_bits(mask_bits); ipLong = ip2long(ipString); } catch (IllegalArgumentException e) { String errorMsg = String.format("could not convert \"%s\" to an integer in block \"%s\"", blockMatch.group(2), blockString); throw new IPStringConversionException(errorMsg); } lo = ipLong & mask; hi = (ipLong | ~mask) & 0xFFFFFFFFL; return new IPv4Range(lo, hi, blockString); }  static String long2ip(long ipLong); static long mask_bits(int bits); static List<String> ipv4BlockToIpStrings(String blockString); static List<String> ipv4BlocksToIpStrings(List<String> ipBlocks); static IPv4Range ipv4BlockToRange(String blockString); static boolean isValid(String ip); static long ip2long(String ip); static boolean rejectUnwantedIps(String ip); }
IPv4ToolSet { public static IPv4Range ipv4BlockToRange(String blockString) throws IPStringConversionException, IPOctetOutOfRangeException, IPCidrBlockOutOfRangeException { long hi; long lo; long ipLong; long mask; long i; int mask_bits = 0; final int loMaskLimit = 0; final int hiMaskLimit = 32; List<String> ips = new ArrayList<String>(); Pattern blockPattern = Pattern.compile("^(.*)/(.*)$"); Matcher blockMatch = blockPattern.matcher(blockString); String parseError = String.format("Could not parse IP block \"%s\"", blockString); if (!blockMatch.find()) { throw new IPStringConversionException(parseError); } String ipString = blockMatch.group(1); try { mask_bits = Integer.parseInt(blockMatch.group(2)); if (mask_bits < loMaskLimit || mask_bits > hiMaskLimit) { String format = "/%d must be in range of %d and %d"; throw new IPCidrBlockOutOfRangeException(String.format(format, mask_bits, loMaskLimit, hiMaskLimit)); } mask = mask_bits(mask_bits); ipLong = ip2long(ipString); } catch (IllegalArgumentException e) { String errorMsg = String.format("could not convert \"%s\" to an integer in block \"%s\"", blockMatch.group(2), blockString); throw new IPStringConversionException(errorMsg); } lo = ipLong & mask; hi = (ipLong | ~mask) & 0xFFFFFFFFL; return new IPv4Range(lo, hi, blockString); }  static String long2ip(long ipLong); static long mask_bits(int bits); static List<String> ipv4BlockToIpStrings(String blockString); static List<String> ipv4BlocksToIpStrings(List<String> ipBlocks); static IPv4Range ipv4BlockToRange(String blockString); static boolean isValid(String ip); static long ip2long(String ip); static boolean rejectUnwantedIps(String ip);  }