Java IP Address Validate isIPv6HexCompressedAddress(String ip)

Here you can find the source of isIPv6HexCompressedAddress(String ip)

Description

is I Pv Hex Compressed Address

License

Open Source License

Declaration

public static boolean isIPv6HexCompressedAddress(String ip) 

Method Source Code

//package com.java2s;
/*//w  w w. j a  v a2s . co m
 * Copyright 2016    https://github.com/sdcuike Inc. 
 * All rights reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.regex.Pattern;

public class Main {
    private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile(
            "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");
    private static final char COLON_CHAR = ':';
    private static final int MAX_COLON_COUNT = 7;

    public static boolean isIPv6HexCompressedAddress(String ip) {
        int count = 0;
        for (int i = 0, length = ip.length(); i < length; i++) {
            if (ip.charAt(i) == COLON_CHAR) {
                count++;
            }
        }

        return count <= MAX_COLON_COUNT && IPV6_HEX_COMPRESSED_PATTERN.matcher(ip).matches();
    }
}

Related

  1. isIPv6Address(final String input)
  2. isIPv6Address(final String input)
  3. isIPv6Address(String _ipAddress)
  4. isIPv6HexCompressedAddress(final String input)
  5. isIPv6HexCompressedAddress(final String input)
  6. isIPv6LiteralAddress(String src)
  7. isIpV6RegexValid(String strIp)
  8. isIPv6StdAddress(final String input)
  9. isIpValid(final String deviceIp)