• EncodingHelper Identify Encoding by BOM


    
    namespace Microshaoft
    {
        using System.IO;
        using System.Text;
        using System.Collections.Generic;
        public static class EncodingHelper
        {
            public static Encoding IdentifyEncoding
                                        (
                                            Stream stream
                                            , Encoding defaultEncoding
                                            , Encoding[] identifyEncodings
                                        )
            {
                byte[] data = StreamDataHelper.ReadDataToBytes(stream);
                return IdentifyEncoding
                            (
                                data
                                , defaultEncoding
                                , identifyEncodings
                            );
            }
            public static Encoding IdentifyEncoding
                                        (
                                            Stream stream
                                            , Encoding defaultEncoding
                                        )
            {
                byte[] data = StreamDataHelper.ReadDataToBytes(stream);
                return IdentifyEncoding
                            (
                                data
                                , defaultEncoding
                            );
            }
            public static Encoding IdentifyEncoding
                                        (
                                            byte[] data
                                            , Encoding defaultEncoding
                                        )
            {
                EncodingInfo[] encodingInfos = Encoding.GetEncodings();
                List<Encoding> list = new List<Encoding>();
                foreach (EncodingInfo info in encodingInfos)
                {
                    Encoding e = info.GetEncoding();
                    if (e.GetPreamble().Length > 0)
                    {
                        list.Add(e);
                        System.Console.WriteLine(e.EncodingName);
                    }
                }
                Encoding[] encodings = new Encoding[list.Count];
                list.CopyTo(encodings);
                return IdentifyEncoding
                            (
                                data
                                , defaultEncoding
                                , encodings
                            );
            }
            public static Encoding IdentifyEncoding
                                        (
                                            byte[] data
                                            , Encoding defaultEncoding
                                            , Encoding[] identifyEncodings
                                        )
            {
                Encoding encoding = defaultEncoding;
                foreach (Encoding e in identifyEncodings)
                {
                    byte[] buffer = e.GetPreamble();
                    int l = buffer.Length;
                    if (l == 0)
                    {
                        continue;
                    }
                    bool flag = false;
                    for (int i = 0; i < l; i++)
                    {
                        if (buffer[i] != data[i])
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        continue;
                    }
                    else
                    {
                        encoding = e;
                    }
                }
                return encoding;
            }
        }
    }
    namespace Microshaoft
    {
        using System.IO;
        public static class StreamDataHelper
        {
            public static byte[] ReadDataToBytes(Stream stream)
            {
                byte[] buffer = new byte[64 * 1024];
                MemoryStream ms = new MemoryStream();
                int r = 0;
                int l = 0;
                long position = -1;
                if (stream.CanSeek)
                {
                    position = stream.Position;
                    stream.Position = 0;
                }
                while (true)
                {
                    r = stream.Read(buffer, 0, buffer.Length);
                    if (r > 0)
                    {
                        l += r;
                        ms.Write(buffer, 0, r);
                    }
                    else
                    {
                        break;
                    }
                }
                byte[] bytes = new byte[l];
                ms.Position = 0;
                ms.Read(bytes, 0, (int) l);
                ms.Close();
                ms.Dispose();
                ms = null;
                if (position >= 0)
                {
                    stream.Position = position;
                }
                return bytes;
            }
        }
    }
    
    
  • 相关阅读:
    实验四 Linux系统搭建C语言编程环境
    实验三 Linux系统用户管理及VIM配置
    实验二 Linux系统简单文件操作命令
    《Linux命令行与shell脚本编程大全》勘错
    考研英语每天一段阅读理解
    仓储管理系统500bug记录一下mysql 8小时超时解决办法
    win7 64位4GB内存下 tomcat7扩大内存
    解决远程连接mysql错误1130代码的方法
    win7 64 位 tomcat 定时重启脚本
    通过Navicat for MySQL远程连接的时候报错mysql 1130
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1487625.html
Copyright © 2020-2023  润新知