zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
    
    
  • 相关阅读:
    Activity
    清晰易懂TCP通信原理解析(附demo、简易TCP通信库源码、解决沾包问题等)
    Android-- FragmentStatePagerAdapter分页
    使用NServiceBus开发分布式应用
    SOA、ESB、NServiceBus、云计算 总结
    ESB简介及选型(转) http://www.cnblogs.com/skyme/archive/2012/08/06/2623414.html
    C# 版dll 程序集合并工具
    83款 网络爬虫开源软件
    13个.Net开源的网络爬虫
    IE6浏览器的bug问题及相关解决的方法
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1487625.html
Copyright © 2011-2022 走看看