public NetmaskIPAddressPermission(String mask) {
Matcher matcher = MASK_VALIDATOR.matcher(mask);
if (false == matcher.matches()) {
throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern());
}
networkAddressBytes = new byte[4];
for (int i = 0; i < 4; i++) {
String group = matcher.group(i + 1);
int value = Integer.parseInt(group);
if (value < 0 || 255 < value) {
throw new IllegalArgumentException("byte #" + i + " is not valid.");
}
networkAddressBytes[i] = (byte) value;
}
netmaskBytes = new byte[4];
String netmask = matcher.group(6);
if (null != netmask) {
int value = Integer.parseInt(netmask);
int pos = value / 8;
int shift = 8 - value % 8;
for (int i = 0; i < pos; i++) {
netmaskBytes[i] = (byte) 0xff;
}
netmaskBytes[pos] = (byte) (0xff < < shift);
} else {
for (int i = 0; i < 4; i++) {
String group = matcher.group(i + 7);
int value = Integer.parseInt(group);
if (value < 0 || 255 < value) {
throw new IllegalArgumentException("byte #" + i + " is not valid.");
}
netmaskBytes[i] = (byte) value;
}
}
}
|