IpHelper { public static long ipToLong(Inet4Address ip) { byte[] octets = ip.getAddress(); long result = 0; for (byte octet : octets) { result <<= 8; result |= octet & 0xff; } return result; }  static long ipToLong(Inet4Address ip); static InetAddress longToIp(long ip); static String longToIpString(long ip); static long ipStringToLong(String ip); static byte[] longToNetworkByteOrderArray(long addr); static String calculateCidrFromIpV4Range(long ipLow, long ipHigh); static InetAddress calculateNetmaskFromCidr(String cidr); static String calculateNetmaskStringFromCidr(String cidr); static int safeLongToInt(long l);  }
@Test(dataProvider = "IpAddresses") public void testIpToLong(String ipAddress, long expectedLongValue) { InetAddress in = InetAddresses.forString(ipAddress); assertThat(in, is(instanceOf(Inet4Address.class))); long ip = IpHelper.ipToLong((Inet4Address) in); assertThat(ip, is(expectedLongValue)); }