|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net
{
internal sealed class RequestStream : Stream
{
private bool _disposed;
private readonly TaskCompletionSource _completeTcs;
private readonly Stream _internalStream;
public RequestStream(Stream internalStream, TaskCompletionSource completeTcs)
{
_internalStream = internalStream;
_completeTcs = completeTcs;
}
public override bool CanRead
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override bool CanWrite
{
get
{
return true;
}
}
public override void Flush()
{
ThrowIfDisposed();
_internalStream.Flush();
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return _internalStream.FlushAsync(cancellationToken);
}
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
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 override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
ValidateBufferArguments(buffer, offset, count);
_internalStream.Write(new(buffer, offset, count));
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ThrowIfDisposed();
ValidateBufferArguments(buffer, offset, count);
return _internalStream.WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
return _internalStream.WriteAsync(buffer, cancellationToken);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
{
ThrowIfDisposed();
ValidateBufferArguments(buffer, offset, count);
return _internalStream.BeginWrite(buffer, offset, count, asyncCallback, asyncState);
}
public void Complete()
{
_completeTcs.TrySetResult();
}
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfDisposed();
_internalStream.EndWrite(asyncResult);
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
_disposed = true;
}
_internalStream.Flush();
Complete();
base.Dispose(disposing);
}
public override async ValueTask DisposeAsync()
{
if (!_disposed)
{
_disposed = true;
}
await _internalStream.FlushAsync().ConfigureAwait(false);
Complete();
await base.DisposeAsync().ConfigureAwait(false);
}
private void ThrowIfDisposed()
{
ObjectDisposedException.ThrowIf(_disposed, this);
}
}
}
|