/** * Creates an IP address by masking it with a network mask of given * mask length. * * @param address the address to mask * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] for IPv4, or [0, 128] for IPv6 * @return a new IP address that is masked with a mask prefix of the * specified length * @throws IllegalArgumentException if the prefix length is invalid */ public static IpAddress makeMaskedAddress(final IpAddress address, int prefixLength) { if (address instanceof Ip4Address) { Ip4Address ip4a = (Ip4Address) address; return Ip4Address.makeMaskedAddress(ip4a, prefixLength); } else if (address instanceof Ip6Address) { Ip6Address ip6a = (Ip6Address) address; return Ip6Address.makeMaskedAddress(ip6a, prefixLength); } else { byte[] net = makeMaskedAddressArray(address, prefixLength); return IpAddress.valueOf(address.version(), net); } }