/// <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; } }
public WriteWriteAsyncStream(IWriteAsyncStream stream, Func<Stream, Stream> writeStack) { _stream = stream; _ms = new MemoryStream(); _watcher = new WriteWatcherStream(_ms, () => { }); // We do this mostly so that the ms is not disposed _stack = writeStack(_watcher); }