Ejemplo n.º 1
0
 /**
  * Creates a <code>IpRange</code> instance by a string.
  *
  * @param range a string such as "1.1.1.1-1.1.2.255".
  * @return IP range.
  */
 public static IPRange parseFromString(String range) {
   String[] ips = range.split("-");
   Preconditions.checkArgument(
       ips.length == 2,
       "IP range string must be fomarted as [minIP-maxIP],error argument:" + range);
   return new IPRange(IP.parseFromString(ips[0]), IP.parseFromString(ips[1]));
 }
Ejemplo n.º 2
0
  /**
   * 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));
  }