public static async Task CopyToAsync(this Stream stream, IWritableChannel channel) { while (true) { var end = channel.BeginWrite(); var block = end.Block; try { int bytesRead = await stream.ReadAsync(block.Array, block.End, block.Data.Offset + block.Data.Count - block.End); if (bytesRead == 0) { channel.CompleteWriting(); break; } else { end.UpdateEnd(bytesRead); await channel.EndWriteAsync(end); } } catch (Exception error) { channel.CompleteWriting(error); break; } } }
public static Task WriteAsync(this IWritableChannel channel, byte[] buffer, int offset, int length) { var end = channel.BeginWrite(); end.CopyFrom(buffer, offset, length); return(channel.EndWriteAsync(end)); }