Java IP Address Mash checkIpMask(String ip)

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

Description

check Ip Mask

License

Open Source License

Declaration

public static String checkIpMask(String ip) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String checkIpMask(String ip) {
        String ipParts[] = ip.split("\\.");
        if (ipParts.length != 4)
            return "IP address must have 4 parts";
        String message = checkIpMaskPart(ipParts[0]);
        if (message != null)
            return message;
        message = checkIpMaskPart(ipParts[1]);
        if (message != null)
            return message;
        message = checkIpMaskPart(ipParts[2]);
        if (message != null)
            return message;
        message = checkIpMaskPart(ipParts[3]);
        if (message != null)
            return message;
        else//from  w w w  .  j ava 2s. c  om
            return null;
    }

    private static String checkIpMaskPart(String part) {
        int dash;
        if ("*".equals(part))
            return null;
        dash = part.indexOf('-');
        if (dash == -1) {
            int value = Integer.parseInt(part);
            if (value < 0 || value > 255)
                return (new StringBuilder()).append("Value out of range in '").append(part).append("'").toString();
            //break MISSING_BLOCK_LABEL_219;
        }
        int from;
        from = Integer.parseInt(part.substring(0, dash));
        if (from < 0 || from > 255)
            return (new StringBuilder()).append("'From' value out of range in '").append(part).append("'")
                    .toString();
        int to;
        try {
            to = Integer.parseInt(part.substring(dash + 1));
            if (to < 0 || to > 255)
                return (new StringBuilder()).append("'To' value out of range in '").append(part).append("'")
                        .toString();
        } catch (NumberFormatException e) {
            return (new StringBuilder()).append("Integer parsing error in '").append(part).append("'").toString();
        }
        if (from > to)
            return (new StringBuilder()).append("'From' value is greater than 'To' value in '").append(part)
                    .append("'").toString();
        return null;
    }
}

Related

  1. checkIpMaskPart(String part)
  2. checkIpMaskPart(String part)