/** * Gets next IP address. If the IP address doesn't have next IP then return <code>null</code>. * * @return Next IP address. */ public IP nextIP() { return new IP(value + 1); }
/** * 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(); }
/** * 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)); }
@Override public IP next() { if (start) { start = false; return currentIP; } else { currentIP = currentIP.nextIP(); return currentIP; } }
public static boolean isValid(String ip) { try { IP.parseFromString(ip); } catch (Exception e) { return false; } return true; }
/** * Returns <code>true</code> if the IP can be used in Internet. * * @return <code>true</code> if the IP can be used in Internet. */ public boolean isUseInInternet() { return !isLocalIP(); }
/** * 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)); }
@Override public IP next() { if (start) { start = false; return currentIP; } else { currentIP = currentIP.nextIP(); return currentIP; } }
public static boolean isValid(String ip) { try { IP.parseFromString(ip); } catch (Exception e) { return false; } return true; }
/** * Returns <code>true</code> if the IP can be used in Internet. * * @return <code>true</code> if the IP can be used in Internet. */ public boolean isUseInInternet() { return !isLocalIP(); }
/** * 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(); }
/** * Gets previous IP address. If the IP address doesn't have previous IP then return * <code>null</code>. * * @return Previous IP address. */ public IP preIP() { return new IP(value - 1); }
/** * 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(); }