コード例 #1
0
 private async Task EnsuranceBuffer()
 {
     if (_buffer.Count == 0)
     {
         _buffer = await _allocator.AllocateAsync(_bufferSize).WithoutCapturingContext();
     }
 }
コード例 #2
0
        public override async Task <int> ReadAsync(byte[] array, int offset, int count, CancellationToken cancellationToken)
        {
            int n = _readLen - _readPos;

            if (n == 0)
            {
                if (_writePos > 0)
                {
                    await FlushWriteAsync();
                }
                if (count > _bufferSize)
                {
                    n = _s.Read(array, offset, count);
                    // Throw away read buffer.
                    _readPos = 0;
                    _readLen = 0;
                    return(n);
                }
                if (_buffer.Count == 0)
                {
                    _buffer = await _allocator.AllocateAsync(_bufferSize);
                }
                n = await _s.ReadAsync(_buffer.Array, _buffer.Offset, _buffer.Count);

                if (n == 0)
                {
                    return(0);
                }
                _readPos = 0;
                _readLen = n;
            }
            if (n > count)
            {
                n = count;
            }
            Buffer.BlockCopy(_buffer.Array, _buffer.Offset + _readPos, array, offset, n);
            _readPos += n;

            return(n);
        }