集册 Java实例教程 是网络块字符串中的有效地址

是网络块字符串中的有效地址

欢马劈雪     最近更新时间:2020-01-02 10:19:05

531
是网络块字符串中的有效地址


import java.util.ArrayList;/** 来 自 时 代 J a v a 公 众 号 - nowjava.com**/

import java.util.regex.Pattern;


public class Main{


    private static final String IPADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."

            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."

            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."

            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

    public static boolean isValidAddressInNetblockString(String address,

            String netblock) throws InvalidIPAddressException {

        String[] parts = netblock.split("/");

        if (parts.length != 2) {

            throw new InvalidIPAddressException(

                    "Incorrectly formatted IP/CIDR string.");

        }

        String baseAddress = parts[0];

        int cidr;

        try {

            cidr = Integer.parseInt(parts[1]);/*时 代 J a v a 公 众 号 提 供*/

        } catch (NumberFormatException e) {

            throw new InvalidIPAddressException(

                    "Could not parse CIDR to integer: " + parts[1]);

        }

        return isValidAddressInNetblock(address, baseAddress, cidr);

    }

    public static boolean isValidAddressInNetblock(String address,

            String baseAddress, int cidr) throws InvalidIPAddressException {

        if (!isValidAddress(address)) {

            throw new InvalidIPAddressException(

                    "Invalid format for IP address.");

        } else if (!isValidAddress(baseAddress)) {

            throw new InvalidIPAddressException(

                    "Invalid format for base address in netblock.");

        } else if (!isValidCidrblock(baseAddress, cidr)) {

            throw new InvalidIPAddressException(

                    "Invalid netblock provided.");

        }


        long firstIp = ipToNumber(baseAddress);

        long givenIp = getCiderBaseIP(ipToNumber(address), cidr);

        return (givenIp == firstIp);

    }

    public static boolean isValidAddress(String address) {

        if (address == null) {

            return false;

        }


        return Pattern.compile(IPADDRESS_PATTERN).matcher(address)

                .matches();

    }

    public static Boolean isValidCidrblock(final long ipAddress,

            final int cidr) throws InvalidIPAddressException {


        if (ipAddress == getCiderBaseIP(ipAddress, cidr)) {

            return true;

        }


        return false;

    }

    public static Boolean isValidCidrblock(final String ipAddress,

            final int cidr) throws InvalidIPAddressException {

        long address = IPAddressUtil.ipToNumber(ipAddress);

        return isValidCidrblock(address, cidr);

    }

    public static long ipToNumber(String ipAddr)

            throws InvalidIPAddressException {

        if (ipAddr == null) {

            throw new InvalidIPAddressException("NULL IP Address");

        }


        String[] addrArray = ipAddr.split("\\.");


        if (addrArray.length != 4) {

            throw new InvalidIPAddressException("Invalid IP Address length");

        }


        long num = 0;

        for (int i = 0; i < addrArray.length; i++) {

            int power = 3 - i;


            int octet = Integer.parseInt(addrArray[i]);


            if (octet < 0 || octet > 255) {

                throw new InvalidIPAddressException(

                        "Invalid octet, it must be between 0-255");

            }


            num += ((octet % 256) * Math.pow(256, power));

        }

        return num;

    }

    public static long getCiderBaseIP(final long ip, final int cidr)

            throws InvalidIPAddressException {


        long netmask = getSubnetMaskNumeric(cidr);


        // get base network ip for this ip/netmask combo

        long baseIP = ip & netmask;

        return baseIP;

    }

    public static long getSubnetMaskNumeric(final 
展开阅读全文