Example usage for org.bouncycastle.util IPAddress isValidIPv4

List of usage examples for org.bouncycastle.util IPAddress isValidIPv4

Introduction

In this page you can find the example usage for org.bouncycastle.util IPAddress isValidIPv4.

Prototype

public static boolean isValidIPv4(String address) 

Source Link

Document

Validate the given IPv4 address.

Usage

From source file:com.cloud.network.bigswitch.BigSwitchBcfUtils.java

License:Apache License

public Integer getSubnetMaskLength(String maskString) {
    if (!IPAddress.isValidIPv4(maskString)) {
        return null;
    }/*from   w w  w  . jav  a2  s. c  o  m*/
    String[] octets = maskString.split("\\.");
    Integer bits = 0;
    for (String o : octets) {
        switch (o) {
        case "255":
            bits += 8;
            continue;
        case "254":
            bits += 7;
            return bits;
        case "252":
            bits += 6;
            return bits;
        case "248":
            bits += 5;
            return bits;
        case "240":
            bits += 4;
            return bits;
        case "224":
            bits += 3;
            return bits;
        case "192":
            bits += 2;
            return bits;
        case "128":
            bits += 1;
            return bits;
        case "0":
            return bits;
        default:
            throw new NumberFormatException("non-contiguous subnet mask not supported");
        }
    }
    return bits;
}