java.lang.Object
org.minidns.util.InetAddressUtil

public class InetAddressUtilextends Object
Utility class for IP address manipulation and validation.
  • Field Details

    • IPV4_PATTERN

      private static final Pattern 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

      private static final Pattern 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

      public static Inet4Address ipv4From(CharSequence cs)
      Creates an Inet4Address from the given CharSequence.
      Parameters:
      cs - the CharSequence containing the IPv4 address
      Returns:
      an Inet4Address built from the four octets of cs as its raw address bytes, with no hostname set
      Throws:
      IllegalArgumentException - if the input is not a valid IPv4 address
    • parseIPv4

      private static byte[] parseIPv4(String ip)
      Parses a dotted-quad address into bytes.
      Parameters:
      ip - the address to parse
      Returns:
      the four bytes
    • ipv6From

      public static Inet6Address ipv6From(CharSequence cs)
      Creates an Inet6Address from the given CharSequence.
      Parameters:
      cs - the CharSequence containing the IPv6 address
      Returns:
      an Inet6Address built from the hexadecimal groups of cs as raw address bytes, with no hostname set
      Throws:
      IllegalArgumentException - if the input is not a valid IPv6 address
    • parseIPv6

      private static byte[] parseIPv6(String ip)
      Parses a colon-hex address into bytes.
      Parameters:
      ip - the address to parse
      Returns:
      the sixteen bytes
    • isIpV4Address

      public static boolean isIpV4Address(CharSequence address)
      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

      public static boolean isIpV6Address(CharSequence address)
      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

      public static boolean isIpAddress(CharSequence address)
      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

      public static InetAddress convertToInetAddressIfPossible(CharSequence address)
      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

      public static DnsName reverseIpAddressOf(Inet6Address inet6Address)
      Creates the reverse DNS name for the given IPv6 address.
      Parameters:
      inet6Address - the IPv6 address
      Returns:
      the reverse DNS name
    • reverseIpAddressOf

      public static DnsName reverseIpAddressOf(Inet4Address inet4Address)
      Creates the reverse DNS name for the given IPv4 address.
      Parameters:
      inet4Address - the IPv4 address
      Returns:
      the reverse DNS name