All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.dcm.internal.IpParserUtil Maven / Gradle / Ivy

Go to download

🌏 a tiny 0-dependency thread-safe Java™ lib for setting/viewing dns programmatically without touching host file, make unit/integration test portable. support Java 8~21, support IPv6.

There is a newer version: 1.8.3
Show newest version
package com.alibaba.dcm.internal;

/**
 * @author Jerry Lee (oldratlee at gmail dot com)
 */
final class IpParserUtil {
    private static final String INVALID_IP_V6_ADDRESS = ": invalid IPv6 address";
    private static final String INVALID_IP_ADDRESS = ": invalid IP address";

    /**
     * source code is dug from {@link java.net.InetAddress#getAllByName(java.lang.String, java.net.InetAddress)}
     */
    static byte[] ip2ByteArray(String ip) {
        boolean ipv6Expected = false;
        if (ip.charAt(0) == '[') {
            // This is supposed to be an IPv6 literal
            if (ip.length() > 2 && ip.charAt(ip.length() - 1) == ']') {
                ip = ip.substring(1, ip.length() - 1);
                ipv6Expected = true;
            } else {
                // This was supposed to be a IPv6 address, but it's not!
                throw new IllegalArgumentException(ip + INVALID_IP_V6_ADDRESS);
            }
        }

        if (Character.digit(ip.charAt(0), 16) != -1 || (ip.charAt(0) == ':')) {
            // see if it is IPv4 address
            byte[] address = IPAddressUtil.textToNumericFormatV4(ip);
            if (address != null) return address;

            // see if it is IPv6 address
            // Check if a numeric or string zone id is present
            address = IPAddressUtil.textToNumericFormatV6(ip);
            if (address != null) return address;

            if (ipv6Expected) {
                throw new IllegalArgumentException(ip + INVALID_IP_V6_ADDRESS);
            } else {
                throw new IllegalArgumentException(ip + INVALID_IP_ADDRESS);
            }
        } else {
            throw new IllegalArgumentException(ip + INVALID_IP_ADDRESS);
        }
    }


    private IpParserUtil() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy