/** * Creates an IPv4 network mask prefix. * * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] * @return a new IPv4 address that contains a mask prefix of the * specified length * @throws IllegalArgumentException if the argument is invalid */ public static Ip4Address makeMaskPrefix(int prefixLength) { byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); return new Ip4Address(mask); }
/** * Creates an IPv6 network mask prefix. * * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 128] * @return a new IPv6 address that contains a mask prefix of the * specified length * @throws IllegalArgumentException if the arguments are invalid */ public static Ip6Address makeMaskPrefix(int prefixLength) { byte[] mask = IpAddress.makeMaskPrefixArray(VERSION, prefixLength); return new Ip6Address(mask); }
/** * Creates an IP network mask prefix. * * @param version the IP address version * @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 contains a mask prefix of the * specified length * @throws IllegalArgumentException if the arguments are invalid */ public static IpAddress makeMaskPrefix(Version version, int prefixLength) { byte[] mask = makeMaskPrefixArray(version, prefixLength); return new IpAddress(version, mask); }
/** * Creates a byte array that represents an IP address masked with * a network mask of given mask length. * * @param addr 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 byte array that represents the IP address masked with * a mask prefix of the specified length * @throws IllegalArgumentException if the prefix length is invalid */ static byte[] makeMaskedAddressArray(final IpAddress addr, int prefixLength) { byte[] mask = IpAddress.makeMaskPrefixArray(addr.version(), prefixLength); byte[] net = new byte[mask.length]; // Mask each byte for (int i = 0; i < net.length; i++) { net[i] = (byte) (addr.octets[i] & mask[i]); } return net; }