netDxf/EncodingType.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

// Code modified from Danny Chen comment in http://stackoverflow.com/questions/3404199/how-to-find-out-the-encoding-of-a-file-c-sharp

using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace netDxf
{
    internal class EncodingType
    {

        public static Encoding GetType(Stream fs)
        {
            byte[] Unicode = {0xFF, 0xFE, 0x41};
            byte[] UnicodeBIG = {0xFE, 0xFF, 0x00};
            byte[] UTF8 = {0xEF, 0xBB, 0xBF}; //with BOM
            Encoding reVal = Encoding.Default;
            
            BinaryReader r = new BinaryReader(fs, Encoding.Default);
            int i;
            int.TryParse(fs.Length.ToString(CultureInfo.InvariantCulture), out i);
            byte[] ss = r.ReadBytes(i);
            if (IsUTF8Bytes(ss) || (ss[0] == UTF8[0] && ss[1] == UTF8[1] && ss[2] == UTF8[2]))
                reVal = Encoding.UTF8;
            else if (ss[0] == UnicodeBIG[0] && ss[1] == UnicodeBIG[1] && ss[2] == UnicodeBIG[2])
                reVal = Encoding.BigEndianUnicode;
            else if (ss[0] == Unicode[0] && ss[1] == Unicode[1] && ss[2] == Unicode[2])
                reVal = Encoding.Unicode;
            fs.Position = 0;           
            return reVal;
        }

        private static bool IsUTF8Bytes(byte[] data)
        {
            int charByteCounter = 1;
            for (int i = 0; i < data.Length; i++)
            {
                byte curByte = data[i];
                if (charByteCounter == 1)
                {
                    if (curByte >= 0x80)
                    {
                        while (((curByte <<= 1) & 0x80) != 0)
                            charByteCounter++;

                        if (charByteCounter == 1 || charByteCounter > 6)
                            return false;
                    }
                }
                else
                {
                    if ((curByte & 0xC0) != 0x80)
                        return false;
                    charByteCounter--;
                }
            }
            if (charByteCounter > 1)
                throw new Exception("Error byte format.");

后面还有 4 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。