mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 15:04:43 +00:00
Rationalize AsyncDisposable
This commit is contained in:
@@ -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)
|
||||
|
||||
23
src/SharpCompress/Writers/IAsyncWriter.cs
Normal file
23
src/SharpCompress/Writers/IAsyncWriter.cs
Normal 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
|
||||
);
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user