⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlstream.cs

📁 Freetextbox是优秀的在线编辑器
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace FreeTextBoxControls.Support.Sgml
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;

    internal class HtmlStream : TextReader
    {
        public HtmlStream(Stream stm, System.Text.Encoding defaultEncoding)
        {
            if (defaultEncoding == null)
            {
                defaultEncoding = System.Text.Encoding.UTF8;
            }
            if (!stm.CanSeek)
            {
                stm = this.CopyToMemoryStream(stm);
            }
            this.stm = stm;
            this.rawBuffer = new byte[0x4000];
            this.rawUsed = stm.Read(this.rawBuffer, 0, 4);
            this.buffer = new char[0x4000];
            this.decoder = HtmlStream.AutoDetectEncoding(this.rawBuffer, ref this.rawPos, this.rawUsed);
            int num1 = this.rawPos;
            if (this.decoder == null)
            {
                this.decoder = defaultEncoding.GetDecoder();
                this.rawUsed += stm.Read(this.rawBuffer, 4, 0x3ffc);
                this.DecodeBlock();
                System.Text.Decoder decoder1 = this.SniffEncoding();
                if (decoder1 != null)
                {
                    this.decoder = decoder1;
                }
            }
            this.stm.Seek((long) 0, SeekOrigin.Begin);
            this.pos = this.used = 0;
            if (num1 > 0)
            {
                stm.Read(this.rawBuffer, 0, num1);
            }
            this.rawPos = this.rawUsed = 0;
        }

        internal static System.Text.Decoder AutoDetectEncoding(byte[] buffer, ref int index, int length)
        {
            if (4 <= (length - index))
            {
                uint num1 = (uint) ((((buffer[index] << 0x18) | (buffer[index + 1] << 0x10)) | (buffer[index + 2] << 8)) | buffer[index + 3]);
                switch (num1)
                {
                    case 0xfefffeff:
                    case 0x3c000000:
                        index += 4;
                        return new Ucs4DecoderBigEngian();

                    case 0xfffefffe:
                    case 60:
                        index += 4;
                        return new Ucs4DecoderLittleEndian();
                }
                num1 = num1 >> 8;
                if (num1 == 0xefbbbf)
                {
                    index += 3;
                    return System.Text.Encoding.UTF8.GetDecoder();
                }
                switch ((num1 >> 8))
                {
                    case 0xfeff:
                    case 0x3c00:
                        index += 2;
                        return System.Text.Encoding.BigEndianUnicode.GetDecoder();

                    case 0xfffe:
                    case 60:
                        index += 2;
                        return new UnicodeEncoding(false, false).GetDecoder();
                }
            }
            return null;
        }

        public override void Close()
        {
            this.stm.Close();
        }

        private Stream CopyToMemoryStream(Stream s)
        {
            int num2;
            int num1 = 0x186a0;
            byte[] buffer1 = new byte[num1];
            MemoryStream stream1 = new MemoryStream();
            while ((num2 = s.Read(buffer1, 0, num1)) > 0)
            {
                stream1.Write(buffer1, 0, num2);
            }
            stream1.Seek((long) 0, SeekOrigin.Begin);
            s.Close();
            return stream1;
        }

        internal void DecodeBlock()
        {
            if (this.pos > 0)
            {
                if (this.pos < this.used)
                {
                    Array.Copy(this.buffer, this.pos, this.buffer, 0, this.used - this.pos);
                }
                this.used -= this.pos;
                this.pos = 0;
            }
            int num1 = this.decoder.GetCharCount(this.rawBuffer, this.rawPos, this.rawUsed - this.rawPos);
            int num2 = this.buffer.Length - this.used;
            if (num2 < num1)
            {
                char[] chArray1 = new char[this.buffer.Length + num1];
                Array.Copy(this.buffer, this.pos, chArray1, 0, this.used - this.pos);
                this.buffer = chArray1;
            }
            this.used = this.pos + this.decoder.GetChars(this.rawBuffer, this.rawPos, this.rawUsed - this.rawPos, this.buffer, this.pos);
            this.rawPos = this.rawUsed;
        }

        internal string ParseAttribute()
        {
            this.SkipTo('=');
            if (this.pos < this.used)
            {
                this.pos++;
                this.SkipWhitespace();
                if (this.pos < this.used)
                {
                    char ch1 = this.buffer[this.pos];
                    this.pos++;
                    int num1 = this.pos;
                    this.SkipTo(ch1);
                    if (this.pos < this.used)
                    {
                        string text1 = new string(this.buffer, num1, this.pos - num1);
                        this.pos++;
                        return text1;
                    }
                }
            }
            return null;
        }

        public override int Peek()
        {
            int num1 = this.Read();
            if (num1 != -1)
            {
                this.pos--;
            }
            return num1;
        }

        private int PeekChar()
        {
            int num1 = this.ReadChar();
            if (num1 != -1)
            {
                this.pos--;
            }
            return num1;
        }

        public override int Read()
        {
            if (this.pos == this.used)
            {
                this.rawUsed = this.stm.Read(this.rawBuffer, 0, this.rawBuffer.Length);
                this.rawPos = 0;
                if (this.rawUsed == 0)
                {
                    return -1;
                }
                this.DecodeBlock();
            }
            if (this.pos < this.used)
            {
                return this.buffer[this.pos++];
            }
            return -1;
        }

        public override int Read(char[] buffer, int start, int length)
        {
            if (this.pos == this.used)
            {
                this.rawUsed = this.stm.Read(this.rawBuffer, 0, this.rawBuffer.Length);
                this.rawPos = 0;
                if (this.rawUsed == 0)
                {
                    return -1;
                }
                this.DecodeBlock();
            }
            if (this.pos < this.used)
            {
                length = Math.Min(this.used - this.pos, length);
                Array.Copy(this.buffer, this.pos, buffer, start, length);
                this.pos += length;
                return length;
            }
            return 0;
        }

        public override int ReadBlock(char[] buffer, int index, int count)
        {
            return this.Read(buffer, index, count);
        }

        private int ReadChar()
        {
            if (this.pos < this.used)
            {
                return this.buffer[this.pos++];
            }
            return -1;
        }

        public int ReadLine(char[] buffer, int start, int length)
        {
            int num1 = 0;
            for (int num2 = this.ReadChar(); num2 != -1; num2 = this.ReadChar())
            {
                buffer[num1 + start] = (char) ((ushort) num2);
                num1++;
                if ((num1 + start) == length)
                {
                    return num1;
                }
                switch (num2)
                {
                    case 13:
                        if (this.PeekChar() == 10)
                        {
                            num2 = this.ReadChar();
                            buffer[num1 + start] = (char) ((ushort) num2);
                            num1++;
                        }
                        return num1;

                    case 10:
                        return num1;
                }
            }
            return num1;
        }

        public override string ReadToEnd()
        {
            char[] chArray1 = new char[0x186a0];
            int num1 = 0;
            StringBuilder builder1 = new StringBuilder();
            while ((num1 = this.Read(chArray1, 0, chArray1.Length)) > 0)
            {
                builder1.Append(chArray1, 0, num1);
            }
            return builder1.ToString();
        }

        internal void SkipTo(char what)
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -