/** * Constructs IP by four numbers. * * @param a First number of the IP address. * @param b Second number of the IP address. * @param c Third number of the IP address. * @param d Last number of the IP address. */ public IP(int a, int b, int c, int d) { checkArgument(checkRange(a) && checkRange(b) && checkRange(c) && checkRange(d), "Each number of IP must in 0 ~ 255"); this.a = a; this.b = b; this.c = c; this.d = d; value = this.toLong(); }
/** * Constructs IP by four numbers. * * @param a First number of the IP address. * @param b Second number of the IP address. * @param c Third number of the IP address. * @param d Last number of the IP address. */ public IP(int a, int b, int c, int d) { checkArgument(checkRange(a) && checkRange(b) && checkRange(c) && checkRange(d), "Each number of IP must in 0 ~ 255"); this.a = a; this.b = b; this.c = c; this.d = d; value = this.toLong(); }
/** * Constructs IP by bytes. * * @param address Bytes of address. */ public IP(byte[] address) { a = UnsignedByte.toInt(address[0]); b = UnsignedByte.toInt(address[1]); c = UnsignedByte.toInt(address[2]); d = UnsignedByte.toInt(address[3]); value = this.toLong(); }
/** * Constructs IP by bytes. * * @param address Bytes of address. */ public IP(byte[] address) { a = UnsignedByte.toInt(address[0]); b = UnsignedByte.toInt(address[1]); c = UnsignedByte.toInt(address[2]); d = UnsignedByte.toInt(address[3]); value = this.toLong(); }
/** * Creates a {@link IPRange} instance by IP with mask. * * @param ipWithMask IP/mask, such as 192.168.70.1/24 * @return {@link IPRange} instance */ public static IPRange parseFromIPWithMask(String ipWithMask) { long minIpAsLong = 0; long maxIpAsLong = 0; String[] strs = ipWithMask.split("/"); if (strs.length == 2) { IP ip = IP.parseFromString(strs[0]); int mask = Integer.parseInt(strs[1]); long maskAsLong = 0xffffffff << (32 - mask); minIpAsLong = ip.toLong(); maxIpAsLong = minIpAsLong | (~maskAsLong); } else { throw new IllegalArgumentException( "The input String format error. For example" + " 192.168.1.1/24"); } return new IPRange(new IP(minIpAsLong), new IP(maxIpAsLong)); }
/** * Creates a {@link IPRange} instance by IP with mask. * * @param ipWithMask IP/mask, such as 192.168.70.1/24 * @return {@link IPRange} instance */ public static IPRange parseFromIPWithMask(String ipWithMask) { long minIpAsLong = 0; long maxIpAsLong = 0; String[] strs = ipWithMask.split("/"); if (strs.length == 2) { IP ip = IP.parseFromString(strs[0]); int mask = Integer.parseInt(strs[1]); long maskAsLong = 0xffffffff << (32 - mask); minIpAsLong = ip.toLong(); maxIpAsLong = minIpAsLong | (~maskAsLong); } else { throw new IllegalArgumentException( "The input String format error. For example" + " 192.168.1.1/24"); } return new IPRange(new IP(minIpAsLong), new IP(maxIpAsLong)); }