From 2f3d11ca61c908fde0228215ab7a418b85b8d03e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 30 Apr 2026 13:57:03 +0100 Subject: [PATCH] Rationalize AsyncDisposable --- src/SharpCompress/Writers/GZip/GZipWriter.cs | 21 +++++++++++++++++ src/SharpCompress/Writers/IAsyncWriter.cs | 23 +++++++++++++++++++ src/SharpCompress/Writers/IWriter.cs | 18 --------------- tests/SharpCompress.Test/GZip/AsyncTests.cs | 3 +-- .../GZip/GZipWriterAsyncTests.cs | 9 ++++++-- 5 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 src/SharpCompress/Writers/IAsyncWriter.cs diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.cs b/src/SharpCompress/Writers/GZip/GZipWriter.cs index eebac616..e25e4f9c 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.Compressors; using SharpCompress.Compressors.Deflate; @@ -48,6 +49,26 @@ public sealed partial class GZipWriter : AbstractWriter base.Dispose(isDisposing); } +#pragma warning disable CA2215 // base.DisposeAsync() calls the sync Dispose path for writers. + public override async ValueTask DisposeAsync() + { + if (_isDisposed) + { + return; + } + + GC.SuppressFinalize(this); + _isDisposed = true; + +#if !LEGACY_DOTNET || NETSTANDARD2_1 + await OutputStream.NotNull().DisposeAsync().ConfigureAwait(false); +#else + OutputStream.NotNull().Dispose(); + await Task.CompletedTask.ConfigureAwait(false); +#endif + } +#pragma warning restore CA2215 + public override void Write(string filename, Stream source, DateTime? modificationTime) { if (_wroteToStream) diff --git a/src/SharpCompress/Writers/IAsyncWriter.cs b/src/SharpCompress/Writers/IAsyncWriter.cs new file mode 100644 index 00000000..121b5ebc --- /dev/null +++ b/src/SharpCompress/Writers/IAsyncWriter.cs @@ -0,0 +1,23 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; + +namespace SharpCompress.Writers; + +public interface IAsyncWriter : IAsyncDisposable +{ + ArchiveType WriterType { get; } + ValueTask WriteAsync( + string filename, + Stream source, + DateTime? modificationTime, + CancellationToken cancellationToken = default + ); + ValueTask WriteDirectoryAsync( + string directoryName, + DateTime? modificationTime, + CancellationToken cancellationToken = default + ); +} diff --git a/src/SharpCompress/Writers/IWriter.cs b/src/SharpCompress/Writers/IWriter.cs index 648c0fe0..4142d602 100644 --- a/src/SharpCompress/Writers/IWriter.cs +++ b/src/SharpCompress/Writers/IWriter.cs @@ -1,7 +1,5 @@ using System; using System.IO; -using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; namespace SharpCompress.Writers; @@ -12,19 +10,3 @@ public interface IWriter : IDisposable void Write(string filename, Stream source, DateTime? modificationTime); void WriteDirectory(string directoryName, DateTime? modificationTime); } - -public interface IAsyncWriter : IDisposable, IAsyncDisposable -{ - ArchiveType WriterType { get; } - ValueTask WriteAsync( - string filename, - Stream source, - DateTime? modificationTime, - CancellationToken cancellationToken = default - ); - ValueTask WriteDirectoryAsync( - string directoryName, - DateTime? modificationTime, - CancellationToken cancellationToken = default - ); -} diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index 150ab4ae..9a0a8590 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -100,11 +100,10 @@ public class AsyncTests : TestBase #if NETFRAMEWORK using (var stream = File.Create(outputPath)) - using ( #else await using (var stream = File.Create(outputPath)) - await using ( #endif + await using ( var writer = await WriterFactory.OpenAsyncWriter( new AsyncOnlyStream(stream), ArchiveType.Zip, diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index 86c2cf35..0cfda96a 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -23,7 +23,7 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using ( + await using ( var writer = await WriterFactory.OpenAsyncWriter( new AsyncOnlyStream(stream), ArchiveType.GZip, @@ -49,7 +49,12 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = new GZipWriter(new AsyncOnlyStream(stream))) +#if NETFRAMEWORK + using ( +#else + await using ( +#endif + var writer = new GZipWriter(new AsyncOnlyStream(stream))) { await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); }