/** * Construct a new instance. * * @param addr An integer value which represents an IPv4 address. * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @throws IllegalArgumentException * The given prefix length is invalid. */ public Ip4Network(int addr, int prefix) { super(prefix); int mask = getNetMask(getPrefixLength()); address = addr & mask; netMask = mask; }
/** * Return an {@link InetAddress} instance which represents the IPv4 netmask * specified by the given prefix length. * * @param length The IPv4 prefix length. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @return An {@link InetAddress} instance. * @throws IllegalArgumentException * The given prefix length is invalid. */ public static InetAddress getInetMask(int length) { return getInetAddress(getNetMask(length)); }
/** * {@inheritDoc} */ @Override InetAddress init(InetAddress iaddr, int prefix) { int addr = NumberUtils.toInteger(iaddr.getAddress()); int mask = getNetMask(prefix); int maskedAddr = addr & mask; address = maskedAddr; netMask = mask; return getInetAddress(maskedAddr); }
/** * Create an IPv4 network address from the given IPv4 address and * prefix length. * * @param iaddr An {@link InetAddress} instance. * @param length The IPv4 prefix length. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @return An {@link InetAddress} instance which represents the IPv4 * network address specified by the given pair of IPv4 address and * prefix length. * @throws NullPointerException * {@code iaddr} is {@code null}. * @throws IllegalArgumentException * The given IPv4 address or prefix length is invalid. */ public static InetAddress getNetworkAddress(InetAddress iaddr, int length) { int addr = NumberUtils.toInteger(iaddr.getAddress()); int mask = getNetMask(length); return getInetAddress(addr & mask); }
/** * Construct a new instance. * * @param bytes A byte array which represents an IPv4 address. * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @throws NullPointerException * {@code bytes} is {@code null}. * @throws IllegalArgumentException * The given prefix length is invalid. * @throws IllegalArgumentException * The given byte address does not represent an IPv4 address. */ public Ip4Network(byte[] bytes, int prefix) { super(prefix); int addr = NumberUtils.toInteger(bytes); int plen = getPrefixLength(); int mask = getNetMask(plen); netMask = mask; if (plen == Integer.SIZE) { address = addr; byteAddress = bytes.clone(); } else { address = addr & mask; } }