Skip to content

Commit 9782335

Browse files
committed
Add BrotliStream.DisposeAsync override
1 parent 4c37e0f commit 9782335

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/System.IO.Compression.Brotli/ref/System.IO.Compression.Brotli.cs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public BrotliStream(System.IO.Stream stream, System.IO.Compression.CompressionMo
3636
public override long Length { get { throw null; } }
3737
public override long Position { get { throw null; } set { } }
3838
protected override void Dispose(bool disposing) { }
39+
public override System.Threading.Tasks.ValueTask DisposeAsync() { throw null; }
3940
public override void Flush() { }
4041
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState) { throw null; }
4142
public override int EndRead(IAsyncResult asyncResult) { throw null; }

src/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Runtime.CompilerServices;
77
using System.Threading;
8+
using System.Threading.Tasks;
89

910
namespace System.IO.Compression
1011
{
@@ -74,6 +75,31 @@ protected override void Dispose(bool disposing)
7475
}
7576
}
7677

78+
public override async ValueTask DisposeAsync()
79+
{
80+
try
81+
{
82+
if (_stream != null)
83+
{
84+
if (_mode == CompressionMode.Compress)
85+
{
86+
await WriteAsyncMemoryCore(ReadOnlyMemory<byte>.Empty, CancellationToken.None, isFinalBlock: true).ConfigureAwait(false);
87+
}
88+
89+
if (!_leaveOpen)
90+
{
91+
await _stream.DisposeAsync().ConfigureAwait(false);
92+
}
93+
}
94+
}
95+
finally
96+
{
97+
_stream = null;
98+
_encoder.Dispose();
99+
_decoder.Dispose();
100+
}
101+
}
102+
77103
private static void ValidateParameters(byte[] array, int offset, int count)
78104
{
79105
if (array == null)

src/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
8181
WriteAsyncMemoryCore(buffer, cancellationToken));
8282
}
8383

84-
private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
84+
private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken, bool isFinalBlock = false)
8585
{
8686
AsyncOperationStarting();
8787
try
@@ -92,7 +92,7 @@ private async Task WriteAsyncMemoryCore(ReadOnlyMemory<byte> buffer, Cancellatio
9292
Memory<byte> output = new Memory<byte>(_buffer);
9393
int bytesConsumed = 0;
9494
int bytesWritten = 0;
95-
lastResult = _encoder.Compress(buffer, output, out bytesConsumed, out bytesWritten, isFinalBlock: false);
95+
lastResult = _encoder.Compress(buffer, output, out bytesConsumed, out bytesWritten, isFinalBlock);
9696
if (lastResult == OperationStatus.InvalidData)
9797
throw new InvalidOperationException(SR.BrotliStream_Compress_InvalidData);
9898
if (bytesConsumed > 0)

src/System.IO.Compression.Brotli/tests/CompressionStreamUnitTests.Brotli.cs

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ public void Precancellation()
3333
}
3434
}
3535

36+
[Theory]
37+
[InlineData(false)]
38+
[InlineData(true)]
39+
public async Task DisposeAsync_Flushes(bool leaveOpen)
40+
{
41+
var ms = new MemoryStream();
42+
var bs = new BrotliStream(ms, CompressionMode.Compress, leaveOpen);
43+
bs.WriteByte(1);
44+
Assert.Equal(0, ms.Position);
45+
await bs.DisposeAsync();
46+
Assert.InRange(ms.ToArray().Length, 1, int.MaxValue);
47+
if (leaveOpen)
48+
{
49+
Assert.InRange(ms.Position, 1, int.MaxValue);
50+
}
51+
else
52+
{
53+
Assert.Throws<ObjectDisposedException>(() => ms.Position);
54+
}
55+
}
56+
3657
[Fact]
3758
[OuterLoop("Test takes ~6 seconds to run")]
3859
public override void FlushAsync_DuringWriteAsync() { base.FlushAsync_DuringWriteAsync(); }

0 commit comments

Comments
 (0)