Exemplo n.º 1
0
            public async Task PrepareData()
            {
                if (!_isDone && _bufferNeedsData)
                {
                    _length = await _stream.ReadAsync(_buffer, 0, _buffer.Length, CancellationToken.None).ConfigureAwait(false);

                    if (_length == 0)
                    {
                        _isDone = true;
                        return;
                    }
                    _offset          = 0;
                    _bufferNeedsData = false;
                }

                if (!_isDone && _bufferNeedsData2)
                {
                    _length2 = await _stream.ReadAsync(_buffer2, 0, _buffer2.Length, CancellationToken.None).ConfigureAwait(false);

                    if (_length2 == 0)
                    {
                        _isDone = true;
                        return;
                    }
                    _offset2          = 0;
                    _bufferNeedsData2 = false;
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Straight copy from the reader to the writer
        /// </summary>
        public static async Task CopyToAsync(this IReadAsyncStream readStream, IWriteAsyncStream writeStream, CancellationToken ct)
        {
            // We have the double exception catching to avoid the using() from hiding the inner exception
            Exception inner = null;

            try
            {
                using (readStream)
                {
                    try
                    {
                        var buffer = new byte[BufferSize];
                        int read;
                        do
                        {
                            ct.ThrowIfCancellationRequested();
                            read = await readStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false);

                            if (read > 0)
                            {
                                ct.ThrowIfCancellationRequested();
                                await writeStream.WriteAsync(buffer, 0, read, ct).ConfigureAwait(false);
                            }
                        } while (read > 0);
                    }
                    catch (Exception e)
                    {
                        inner = e;
                        throw;
                    }
                }
            }
            catch (Exception)
            {
                if (inner != null)
                {
                    throw inner;
                }
                else
                {
                    throw;
                }
            }
        }