Common class for judge IPV6 or IPV4

简介: import java.util.regex.Pattern; import org.apache.http.annotation.Immutable; /** * A collection of utilities relating to InetAddresses.
import java.util.regex.Pattern;

import org.apache.http.annotation.Immutable;

/**
 * A collection of utilities relating to InetAddresses.
 *
 * @since 4.0
 */
@Immutable
public class InetAddressUtils {

    private InetAddressUtils() {
    }

    private static final String IPV4_BASIC_PATTERN_STRING =
            "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" + // initial 3 fields, 0-255 followed by .
             "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; // final field, 0-255

    private static final Pattern IPV4_PATTERN =
        Pattern.compile("^" + IPV4_BASIC_PATTERN_STRING + "$");

    private static final Pattern IPV4_MAPPED_IPV6_PATTERN = // TODO does not allow for redundant leading zeros
            Pattern.compile("^::[fF]{4}:" + IPV4_BASIC_PATTERN_STRING + "$");

    private static final Pattern IPV6_STD_PATTERN =
        Pattern.compile(
                "^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$");

    private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
        Pattern.compile(
                "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" + // 0-6 hex fields
                 "::" +
                 "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0-6 hex fields

    /*
     *  The above pattern is not totally rigorous as it allows for more than 7 hex fields in total
     */
    private static final char COLON_CHAR = ':';

    // Must not have more than 7 colons (i.e. 8 fields)
    private static final int MAX_COLON_COUNT = 7;

    /**
     * Checks whether the parameter is a valid IPv4 address
     *
     * @param input the address string to check for validity
     * @return true if the input parameter is a valid IPv4 address
     */
    public static boolean isIPv4Address(final String input) {
        return IPV4_PATTERN.matcher(input).matches();
    }

    public static boolean isIPv4MappedIPv64Address(final String input) {
        return IPV4_MAPPED_IPV6_PATTERN.matcher(input).matches();
    }

    /**
     * Checks whether the parameter is a valid standard (non-compressed) IPv6 address
     *
     * @param input the address string to check for validity
     * @return true if the input parameter is a valid standard (non-compressed) IPv6 address
     */
    public static boolean isIPv6StdAddress(final String input) {
        return IPV6_STD_PATTERN.matcher(input).matches();
    }

    /**
     * Checks whether the parameter is a valid compressed IPv6 address
     *
     * @param input the address string to check for validity
     * @return true if the input parameter is a valid compressed IPv6 address
     */
    public static boolean isIPv6HexCompressedAddress(final String input) {
        int colonCount = 0;
        for(int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == COLON_CHAR) {
                colonCount++;
            }
        }
        return  colonCount <= MAX_COLON_COUNT && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
    }

    /**
     * Checks whether the parameter is a valid IPv6 address (including compressed).
     *
     * @param input the address string to check for validity
     * @return true if the input parameter is a valid standard or compressed IPv6 address
     */
    public static boolean isIPv6Address(final String input) {
        return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);
    }

}
相关文章
Net: Board Net Initialization Failed No ethernet found.
Net: Board Net Initialization Failed No ethernet found.
317 0
|
网络协议 C# 数据库
Convert IPv6 Address to IP numbers (C#)
URL: http://lite.ip2location.com/ Use the code below to convert the IP address of your web visitors and lookup for their geographical location, e.
1466 0
|
C#
一起谈.NET技术,C#中int和System.Int32理解总结
最近园里的TeamOne写了一篇《[C#] int与System.Int32有什么区别》,发现里面有不少精彩的评论,所以忍不住想这篇文章总结一下: 本文的主要参考资料:   1.《理解C#中的System.Int32和int:并非鸡和鸡蛋》@Author:Dixin   2.《[C#] int与System.Int32有什么区别》@Author:TeamOne   一.问题的来源   MSDN说,int只不过是System.Int32的别名而已,也就是说: int i=1;System.Int32 i=1;   应该是等价的,或者说毫无区别的。
1204 0
一起谈.NET技术,用lambda去除Magic-String
string是我们的朋友。我们离不开string,但是有时候string也挺烦人的。 比如说,下面的代码,根据方法名来获取MethodInfo: var info = typeof (DateTime).GetMethod("ToShortDateSting");Console.WriteLine(info.Name); 进行“Rename Method”重构时,重构工具是不会去对string进行重命名的。
761 0
艾伟:.NET中Flags枚举的使用
  .NET中的枚举我们一般有两种用法,一是表示唯一的元素序列,例如一周里的各天;还有就是用来表示多种复合的状态。这个时候一般需要为枚举加上[Flags]特性标记为位域,例如: [Flags] enum Styles{ ShowBorder = 1, //是否显示边框ShowCa...
798 0
.Net中stirng转System.Type的一种实现思路
今天在上班的过程中,许长时间未联系的大学小伙伴发来消息,带着一个疑问来找我。     他的需求是type动态添加,这对我来说当然很easy,用泛型就好了, 随后,手起刀落,Demo就写出来,如下: 写了一个方法,传入T进行了where T:class约束,,如此easy,小伙伴怎么不会呢?然而事情并非如此简单。
1010 0