public FactorizedIPAddressPermission(String mask) {
Matcher matcher = MASK_VALIDATOR.matcher(mask);
if (false == matcher.matches()) {
throw new IllegalArgumentException("Mask " + mask + " does not match pattern " + MASK_VALIDATOR.pattern());
}
// group 1 is the factorized IP part.
// e.g. group 1 in "1.2.3.{4,5,6}" is "1.2.3."
String prefix = matcher.group(1);
StringTokenizer tokenizer = new StringTokenizer(prefix, ".");
prefixBytes = new byte[tokenizer.countTokens()];
for (int i = 0; i < prefixBytes.length; i++) {
String token = tokenizer.nextToken();
int value = Integer.parseInt(token);
if (value < 0 || 255 < value) {
throw new IllegalArgumentException("byte #" + i + " is not valid.");
}
prefixBytes[i] = (byte) value;
}
// group 5 is a comma separated list of optional suffixes.
// e.g. group 5 in "1.2.3.{4,5,6}" is ",5,6"
String suffix = matcher.group(5);
tokenizer = new StringTokenizer(suffix, ",");
suffixBytes = new byte[1 + tokenizer.countTokens()];
// group 4 is the compulsory and first suffix.
// e.g. group 4 in "1.2.3.{4,5,6}" is "4"
int value = Integer.parseInt(matcher.group(4));
int i = 0;
if (value < 0 || 255 < value) {
throw new IllegalArgumentException("suffix " + i + " is not valid.");
}
suffixBytes[i++] = (byte) value;
for (; i < suffixBytes.length; i++) {
String token = tokenizer.nextToken();
value = Integer.parseInt(token);
if (value < 0 || 255 < value) {
throw new IllegalArgumentException("byte #" + i + " is not valid.");
}
suffixBytes[i] = (byte) value;
}
}
|