Class IpUtils


  • public class IpUtils
    extends Object
    Various utility methods for working with (string representations of) IP-addresses.
    Author:
    Guus der Kinderen, guus@goodbytes.nl
    • Constructor Summary

      Constructors 
      Constructor Description
      IpUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String convertIpv4WildcardRangeToCidrNotation​(String value)
      Replaces a representation of an IPv4 network range that is using wildcards (eg: 192.*.*.*) with a notation that is CIDR-based (eg: 192.0.0.0/8).
      static boolean isAddressInAnyOf​(byte[] address, Set<String> addressesAndRanges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(com.github.jgonian.ipmath.Ipv4 address, Set<com.github.jgonian.ipmath.Ipv4> addresses, Set<com.github.jgonian.ipmath.Ipv4Range> ranges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(com.github.jgonian.ipmath.Ipv4 address, Set<String> addressesAndRanges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(com.github.jgonian.ipmath.Ipv6 address, Set<com.github.jgonian.ipmath.Ipv6> addresses, Set<com.github.jgonian.ipmath.Ipv6Range> ranges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(com.github.jgonian.ipmath.Ipv6 address, Set<String> addressesAndRanges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(String address, Set<String> addressesAndRanges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isAddressInAnyOf​(InetAddress address, Set<String> addressesAndRanges)
      Checks if the provided address is matched by any of the addresses or address ranges.
      static boolean isValidIpAddress​(String value)
      Checks if the provided value is a representation of an IP address.
      static boolean isValidIpAddressOrRange​(String value)
      Checks if the provided value is a representation of an IP address or an IP address range.
      static boolean isValidIpRange​(String value)
      Checks if the provided value is a representation of an IP address range.
      static boolean isValidIpv4​(String value)
      Checks if the provided value is a representation of an IPv4 address.
      static boolean isValidIpv4Range​(String value)
      Checks if the provided value is a representation of an IPv4 address range.
      static boolean isValidIpv6​(String value)
      Checks if the provided value is a representation of an IPv6 address.
      static boolean isValidIpv6Range​(String value)
      Checks if the provided value is a representation of an IPv6 address range.
      static String removeBracketsFromIpv6Address​(String address)
      When the provided input is an IPv6 literal that is enclosed in brackets (the [] style as expressed in https://tools.ietf.org/html/rfc2732 and https://tools.ietf.org/html/rfc6874), this method returns the value stripped from those brackets (the IPv6 address, instead of the literal).
    • Constructor Detail

      • IpUtils

        public IpUtils()
    • Method Detail

      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               byte[] address,
                                               @Nonnull
                                               Set<String> addressesAndRanges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addressesAndRanges - the values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               String address,
                                               @Nonnull
                                               Set<String> addressesAndRanges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addressesAndRanges - the values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               InetAddress address,
                                               @Nonnull
                                               Set<String> addressesAndRanges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addressesAndRanges - the values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               com.github.jgonian.ipmath.Ipv4 address,
                                               @Nonnull
                                               Set<String> addressesAndRanges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addressesAndRanges - the values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               com.github.jgonian.ipmath.Ipv6 address,
                                               @Nonnull
                                               Set<String> addressesAndRanges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addressesAndRanges - the values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               com.github.jgonian.ipmath.Ipv4 address,
                                               @Nonnull
                                               Set<com.github.jgonian.ipmath.Ipv4> addresses,
                                               @Nonnull
                                               Set<com.github.jgonian.ipmath.Ipv4Range> ranges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addresses - the address-values to compare the provided address with.
        ranges - the range-values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • isAddressInAnyOf

        public static boolean isAddressInAnyOf​(@Nonnull
                                               com.github.jgonian.ipmath.Ipv6 address,
                                               @Nonnull
                                               Set<com.github.jgonian.ipmath.Ipv6> addresses,
                                               @Nonnull
                                               Set<com.github.jgonian.ipmath.Ipv6Range> ranges)
        Checks if the provided address is matched by any of the addresses or address ranges.
        Parameters:
        address - The address to look up.
        addresses - the address-values to compare the provided address with.
        ranges - the range-values to compare the provided address with.
        Returns:
        true if the address is found in the addresses or ranges, otherwise false.
      • removeBracketsFromIpv6Address

        @Nonnull
        public static String removeBracketsFromIpv6Address​(@Nonnull
                                                           String address)
        When the provided input is an IPv6 literal that is enclosed in brackets (the [] style as expressed in https://tools.ietf.org/html/rfc2732 and https://tools.ietf.org/html/rfc6874), this method returns the value stripped from those brackets (the IPv6 address, instead of the literal). In all other cases, the input value is returned.
        Parameters:
        address - The value from which to strip brackets.
        Returns:
        the input value, stripped from brackets if applicable.
      • convertIpv4WildcardRangeToCidrNotation

        public static String convertIpv4WildcardRangeToCidrNotation​(@Nonnull
                                                                    String value)
        Replaces a representation of an IPv4 network range that is using wildcards (eg: 192.*.*.*) with a notation that is CIDR-based (eg: 192.0.0.0/8). When the string cannot be transformed, the original string is returned.
        Parameters:
        value - The network range to transform
        Returns:
        The transformed range.
      • isValidIpv4

        public static boolean isValidIpv4​(@Nullable
                                          String value)
        Checks if the provided value is a representation of an IPv4 address.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IPv4 address, otherwise false.
      • isValidIpv4Range

        public static boolean isValidIpv4Range​(@Nullable
                                               String value)
        Checks if the provided value is a representation of an IPv4 address range.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IPv4 address range, otherwise false.
      • isValidIpv6

        public static boolean isValidIpv6​(@Nullable
                                          String value)
        Checks if the provided value is a representation of an IPv6 address.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IPv6 address, otherwise false.
      • isValidIpv6Range

        public static boolean isValidIpv6Range​(@Nullable
                                               String value)
        Checks if the provided value is a representation of an IPv6 address range.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IPv6 address range, otherwise false.
      • isValidIpAddress

        public static boolean isValidIpAddress​(@Nullable
                                               String value)
        Checks if the provided value is a representation of an IP address.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IP address, otherwise false.
      • isValidIpRange

        public static boolean isValidIpRange​(@Nullable
                                             String value)
        Checks if the provided value is a representation of an IP address range.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IP address range, otherwise false.
      • isValidIpAddressOrRange

        public static boolean isValidIpAddressOrRange​(@Nullable
                                                      String value)
        Checks if the provided value is a representation of an IP address or an IP address range.
        Parameters:
        value - the value to check
        Returns:
        true if the provided value is an IP address or IP address range, otherwise false.