我需要在Java中使用CIDR符号的能力。
我找到了公共图书馆SubnetUtils。
当我尝试使用CIDR为10.10.0.0/22时,作为一个例子,我得到以下信息
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Could not parse [10.10.0.0] at org.apache.commons.net.util.SubnetUtils.calculate(SubnetUtils.java:240) at org.apache.commons.net.util.SubnetUtils.<init>(SubnetUtils.java:52)
调用的代码是:
SubnetUtils subnetUtils = new SubnetUtils(cidrNotation); SubnetInfo info = subnetUtils.getInfo();
我看到SubnetUtils正在检查比特,以确定掩码是否有效。
`来自SubnetUtils.java
private void calculate(String mask) {
Matcher matcher = cidrPattern.matcher(mask);
if (matcher.matches()) {
address = matchAddress(matcher);
/* Create a binary netmask from the number of bits specification /x */
int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, NBITS);
for (int j = 0; j < cidrPart; ++j) {
netmask |= (1 << 31-j);
}
/* Calculate base network address */
network = (address & netmask);
/* Calculate broadcast address */
broadcast = network | ~(netmask);
} else {
throw new IllegalArgumentException("Could not parse [" + mask + "]");
}`
我知道10.0.0.0/8是A类,但我应该可以合法地把它切成小块。
有办法绕道吗?更好的效用?还是我被困在重写SubnetUtils上了?
发布于 2014-07-24 03:49:11
我继续调查,结果发现在递归过程中,我丢失了CIDR上的面具。第一批传球是正常的,然后在异常路径中丢失。该实用程序按预期工作。
https://stackoverflow.com/questions/24923960
复制相似问题