.net 在byte[]数组中进行Readline

来源:互联网 发布:微信生成器软件 编辑:程序博客网 时间:2024/06/12 11:08
/// <summary>
        /// 返回说明:
        ///  string 发现一个\r\n
        ///  ""   有连续的\r\n
        ///  null  没有结束的\r\n,但缓冲区可能还有数据。
        /// </summary>
        public string ReadLine(System.Text.Encoding encode,bool rnFoure)
        {
            if (rnFoure)
            {//4字节 回车换行
                int toFind = 655373;
                fixed (byte* p = this.Buffer)
                {
                    byte* begin = p + this.m_Read;
                    int nowEnd = 0;
                    while (true)
                    {
                        if (nowEnd + m_Read + 4 >= m_Write)
                            return null;


                        if (toFind == (*((int*)(begin + nowEnd))))
                        {
                            int readBegin =  m_Read;
                            m_Read = m_Read + nowEnd + 4;
                            return encode.GetString(Buffer, readBegin, nowEnd);
                        }
                        nowEnd++;
                    }
                }
            }
            else
            {//2字节 回车换行
                short search = 2573;
                fixed (byte* p = this.Buffer)
                {
                    byte* begin = p + this.m_Read;
                    int nowEnd = 0;
                    while (true)
                    {
                        if (nowEnd + m_Read + 2 >= m_Write)
                            return null;


                        if (search == (*((short*)(begin + nowEnd))))
                        {
                            int readBegin = m_Read;
                            m_Read = m_Read + nowEnd + 2;
                            return encode.GetString(Buffer, readBegin, nowEnd);
                        }
                        nowEnd++;
                    }
                }
            }

        }


 //第二次参数的获取: 

 public static bool IsRNFoure(System.Text.Encoding encode)
        {
            return encode.GetBytes("\r\n").Length == 4;
        }


0 0
原创粉丝点击