LoginSignup
5
4

More than 5 years have passed since last update.

SubnetUtilsについて

Posted at

commons.netにSubnetUtilsというものがあります。

public boolean isInRange(String address) {
    return isInRange(toInteger(address));
}

こんな感じのメソッドがあり便利です。

public void test1(){
    SubnetUtils subnet = new SubnetUtils("127.0.0.0/8");
    assertTrue(subnet.getInfo().isInRange("127.0.0.1"));
}

これは通ります。
しかし、

public void test2(){
    SubnetUtils subnet = new SubnetUtils("123.123.123.123/32");
    assertTrue(subnet.getInfo().isInRange("123.123.123.123"));
}

これはこけます。

public void test3(){
    SubnetUtils subnet = new SubnetUtils("123.123.123.123/32");
    subnet.setInclusiveHostCount(true);
    assertTrue(subnet.getInfo().isInRange("123.123.123.123"));
}

1つだけ指定したいときはこうしましょう。
ちなみにipv6も対応していないようです。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4