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

/// <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;
        }



版权声明:本文为okkk原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。