v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 authored at 2018-06-14 17:56:44
2.68 KiB
X
namespace Microsoft.VisualStudio.TextTemplating
{
    using System;
    using System.IO;
    using System.Text;

    public static class EncodingHelper
    {
        public static Encoding GetEncoding(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            Encoding currentEncoding = Encoding.Default;
            if (!File.Exists(filePath))
            {
                return currentEncoding;
            }
            try
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    if (stream.Length > 0L)
                    {
                        using (StreamReader reader = new StreamReader(stream, true))
                        {
                            char[] chArray = new char[1];
                            reader.Read(chArray, 0, 1);
                            currentEncoding = reader.CurrentEncoding;
                            reader.BaseStream.Position = 0L;
                            if (currentEncoding == Encoding.UTF8)
                            {
                                byte[] preamble = currentEncoding.GetPreamble();
                                if (stream.Length >= preamble.Length)
                                {
                                    byte[] buffer = new byte[preamble.Length];
                                    stream.Read(buffer, 0, buffer.Length);
                                    for (int i = 0; i < buffer.Length; i++)
                                    {
                                        if (buffer[i] != preamble[i])
                                        {
                                            currentEncoding = Encoding.Default;
                                            goto Label_00EF;
                                        }
                                    }
                                }
                                else
                                {
                                    currentEncoding = Encoding.Default;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (Engine.IsCriticalException(exception))
                {
                    throw;
                }
            }
        Label_00EF:
            if (currentEncoding == null)
            {
                currentEncoding = Encoding.UTF8;
            }
            return currentEncoding;
        }
    }
}