/** * Converts a byte array into an IPv4 address. * * @param value the IPv4 address value stored in network byte order * (i.e., the most significant byte first) * @return an IPv4 address * @throws IllegalArgumentException if the argument is invalid */ public static Ip4Address valueOf(byte[] value) { return new Ip4Address(value); }
/** * Converts an integer into an IPv4 address. * * @param value an integer representing an IPv4 address value * @return an IPv4 address */ public static Ip4Address valueOf(int value) { byte[] bytes = ByteBuffer.allocate(INET_BYTE_LENGTH).putInt(value).array(); return new Ip4Address(bytes); }
/** * 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); }
/** * Converts an InetAddress into an IPv4 address. * * @param inetAddress the InetAddress value to use. It must contain an IPv4 * address * @return an IPv4 address * @throws IllegalArgumentException if the argument is invalid */ public static Ip4Address valueOf(InetAddress inetAddress) { byte[] bytes = inetAddress.getAddress(); if (inetAddress instanceof Inet4Address) { return new Ip4Address(bytes); } if ((inetAddress instanceof Inet6Address) || (bytes.length == INET6_BYTE_LENGTH)) { final String msg = "Invalid IPv4 version address string: " + inetAddress.toString(); throw new IllegalArgumentException(msg); } // Use the number of bytes as a hint if (bytes.length == INET_BYTE_LENGTH) { return new Ip4Address(bytes); } final String msg = "Unrecognized IP version address string: " + inetAddress.toString(); throw new IllegalArgumentException(msg); }