@Test public void testGetPrefixLength() { List<Integer> prefixLength = Arrays.asList(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); for (int i = 0; i < NetworkUtil.validNetMasks.size(); i++) { assertEquals(Integer.valueOf(NetworkUtil.getPrefixLength(NetworkUtil.validNetMasks.get(i))), prefixLength.get(i)); } List<String> invalidNetMasks = Arrays.asList("192.168.0.1", "foo", "127.0.0.1"); for (String invalidNetMask : invalidNetMasks) { try { NetworkUtil.getPrefixLength(invalidNetMask); fail("Expected to throw IllegalArgument exception"); } catch (IllegalArgumentException iae) { } } }
public static int getPrefixLength(String netmask) { if (!validNetMasks.contains(netmask)) { throw new IllegalArgumentException(netmask + " is not a valid netmask."); } String[] octets = netmask.split("\\."); int octet1 = Integer.parseInt(octets[0]); int octet2 = Integer.parseInt(octets[1]); int octet3 = Integer.parseInt(octets[2]); int octet4 = Integer.parseInt(octets[3]); int mask = Integer.bitCount(octet1) + Integer.bitCount(octet2) + Integer.bitCount(octet3) + Integer.bitCount(octet4); return mask; }
NetworkUtil { public static int getPrefixLength(String netmask) { if (!validNetMasks.contains(netmask)) { throw new IllegalArgumentException(netmask + " is not a valid netmask."); } String[] octets = netmask.split("\\."); int octet1 = Integer.parseInt(octets[0]); int octet2 = Integer.parseInt(octets[1]); int octet3 = Integer.parseInt(octets[2]); int octet4 = Integer.parseInt(octets[3]); int mask = Integer.bitCount(octet1) + Integer.bitCount(octet2) + Integer.bitCount(octet3) + Integer.bitCount(octet4); return mask; } }
NetworkUtil { public static int getPrefixLength(String netmask) { if (!validNetMasks.contains(netmask)) { throw new IllegalArgumentException(netmask + " is not a valid netmask."); } String[] octets = netmask.split("\\."); int octet1 = Integer.parseInt(octets[0]); int octet2 = Integer.parseInt(octets[1]); int octet3 = Integer.parseInt(octets[2]); int octet4 = Integer.parseInt(octets[3]); int mask = Integer.bitCount(octet1) + Integer.bitCount(octet2) + Integer.bitCount(octet3) + Integer.bitCount(octet4); return mask; }  }
NetworkUtil { public static int getPrefixLength(String netmask) { if (!validNetMasks.contains(netmask)) { throw new IllegalArgumentException(netmask + " is not a valid netmask."); } String[] octets = netmask.split("\\."); int octet1 = Integer.parseInt(octets[0]); int octet2 = Integer.parseInt(octets[1]); int octet3 = Integer.parseInt(octets[2]); int octet4 = Integer.parseInt(octets[3]); int mask = Integer.bitCount(octet1) + Integer.bitCount(octet2) + Integer.bitCount(octet3) + Integer.bitCount(octet4); return mask; }  static String getHostIpAddress(); static int getPrefixLength(String netmask); static String resolveIpToName(String ipAddress); }
NetworkUtil { public static int getPrefixLength(String netmask) { if (!validNetMasks.contains(netmask)) { throw new IllegalArgumentException(netmask + " is not a valid netmask."); } String[] octets = netmask.split("\\."); int octet1 = Integer.parseInt(octets[0]); int octet2 = Integer.parseInt(octets[1]); int octet3 = Integer.parseInt(octets[2]); int octet4 = Integer.parseInt(octets[3]); int mask = Integer.bitCount(octet1) + Integer.bitCount(octet2) + Integer.bitCount(octet3) + Integer.bitCount(octet4); return mask; }  static String getHostIpAddress(); static int getPrefixLength(String netmask); static String resolveIpToName(String ipAddress);  }