v7.3.2018.0614   重构高性能资源池,减少GC压力,增加线程池,让异步任务得到平等竞争CPU的机会
大石头 编写于 2018-06-14 17:56:44
X

namespace NewLife.Net.Http
{
    sealed class ByteParser
    {
        private byte[] _bytes;
        private int _pos;

        internal ByteParser(byte[] bytes)
        {
            _bytes = bytes;
            _pos = 0;
        }

        internal ByteString ReadLine()
        {
            ByteString str = null;
            for (int i = _pos; i < _bytes.Length; i++)
            {
                if (_bytes[i] == 10)
                {
                    int length = i - _pos;
                    if (length > 0 && _bytes[i - 1] == 13) length--;
                    str = new ByteString(_bytes, _pos, length);
                    _pos = i + 1;
                    return str;
                }
            }
            if (_pos < _bytes.Length) str = new ByteString(_bytes, _pos, _bytes.Length - _pos);
            _pos = _bytes.Length;
            return str;
        }

        internal int CurrentOffset { get { return _pos; } }
    }
}