Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few Stream-related issues in System.Net.Http #70731

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/libraries/Common/src/System/IO/DelegatingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public override int EndRead(IAsyncResult asyncResult)
return _innerStream.EndRead(asyncResult);
}

public override void CopyTo(Stream destination, int bufferSize)
{
_innerStream.CopyTo(destination, bufferSize);
}

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken);
}

#endregion Read

#region Write
Expand Down Expand Up @@ -175,11 +185,6 @@ public override void EndWrite(IAsyncResult asyncResult)
{
_innerStream.EndWrite(asyncResult);
}

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken);
}
#endregion Write
}
}
14 changes: 11 additions & 3 deletions src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,24 @@ public override void CopyTo(Stream destination, int bufferSize)
if (_content.Length > _position)
{
destination.Write(_content.Span.Slice(_position));
_position = _content.Length;
}
}

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
ValidateCopyToArguments(destination, bufferSize);
EnsureNotClosed();
return _content.Length > _position ?
destination.WriteAsync(_content.Slice(_position), cancellationToken).AsTask() :
Task.CompletedTask;
if (_content.Length > _position)
{
ReadOnlyMemory<byte> content = _content.Slice(_position);
_position = _content.Length;
return destination.WriteAsync(content, cancellationToken).AsTask();
}
else
{
return Task.CompletedTask;
}
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,17 @@ protected async Task ValidateMisuseExceptionsAsync(Stream stream)
Assert.Throws<IOException>(() => { stream.Seek(-1, SeekOrigin.Begin); });
Assert.Throws<IOException>(() => { stream.Seek(-stream.Position - 1, SeekOrigin.Current); });
Assert.Throws<IOException>(() => { stream.Seek(-stream.Length - 1, SeekOrigin.End); });
Assert.Throws<ArgumentException>(() => { stream.Seek(0, (SeekOrigin)(-1)); });
Assert.Throws<ArgumentException>(() => { stream.Seek(0, (SeekOrigin)3); });
Assert.Throws<ArgumentException>(() => { stream.Seek(0, ~SeekOrigin.Begin); });
Assert.Throws<ArgumentOutOfRangeException>(() => { stream.SetLength(-1); });
Assert.ThrowsAny<ArgumentException>(() => { stream.Seek(0, (SeekOrigin)(-1)); });
Assert.ThrowsAny<ArgumentException>(() => { stream.Seek(0, (SeekOrigin)3); });
Assert.ThrowsAny<ArgumentException>(() => { stream.Seek(0, ~SeekOrigin.Begin); });
if (CanSetLength)
{
Assert.Throws<ArgumentOutOfRangeException>(() => { stream.SetLength(-1); });
}
else
{
Assert.Throws<NotSupportedException>(() => { stream.SetLength(0); });
}
}
else
{
Expand Down Expand Up @@ -616,10 +623,11 @@ protected sealed unsafe class NativeMemoryManager : MemoryManager<byte>
private readonly int _length;
private IntPtr _ptr;
public int PinRefCount;
private readonly string _ctorStack = Environment.StackTrace;

public NativeMemoryManager(int length) => _ptr = Marshal.AllocHGlobal(_length = length);

~NativeMemoryManager() => Assert.False(true, $"{nameof(NativeMemoryManager)} being finalized");
~NativeMemoryManager() => Assert.False(true, $"{nameof(NativeMemoryManager)} being finalized. Created at {_ctorStack}");

public override Memory<byte> Memory => CreateMemory(_length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ public override int Read(byte[] buffer, int offset, int count)
}
}

public override int ReadByte()
{
Span<byte> buffer = stackalloc byte[1];
return Read(buffer) == 1 ? buffer[0] : -1;
}

public override int Read(Span<byte> buffer)
{
if (buffer.Length == 0)
Expand Down Expand Up @@ -632,6 +638,8 @@ public override long Seek(long offset, SeekOrigin origin)
public override long Length => _length;

public override void Flush() { }
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;

public override void SetLength(long value) { throw new NotSupportedException(); }
public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
public override void Write(ReadOnlySpan<byte> buffer) { throw new NotSupportedException(); }
Expand Down
52 changes: 18 additions & 34 deletions src/libraries/System.Net.Http/src/System/Net/Http/StreamContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,60 +138,44 @@ private void PrepareContent()

private sealed class ReadOnlyStream : DelegatingStream
{
public override bool CanWrite
public ReadOnlyStream(Stream innerStream) : base(innerStream)
{
get { return false; }
}

public override int WriteTimeout
{
get { throw new NotSupportedException(SR.net_http_content_readonly_stream); }
set { throw new NotSupportedException(SR.net_http_content_readonly_stream); }
}
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
public override bool CanWrite => false;

public ReadOnlyStream(Stream innerStream)
: base(innerStream)
{
}
public override void Flush() { }

public override void Flush()
{
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;

public override void SetLength(long value) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override Task FlushAsync(CancellationToken cancellationToken)
{
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override void SetLength(long value)
{
public override void EndWrite(IAsyncResult asyncResult) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override void Write(byte[] buffer, int offset, int count)
{
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override void Write(ReadOnlySpan<byte> buffer)
{
public override void Write(ReadOnlySpan<byte> buffer) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override void WriteByte(byte value)
{
public override void WriteByte(byte value) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override Task WriteAsync(byte[] buffer, int offset, int count, Threading.CancellationToken cancellationToken)
{
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);
}

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) =>
throw new NotSupportedException(SR.net_http_content_readonly_stream);

public override int WriteTimeout
{
get => throw new InvalidOperationException(SR.net_http_content_readonly_stream);
set => throw new InvalidOperationException(SR.net_http_content_readonly_stream);
}
}
}
Expand Down
Loading