public async Task StreamAsPipelineReaderReadStream() { var stream = new StreamAndPipelineReader(); var sw = new StreamWriter(stream); sw.Write("Hello"); sw.Flush(); var reader = stream.AsPipelineReader(); var result = await reader.ReadAsync(); var buffer = result.Buffer; var segment = buffer.ToArray(); var data = Encoding.UTF8.GetString(segment); Assert.Equal("Hello", data); reader.Advance(buffer.End); sw.Write("World"); sw.Flush(); stream.FinishWriting(); var readBuf = new byte[512]; int read = await stream.ReadAsync(readBuf, 0, readBuf.Length); Assert.Equal("World", Encoding.UTF8.GetString(readBuf, 0, read)); }
public async Task StreamAsPipelineReaderUsesUnderlyingPipelineReaderIfAvailable() { var stream = new StreamAndPipelineReader(); var sw = new StreamWriter(stream); sw.Write("Hello"); sw.Flush(); stream.FinishWriting(); var reader = stream.AsPipelineReader(); while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; if (buffer.IsEmpty && result.IsCompleted) { // Done break; } var segment = buffer.ToArray(); var data = Encoding.UTF8.GetString(segment); Assert.Equal("Hello", data); reader.Advance(buffer.End); } }