Skip to content

Commit 876e36d

Browse files
Make Stream.Flush no-op on read-only streams (#57079)
* Make Stream.Flush no-op Make implementations of `Stream.Flush()` a no-op instead of throw an exception. Relates to #57064. * Make Stream.FlushAsync no-op - Override `Stream.FlushAsync()` methods as a no-op. - Make implementations of `Stream.FlushAsync()` a no-op instead of throw an exception.
1 parent 24e0d10 commit 876e36d

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

src/Components/Server/src/Circuits/RemoteJSDataStream.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ public override long Position
161161
}
162162

163163
public override void Flush()
164-
=> throw new NotSupportedException();
164+
{
165+
// No-op
166+
}
167+
168+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
165169

166170
public override int Read(byte[] buffer, int offset, int count)
167171
=> throw new NotSupportedException("Synchronous reads are not supported.");

src/Components/Shared/src/PullFromJSDataStream.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public override long Position
5555
}
5656

5757
public override void Flush()
58-
=> throw new NotSupportedException();
58+
{
59+
// No-op
60+
}
61+
62+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
5963

6064
public override int Read(byte[] buffer, int offset, int count)
6165
=> throw new NotSupportedException("Synchronous reads are not supported.");

src/Hosting/TestHost/src/ResponseBodyReaderStream.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ public override long Position
4848

4949
public override void SetLength(long value) => throw new NotSupportedException();
5050

51-
public override void Flush() => throw new NotSupportedException();
51+
public override void Flush()
52+
{
53+
// No-op
54+
}
5255

53-
public override Task FlushAsync(CancellationToken cancellationToken) => throw new NotSupportedException();
56+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
5457

5558
// Write with count 0 will still trigger OnFirstWrite
5659
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();

src/Mvc/shared/Mvc.Core.TestCommon/NonSeekableReadableStream.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ public override long Position
3838

3939
public override void Flush()
4040
{
41-
throw new NotImplementedException();
41+
// No-op
4242
}
4343

44+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
45+
4446
public override long Seek(long offset, SeekOrigin origin)
4547
{
4648
throw new NotSupportedException();

src/Servers/HttpSys/src/RequestProcessing/RequestStream.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ public override long Seek(long offset, SeekOrigin origin)
7777

7878
public override void SetLength(long value) => throw new NotSupportedException(Resources.Exception_NoSeek);
7979

80-
public override void Flush() => throw new InvalidOperationException(Resources.Exception_ReadOnlyStream);
80+
public override void Flush()
81+
{
82+
// No-op
83+
}
8184

82-
public override Task FlushAsync(CancellationToken cancellationToken)
83-
=> throw new InvalidOperationException(Resources.Exception_ReadOnlyStream);
85+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
8486

8587
internal void SwitchToOpaqueMode()
8688
{

src/Shared/SegmentWriteStream.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ protected override void Dispose(bool disposing)
9999

100100
public override void Flush()
101101
{
102-
if (!CanWrite)
103-
{
104-
throw new ObjectDisposedException(nameof(SegmentWriteStream), "The stream has been closed for writing.");
105-
}
102+
// No-op
106103
}
107104

105+
public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
106+
108107
public override int Read(byte[] buffer, int offset, int count)
109108
{
110109
throw new NotSupportedException("The stream does not support reading.");

0 commit comments

Comments
 (0)