PipeWriterStream
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace System.IO.Pipelines
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal sealed class PipeWriterStream : Stream
{
private readonly PipeWriter _pipeWriter;
internal bool LeaveOpen { get; set; }
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length {
get {
throw new NotSupportedException();
}
}
public override long Position {
get {
throw new NotSupportedException();
}
set {
throw new NotSupportedException();
}
}
public PipeWriterStream(PipeWriter pipeWriter, bool leaveOpen)
{
_pipeWriter = pipeWriter;
LeaveOpen = leaveOpen;
}
protected override void Dispose(bool disposing)
{
if (!LeaveOpen)
_pipeWriter.Complete(null);
}
public override void Flush()
{
FlushAsync().GetAwaiter().GetResult();
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public sealed override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, [System.Runtime.CompilerServices.Nullable(2)] AsyncCallback callback, [System.Runtime.CompilerServices.Nullable(2)] object state)
{
return System.Threading.Tasks.TaskToAsyncResult.Begin(WriteAsync(buffer, offset, count, default(CancellationToken)), callback, state);
}
public sealed override void EndWrite(IAsyncResult asyncResult)
{
System.Threading.Tasks.TaskToAsyncResult.End(asyncResult);
}
public override void Write(byte[] buffer, int offset, int count)
{
WriteAsync(buffer, offset, count).GetAwaiter().GetResult();
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (buffer == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.buffer);
return GetFlushResultAsTask(_pipeWriter.WriteAsync(new ReadOnlyMemory<byte>(buffer, offset, count), cancellationToken));
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
return GetFlushResultAsTask(_pipeWriter.FlushAsync(cancellationToken));
}
private static Task GetFlushResultAsTask(ValueTask<FlushResult> valueTask)
{
if (valueTask.IsCompletedSuccessfully) {
if (valueTask.Result.IsCanceled)
ThrowHelper.ThrowOperationCanceledException_FlushCanceled();
return Task.CompletedTask;
}
return <GetFlushResultAsTask>g__AwaitTask|27_0(valueTask);
}
}
}