java.lang.Object
org.minidns.util.InetAddressUtil
Utility class for IP address manipulation and validation.
Field Summary
Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionstatic InetAddressConverts a CharSequence to an InetAddress if it represents a valid IP address.static Inet4Addressipv4From(CharSequence cs) Creates an Inet4Address from the given CharSequence.static Inet6Addressipv6From(CharSequence cs) Creates an Inet6Address from the given CharSequence.static booleanisIpAddress(CharSequence address) Checks if the given CharSequence represents a valid IP address (IPv4 or IPv6).static booleanisIpV4Address(CharSequence address) Checks if the given CharSequence represents a valid IPv4 address.static booleanisIpV6Address(CharSequence address) Checks if the given CharSequence represents a valid IPv6 address.private static byte[]Parses a dotted-quad address into bytes.private static byte[]Parses a colon-hex address into bytes.static DnsNamereverseIpAddressOf(Inet4Address inet4Address) Creates the reverse DNS name for the given IPv4 address.static DnsNamereverseIpAddressOf(Inet6Address inet6Address) Creates the reverse DNS name for the given IPv6 address.
Field Details
IPV4_PATTERN
Matches a dotted-quad address. Regex from the IPv4 validation answer by Kevin Wong on stackoverflow.com, licensed CC BY-SA 3.0.IPV6_PATTERN
Matches a colon-hex address. Regex from the IPv6 validation answer by David M. Syzdek on stackoverflow.com, licensed CC BY-SA 3.0.
Constructor Details
InetAddressUtil
public InetAddressUtil()Constructor. Each helper below parses or formats the address it is handed, so an instance carries no state.
Method Details
ipv4From
Creates an Inet4Address from the given CharSequence.- Parameters:
cs- the CharSequence containing the IPv4 address- Returns:
- an
Inet4Addressbuilt from the four octets ofcsas its raw address bytes, with no hostname set - Throws:
IllegalArgumentException- if the input is not a valid IPv4 address
parseIPv4
Parses a dotted-quad address into bytes.- Parameters:
ip- the address to parse- Returns:
- the four bytes
ipv6From
Creates an Inet6Address from the given CharSequence.- Parameters:
cs- the CharSequence containing the IPv6 address- Returns:
- an
Inet6Addressbuilt from the hexadecimal groups ofcsas raw address bytes, with no hostname set - Throws:
IllegalArgumentException- if the input is not a valid IPv6 address
parseIPv6
Parses a colon-hex address into bytes.- Parameters:
ip- the address to parse- Returns:
- the sixteen bytes
isIpV4Address
Checks if the given CharSequence represents a valid IPv4 address.- Parameters:
address- the CharSequence to validate- Returns:
- true if the input is a valid IPv4 address, false otherwise
isIpV6Address
Checks if the given CharSequence represents a valid IPv6 address.- Parameters:
address- the CharSequence to validate- Returns:
- true if the input is a valid IPv6 address, false otherwise
isIpAddress
Checks if the given CharSequence represents a valid IP address (IPv4 or IPv6).- Parameters:
address- the CharSequence to validate- Returns:
- true if the input is a valid IP address, false otherwise
convertToInetAddressIfPossible
Converts a CharSequence to an InetAddress if it represents a valid IP address.- Parameters:
address- the CharSequence to convert- Returns:
- the InetAddress if the input is a valid IP address, null otherwise
reverseIpAddressOf
Creates the reverse DNS name for the given IPv6 address.- Parameters:
inet6Address- the IPv6 address- Returns:
- the reverse DNS name
reverseIpAddressOf
Creates the reverse DNS name for the given IPv4 address.- Parameters:
inet4Address- the IPv4 address- Returns:
- the reverse DNS name