class NetUtils {

    public static InetAddress getInetNetworkMask(int prefixMaskLength, boolean isV6) {
        if (prefixMaskLength < 0 || !isV6 && prefixMaskLength > 32 || isV6 && prefixMaskLength > 128) {
            return null;
        }
        byte[] v4Address = { 0, 0, 0, 0 };
        byte[] v6Address = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        byte[] address = isV6 ? v6Address : v4Address;
        int numBytes = prefixMaskLength / 8;
        int numBits = prefixMaskLength % 8;
        int index = 0;
        for (; index < numBytes; index++) {
            address[index] = (byte) 0xff;
        }
        if (numBits > 0) {
            int rem = 0;
            for (int j = 0; j < numBits; j++) {
                rem |= 1 << 7 - j;
            }
            address[index] = (byte) rem;
        }

        try {
            return InetAddress.getByAddress(address);
        } catch (UnknownHostException e) {
            LOG.error("", e);
        }
        return null;
    }

    private  NetUtils();

}