Rationalize AsyncDisposable

This commit is contained in:
Adam Hathcock
2026-04-30 13:57:03 +01:00
parent de36344863
commit 2f3d11ca61
5 changed files with 52 additions and 22 deletions

View File

@@ -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)

View File

@@ -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
);
}

View File

@@ -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
);
}

View File

@@ -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,

View File

@@ -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"));
}