<commit_before>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.
// ----------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;

namespace Microsoft.Azure.Commands.RecoveryServices
{
    /// <summary>
    /// Class to define Utility methods 
    /// </summary>
    public static class Utilities
    {
        /// <summary>
        /// Serialize the T as xml using DataContractSerializer
        /// </summary>
        /// <typeparam name="T">the type name</typeparam>
        /// <param name="value">the T object.</param>
        /// <returns>the serialized object.</returns>
        public static string Serialize<T>(T value)
        {
            if (value == null)
            {
                return null;
            }

            string serializedValue;

            using (MemoryStream memoryStream = new MemoryStream())
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(memoryStream, value);
                memoryStream.Position = 0;
                serializedValue = reader.ReadToEnd();
            }

            return serializedValue;
        }

        /// <summary>
        /// Deserialize the xml as T
        /// </summary>
        /// <typeparam name="T">the type name</typeparam>
        /// <param name="xml">the xml as string</param>
        /// <returns>the equivalent T</returns>
        public static T Deserialize<T>(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return default(T);
            }

            using (Stream stream = new MemoryStream())
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
                return (T)deserializer.ReadObject(stream);
            }
        }

        /// <summary>
        /// Method to write content to a file.
        /// </summary>
        /// <typeparam name="T">Class to be serialized</typeparam>
        /// <param name="fileContent">content to be written to the file</param>
        /// <param name="filePath">the path where the file is to be created</param>
        /// <param name="fileName">name of the file to be created</param>
        /// <returns>file path with file name as string</returns>
        public static string WriteToFile<T>(T fileContent, string filePath, string fileName)
        {
            string fullFileName = Path.Combine(filePath, fileName);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fullFileName, false))
            {
                string contentToWrite = Serialize<T>(fileContent);
                file.WriteLine(contentToWrite);
            }

            return fullFileName;
        }

        /// <summary>
        /// Imports Azure Site Recovery Vault settings.
        /// </summary>
        /// <param name="asrVaultCreds">ASR Vault credentials</param>
        public static void UpdateVaultSettings(ASRVaultCreds asrVaultCreds)
        {
            object updateVaultSettingsOneAtATime = new object();
            lock (updateVaultSettingsOneAtATime)
            {
                PSRecoveryServicesClient.asrVaultCreds.ResourceName =
                    asrVaultCreds.ResourceName;
                PSRecoveryServicesClient.asrVaultCreds.CloudServiceName =
                    asrVaultCreds.CloudServiceName;
                PSRecoveryServicesClient.asrVaultCreds.ChannelIntegrityKey =
                    asrVaultCreds.ChannelIntegrityKey;
            }
        }

        /// <summary>
        /// method to return the Downloads path for the current user.
        /// </summary>
        /// <returns>path as  string.</returns>
        public static string GetDefaultPath()
        {
            string path = null;
            path = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
            path = Path.Combine(path, "Downloads");

            return path;
        }

        /// <summary>
        /// Generate cryptographically random key of given bit size.
        /// </summary>
        /// <param name="size">size of the key to be generated</param>
        /// <returns>the key</returns>
        public static string GenerateRandomKey(int size)
        {
            byte[] key = new byte[(int)size / 8];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetBytes(key);
            return Convert.ToBase64String(key);
        }

        /// <summary>
        /// Method to generate the cloud service name <commit_msg>Fixing the default path for the file to be temporary<commit_after>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.
// ----------------------------------------------------------------------------------

using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Azure.Portal.RecoveryServices.Models.Common;

namespace Microsoft.Azure.Commands.RecoveryServices
{
    /// <summary>
    /// Class to define Utility methods 
    /// </summary>
    public static class Utilities
    {
        /// <summary>
        /// Serialize the T as xml using DataContractSerializer
        /// </summary>
        /// <typeparam name="T">the type name</typeparam>
        /// <param name="value">the T object.</param>
        /// <returns>the serialized object.</returns>
        public static string Serialize<T>(T value)
        {
            if (value == null)
            {
                return null;
            }

            string serializedValue;

            using (MemoryStream memoryStream = new MemoryStream())
            using (StreamReader reader = new StreamReader(memoryStream))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(memoryStream, value);
                memoryStream.Position = 0;
                serializedValue = reader.ReadToEnd();
            }

            return serializedValue;
        }

        /// <summary>
        /// Deserialize the xml as T
        /// </summary>
        /// <typeparam name="T">the type name</typeparam>
        /// <param name="xml">the xml as string</param>
        /// <returns>the equivalent T</returns>
        public static T Deserialize<T>(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                return default(T);
            }

            using (Stream stream = new MemoryStream())
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
                return (T)deserializer.ReadObject(stream);
            }
        }

        /// <summary>
        /// Method to write content to a file.
        /// </summary>
        /// <typeparam name="T">Class to be serialized</typeparam>
        /// <param name="fileContent">content to be written to the file</param>
        /// <param name="filePath">the path where the file is to be created</param>
        /// <param name="fileName">name of the file to be created</param>
        /// <returns>file path with file name as string</returns>
        public static string WriteToFile<T>(T fileContent, string filePath, string fileName)
        {
            string fullFileName = Path.Combine(filePath, fileName);
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@fullFileName, false))
            {
                string contentToWrite = Serialize<T>(fileContent);
                file.WriteLine(contentToWrite);
            }

            return fullFileName;
        }

        /// <summary>
        /// Imports Azure Site Recovery Vault settings.
        /// </summary>
        /// <param name="asrVaultCreds">ASR Vault credentials</param>
        public static void UpdateVaultSettings(ASRVaultCreds asrVaultCreds)
        {
            object updateVaultSettingsOneAtATime = new object();
            lock (updateVaultSettingsOneAtATime)
            {
                PSRecoveryServicesClient.asrVaultCreds.ResourceName =
                    asrVaultCreds.ResourceName;
                PSRecoveryServicesClient.asrVaultCreds.CloudServiceName =
                    asrVaultCreds.CloudServiceName;
                PSRecoveryServicesClient.asrVaultCreds.ChannelIntegrityKey =
                    asrVaultCreds.ChannelIntegrityKey;
            }
        }

        /// <summary>
        /// method to return the Downloads path for the current user.
        /// </summary>
        /// <returns>path as  string.</returns>
        public static string GetDefaultPath()
        {
            string path = Path.GetTempPath();
            return path;
        }

        /// <summary>
        /// Generate cryptographically random key of given bit size.
        /// </summary>
        /// <param name="size">size of the key to be generated</param>
        /// <returns>the key</returns>
        public static string GenerateRandomKey(int size)
        {
            byte[] key = new byte[(int)size / 8];
            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetBytes(key);
            return Convert.ToBase64String(key);
        }

        /// <summary>
        /// Method to generate the cloud service name 
{'fraction_non_alphanumeric': 0.12739933934470138, 'fraction_numerical': 0.0019641103472904206, 'mean_word_length': 1.7618343195266273, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3526689', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2647, 'n_words': 774}