Here you can find the source of isCIDR(String network)
Parameter | Description |
---|---|
network | CIDR to verify |
Parameter | Description |
---|---|
IllegalArgumentException | an exception |
public static void isCIDR(String network) throws IllegalArgumentException
//package com.java2s; /*/* w ww.j a va 2 s.co m*/ * Copyright (c) 2015-2016 VMware, Inc. All Rights Reserved. * * Licensed 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.net.InetAddress; import java.net.UnknownHostException; public class Main { /** * Verify if CIDR string is a valid CIDR address. * * @param network CIDR to verify * @throws IllegalArgumentException */ public static void isCIDR(String network) throws IllegalArgumentException { String[] hostMask = network.split("/"); if (hostMask.length != 2) { throw new IllegalArgumentException("subnetAddress is not a CIDR"); } isValidInetAddress(hostMask[0]); // Mask must be < 32 if (Integer.parseUnsignedInt(hostMask[1]) > 32) { throw new IllegalArgumentException("CIDR mask may not be larger than 32"); } } /** * Verify if IP string is an IPv4 address. * * @param IP IP to verify * @throws IllegalArgumentException */ public static void isValidInetAddress(String IP) throws IllegalArgumentException { // Opened issue #84 to track proper validation if (IP == null || IP.isEmpty()) { throw new IllegalArgumentException("IP is missing or empty"); } if (IP.contains(":")) { // implement IPv6 validation } else { String[] segments = IP.split("\\."); if (segments.length != 4) { throw new IllegalArgumentException("IP does not appear valid:" + IP); } // it appears to be literal IP, its safe to use the getByName method try { InetAddress.getByName(IP); } catch (UnknownHostException e) { throw new IllegalArgumentException(e); } } } }