diff --git a/.editorconfig b/.editorconfig index 3bb29d9d..eaab4189 100644 --- a/.editorconfig +++ b/.editorconfig @@ -257,7 +257,7 @@ csharp_style_namespace_declarations = file_scoped ########################################## [*.{cs,csx,cake,vb,vbx}] -dotnet_diagnostic.CA1000.severity = suggestion # do not declare static members on generic types +dotnet_diagnostic.CA1000.severity = error # do not declare static members on generic types dotnet_diagnostic.CA1001.severity = error # disposable field owners should be disposable dotnet_diagnostic.CA1018.severity = error # mark custom attributes with AttributeUsage dotnet_diagnostic.CA1036.severity = silent @@ -267,29 +267,29 @@ dotnet_diagnostic.CA1069.severity = error # enums should not have duplicate valu dotnet_diagnostic.CA1304.severity = error # specify CultureInfo for culture-sensitive operations dotnet_diagnostic.CA1305.severity = suggestion # specify IFormatProvider dotnet_diagnostic.CA1307.severity = suggestion # specify StringComparison for clarity -dotnet_diagnostic.CA1309.severity = suggestion # use ordinal StringComparison +dotnet_diagnostic.CA1309.severity = error # use ordinal StringComparison dotnet_diagnostic.CA1310.severity = error # specify StringComparison for correctness -dotnet_diagnostic.CA1507.severity = suggestion # use nameof in place of string literals +dotnet_diagnostic.CA1507.severity = error # use nameof in place of string literals dotnet_diagnostic.CA1513.severity = suggestion # use ObjectDisposedException throw helper dotnet_diagnostic.CA1707.severity = suggestion # identifiers should not contain underscores dotnet_diagnostic.CA1708.severity = suggestion # identifiers should differ by more than case dotnet_diagnostic.CA1711.severity = suggestion # identifiers should not have incorrect suffixes dotnet_diagnostic.CA1716.severity = suggestion # identifiers should not match language keywords dotnet_diagnostic.CA1720.severity = suggestion # identifiers should not contain type names -dotnet_diagnostic.CA1725.severity = suggestion # parameter names should match base declaration +dotnet_diagnostic.CA1725.severity = error # parameter names should match base declaration dotnet_diagnostic.CA1805.severity = suggestion # avoid unnecessary default value initialization dotnet_diagnostic.CA1816.severity = suggestion # call GC.SuppressFinalize correctly dotnet_diagnostic.CA1822.severity = suggestion # mark members static when possible dotnet_diagnostic.CA1825.severity = error # avoid zero-length array allocations dotnet_diagnostic.CA1826.severity = silent dotnet_diagnostic.CA1827.severity = error # use Any() instead of Count()/LongCount() checks -dotnet_diagnostic.CA1829.severity = suggestion # use Length or Count property instead of LINQ Count() +dotnet_diagnostic.CA1829.severity = error # use Length or Count property instead of LINQ Count() dotnet_diagnostic.CA1834.severity = error # prefer StringBuilder.Append(char) for single chars -dotnet_diagnostic.CA1845.severity = suggestion # use span-based string.Concat overloads -dotnet_diagnostic.CA1848.severity = suggestion # use LoggerMessage for high-performance logging +dotnet_diagnostic.CA1845.severity = error # use span-based string.Concat overloads +dotnet_diagnostic.CA1848.severity = error # use LoggerMessage for high-performance logging dotnet_diagnostic.CA1852.severity = suggestion # seal types that are not intended for inheritance dotnet_diagnostic.CA1860.severity = silent -dotnet_diagnostic.CA2016.severity = suggestion # forward CancellationToken to invoked methods +dotnet_diagnostic.CA2016.severity = error # forward CancellationToken to invoked methods dotnet_diagnostic.CA2201.severity = error # do not throw reserved or overly general exceptions dotnet_diagnostic.CA2206.severity = error # enforce CA2206 usage guidance dotnet_diagnostic.CA2208.severity = error # instantiate ArgumentException types correctly @@ -297,12 +297,12 @@ dotnet_diagnostic.CA2211.severity = error # non-constant fields should not be vi dotnet_diagnostic.CA2249.severity = error # prefer string.Contains over string.IndexOf checks dotnet_diagnostic.CA2251.severity = error # use string.Equals over string.Compare equality checks dotnet_diagnostic.CA2252.severity = none -dotnet_diagnostic.CA2254.severity = suggestion # logging message templates should be static expressions +dotnet_diagnostic.CA2254.severity = error # logging message templates should be static expressions ; High volume analyzers requiring extensive refactoring - set to suggestion temporarily dotnet_diagnostic.CA1835.severity = suggestion # prefer Memory-based async overloads -dotnet_diagnostic.CA1510.severity = suggestion # use ArgumentNullException.ThrowIfNull -dotnet_diagnostic.CA1512.severity = suggestion # use ArgumentOutOfRangeException throw helpers +dotnet_diagnostic.CA1510.severity = error # use ArgumentNullException.ThrowIfNull +dotnet_diagnostic.CA1512.severity = error # use ArgumentOutOfRangeException throw helpers dotnet_diagnostic.CA1844.severity = suggestion # provide memory-based async stream overrides dotnet_diagnostic.CA1825.severity = error # avoid zero-length array allocations dotnet_diagnostic.CA1712.severity = suggestion # do not prefix enum values with type name @@ -310,11 +310,11 @@ dotnet_diagnostic.CA2022.severity = suggestion # avoid inexact reads with Stream dotnet_diagnostic.CA1850.severity = error # prefer static HashData over ComputeHash dotnet_diagnostic.CA2263.severity = error # prefer generic overload when type is known dotnet_diagnostic.CA2012.severity = suggestion # use ValueTasks correctly -dotnet_diagnostic.CA1001.severity = suggestion # disposable field owners should be disposable +dotnet_diagnostic.CA1001.severity = error # disposable field owners should be disposable dotnet_diagnostic.CS0169.severity = error # field is never used dotnet_diagnostic.CS0219.severity = error # variable assigned but never used -dotnet_diagnostic.CS0649.severity = suggestion # field is never assigned and remains default +dotnet_diagnostic.CS0649.severity = error # field is never assigned and remains default dotnet_diagnostic.CS1998.severity = error # async method lacks await operators dotnet_diagnostic.CS8602.severity = error # possible null reference dereference dotnet_diagnostic.CS8604.severity = error # possible null reference argument diff --git a/build/Program.cs b/build/Program.cs index 82c4d75a..f2072b7a 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -650,7 +650,7 @@ static double ParseTimeValue(string timeStr) return 0; } - var value = double.Parse(match.Groups[1].Value); + var value = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); var unit = match.Groups[2].Value.ToLower(CultureInfo.InvariantCulture); // Convert to microseconds for comparison @@ -679,7 +679,7 @@ static double ParseMemoryValue(string memStr) return 0; } - var value = double.Parse(match.Groups[1].Value); + var value = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); var unit = match.Groups[2].Value.ToUpper(CultureInfo.InvariantCulture); // Convert to KB for comparison diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 5d87d7bd..e47bf7c6 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -37,7 +37,7 @@ public partial class GZipArchive } protected override GZipArchiveEntry CreateEntryInternal( - string filePath, + string key, Stream source, long size, DateTime? modified, @@ -48,13 +48,11 @@ public partial class GZipArchive { throw new InvalidFormatException("Only one entry is allowed in a GZip Archive"); } - return new GZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream); + return new GZipWritableArchiveEntry(this, source, key, size, modified, closeStream); } - protected override GZipArchiveEntry CreateDirectoryEntry( - string directoryPath, - DateTime? modified - ) => throw new NotSupportedException("GZip archives do not support directory entries."); + protected override GZipArchiveEntry CreateDirectoryEntry(string key, DateTime? modified) => + throw new NotSupportedException("GZip archives do not support directory entries."); protected override void SaveTo( Stream stream, diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs index edf74590..707049d3 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs @@ -31,7 +31,7 @@ public partial class RarArchive return new((IRarAsyncArchive)OpenArchive(new FileInfo(path), readerOptions)); } - public static IRarArchive OpenArchive(string filePath, ReaderOptions? options = null) + public static IRarArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); var fileInfo = new FileInfo(filePath); @@ -39,24 +39,24 @@ public partial class RarArchive new SourceStream( fileInfo, i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), - options ?? new ReaderOptions() + readerOptions ?? new ReaderOptions() ) ); } - public static IRarArchive OpenArchive(FileInfo fileInfo, ReaderOptions? options = null) + public static IRarArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new RarArchive( new SourceStream( fileInfo, i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), - options ?? new ReaderOptions() + readerOptions ?? new ReaderOptions() ) ); } - public static IRarArchive OpenArchive(Stream stream, ReaderOptions? options = null) + public static IRarArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -65,7 +65,9 @@ public partial class RarArchive throw new ArgumentException("Stream must be seekable", nameof(stream)); } - return new RarArchive(new SourceStream(stream, _ => null, options ?? new ReaderOptions())); + return new RarArchive( + new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions()) + ); } public static IRarArchive OpenArchive( diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs index db06ec9a..30adbaf9 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs @@ -37,7 +37,7 @@ public partial class SevenZipArchive public static IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - fileInfo.NotNull("fileInfo"); + fileInfo.NotNull(nameof(fileInfo)); return new SevenZipArchive( new SourceStream( fileInfo, @@ -81,7 +81,7 @@ public partial class SevenZipArchive public static IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { - stream.NotNull("stream"); + stream.NotNull(nameof(stream)); if (stream is not { CanSeek: true }) { diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index c200400f..fdd8ac3a 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -182,7 +182,7 @@ public partial class TarArchive } protected override TarArchiveEntry CreateEntryInternal( - string filePath, + string key, Stream source, long size, DateTime? modified, @@ -192,16 +192,14 @@ public partial class TarArchive this, source, CompressionType.Unknown, - filePath, + key, size, modified, closeStream ); - protected override TarArchiveEntry CreateDirectoryEntry( - string directoryPath, - DateTime? modified - ) => new TarWritableArchiveEntry(this, directoryPath, modified); + protected override TarArchiveEntry CreateDirectoryEntry(string key, DateTime? modified) => + new TarWritableArchiveEntry(this, key, modified); protected override void SaveTo( Stream stream, diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index cff4339f..20604814 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -34,14 +34,14 @@ public partial class ZipArchive internal ZipArchive() : base(ArchiveType.Zip) { } - protected override IEnumerable LoadVolumes(SourceStream stream) + protected override IEnumerable LoadVolumes(SourceStream sourceStream) { - stream.LoadAllParts(); + sourceStream.LoadAllParts(); //stream.Position = 0; - var streams = stream.Streams.ToList(); + var streams = sourceStream.Streams.ToList(); var idx = 0; - if (streams.Count() > 1) + if (streams.Count > 1) { //check if second stream is zip header without changing position var headerProbeStream = streams[1]; @@ -51,7 +51,7 @@ public partial class ZipArchive headerProbeStream.Position = startPosition; if (isZip) { - stream.IsVolumes = true; + sourceStream.IsVolumes = true; var tmp = streams[0]; streams.RemoveAt(0); @@ -61,7 +61,7 @@ public partial class ZipArchive } } - return new ZipVolume(stream, ReaderOptions, idx++).AsEnumerable(); + return new ZipVolume(sourceStream, ReaderOptions, idx++).AsEnumerable(); } protected override IEnumerable LoadEntries(IEnumerable volumes) @@ -150,17 +150,15 @@ public partial class ZipArchive } protected override ZipArchiveEntry CreateEntryInternal( - string filePath, + string key, Stream source, long size, DateTime? modified, bool closeStream - ) => new ZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream); + ) => new ZipWritableArchiveEntry(this, source, key, size, modified, closeStream); - protected override ZipArchiveEntry CreateDirectoryEntry( - string directoryPath, - DateTime? modified - ) => new ZipWritableArchiveEntry(this, directoryPath, modified); + protected override ZipArchiveEntry CreateDirectoryEntry(string key, DateTime? modified) => + new ZipWritableArchiveEntry(this, key, modified); protected override IReader CreateReaderForSolidExtraction() { diff --git a/src/SharpCompress/Common/Ace/Headers/AceFileHeader.Async.cs b/src/SharpCompress/Common/Ace/Headers/AceFileHeader.Async.cs index 8f8a031e..4f4aecbf 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceFileHeader.Async.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceFileHeader.Async.cs @@ -14,11 +14,11 @@ public sealed partial class AceFileHeader /// Supports both ACE 1.0 and ACE 2.0 formats. /// public override async ValueTask ReadAsync( - Stream stream, + Stream reader, CancellationToken cancellationToken = default ) { - var headerData = await ReadHeaderAsync(stream, cancellationToken).ConfigureAwait(false); + var headerData = await ReadHeaderAsync(reader, cancellationToken).ConfigureAwait(false); if (headerData.Length == 0) { return null; @@ -104,7 +104,7 @@ public sealed partial class AceFileHeader } // Store the data start position - DataStartPosition = stream.Position; + DataStartPosition = reader.Position; return this; } diff --git a/src/SharpCompress/Common/Ace/Headers/AceFileHeader.cs b/src/SharpCompress/Common/Ace/Headers/AceFileHeader.cs index 9a968810..1c71d18d 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceFileHeader.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceFileHeader.cs @@ -56,9 +56,9 @@ public sealed partial class AceFileHeader : AceHeader /// Returns null if no more entries or end of archive. /// Supports both ACE 1.0 and ACE 2.0 formats. /// - public override AceHeader? Read(Stream stream) + public override AceHeader? Read(Stream reader) { - var headerData = ReadHeader(stream); + var headerData = ReadHeader(reader); if (headerData.Length == 0) { return null; @@ -144,7 +144,7 @@ public sealed partial class AceFileHeader : AceHeader } // Store the data start position - DataStartPosition = stream.Position; + DataStartPosition = reader.Position; return this; } diff --git a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs index 290cd199..b21ae450 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.Async.cs @@ -15,11 +15,11 @@ public sealed partial class AceMainHeader /// Supports both ACE 1.0 and ACE 2.0 formats. /// public override async ValueTask ReadAsync( - Stream stream, + Stream reader, CancellationToken cancellationToken = default ) { - var headerData = await ReadHeaderAsync(stream, cancellationToken).ConfigureAwait(false); + var headerData = await ReadHeaderAsync(reader, cancellationToken).ConfigureAwait(false); if (headerData.Length == 0) { return null; diff --git a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs index fd74354f..01a19bcf 100644 --- a/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs +++ b/src/SharpCompress/Common/Ace/Headers/AceMainHeader.cs @@ -32,9 +32,9 @@ public sealed partial class AceMainHeader : AceHeader /// Returns header if this is a valid ACE archive. /// Supports both ACE 1.0 and ACE 2.0 formats. /// - public override AceHeader? Read(Stream stream) + public override AceHeader? Read(Stream reader) { - var headerData = ReadHeader(stream); + var headerData = ReadHeader(reader); if (headerData.Length == 0) { return null; diff --git a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.Async.cs b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.Async.cs index c5558325..5b5dab70 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.Async.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.Async.cs @@ -7,16 +7,16 @@ namespace SharpCompress.Common.Arj.Headers; public partial class ArjLocalHeader { public override async ValueTask ReadAsync( - Stream stream, + Stream reader, CancellationToken cancellationToken = default ) { - var body = await ReadHeaderAsync(stream, cancellationToken).ConfigureAwait(false); + var body = await ReadHeaderAsync(reader, cancellationToken).ConfigureAwait(false); if (body.Length > 0) { - await ReadExtendedHeadersAsync(stream, cancellationToken).ConfigureAwait(false); + await ReadExtendedHeadersAsync(reader, cancellationToken).ConfigureAwait(false); var header = LoadFrom(body); - header.DataStartPosition = stream.Position; + header.DataStartPosition = reader.Position; return header; } return null; diff --git a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs index d37121a9..5e5145f5 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjLocalHeader.cs @@ -43,14 +43,14 @@ public partial class ArjLocalHeader : ArjHeader archiveEncoding ?? throw new ArgumentNullException(nameof(archiveEncoding)); } - public override ArjHeader? Read(Stream stream) + public override ArjHeader? Read(Stream reader) { - var body = ReadHeader(stream); + var body = ReadHeader(reader); if (body.Length > 0) { - ReadExtendedHeaders(stream); + ReadExtendedHeaders(reader); var header = LoadFrom(body); - header.DataStartPosition = stream.Position; + header.DataStartPosition = reader.Position; return header; } return null; diff --git a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.Async.cs b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.Async.cs index f337b0d3..2271877c 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.Async.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.Async.cs @@ -7,12 +7,12 @@ namespace SharpCompress.Common.Arj.Headers; public partial class ArjMainHeader { public override async ValueTask ReadAsync( - Stream stream, + Stream reader, CancellationToken cancellationToken = default ) { - var body = await ReadHeaderAsync(stream, cancellationToken).ConfigureAwait(false); - await ReadExtendedHeadersAsync(stream, cancellationToken).ConfigureAwait(false); + var body = await ReadHeaderAsync(reader, cancellationToken).ConfigureAwait(false); + await ReadExtendedHeadersAsync(reader, cancellationToken).ConfigureAwait(false); return LoadFrom(body); } } diff --git a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs index 5fbc9486..e81d2669 100644 --- a/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs +++ b/src/SharpCompress/Common/Arj/Headers/ArjMainHeader.cs @@ -37,10 +37,10 @@ public partial class ArjMainHeader : ArjHeader archiveEncoding ?? throw new ArgumentNullException(nameof(archiveEncoding)); } - public override ArjHeader? Read(Stream stream) + public override ArjHeader? Read(Stream reader) { - var body = ReadHeader(stream); - ReadExtendedHeaders(stream); + var body = ReadHeader(reader); + ReadExtendedHeaders(reader); return LoadFrom(body); } diff --git a/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs b/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs index 41a8b037..5636f321 100644 --- a/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/AsyncMarkingBinaryReader.cs @@ -8,7 +8,10 @@ using SharpCompress.IO; namespace SharpCompress.Common.Rar; -internal class AsyncMarkingBinaryReader +internal class AsyncMarkingBinaryReader : IDisposable +#if NET8_0_OR_GREATER + , IAsyncDisposable +#endif { private readonly AsyncBinaryReader _reader; @@ -187,4 +190,9 @@ internal class AsyncMarkingBinaryReader throw new FormatException("malformed vint"); } + + public virtual void Dispose() => _reader.Dispose(); +#if NET8_0_OR_GREATER + public virtual ValueTask DisposeAsync() => _reader.DisposeAsync(); +#endif } diff --git a/src/SharpCompress/Common/Rar/RarVolume.cs b/src/SharpCompress/Common/Rar/RarVolume.cs index 27134985..e2bc6a45 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -249,7 +249,9 @@ public abstract class RarVolume : Volume } // we only want to load the archive header to avoid overhead but have to do the nasty thing and reset the stream +#pragma warning disable CA2016 // Forward token if available; polyfill FirstAsync has no token overload await GetVolumeFilePartsAsync(cancellationToken).FirstAsync().ConfigureAwait(false); +#pragma warning restore CA2016 Stream.Position = 0; } } diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs index fa910ca0..9b3bf22f 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.Async.cs @@ -266,7 +266,7 @@ internal sealed partial class TarHeader LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime(); Magic = ArchiveEncoding.Decode(buffer, 257, 6).TrimNulls(); - if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic)) + if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic, StringComparison.Ordinal)) { var namePrefix = ArchiveEncoding.Decode(buffer, 345, 157).TrimNulls(); diff --git a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs index f67e8c74..5ceda5e7 100644 --- a/src/SharpCompress/Common/Tar/Headers/TarHeader.cs +++ b/src/SharpCompress/Common/Tar/Headers/TarHeader.cs @@ -299,7 +299,7 @@ internal sealed partial class TarHeader LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime(); Magic = ArchiveEncoding.Decode(buffer, 257, 6).TrimNulls(); - if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic)) + if (!string.IsNullOrEmpty(Magic) && "ustar".Equals(Magic, StringComparison.Ordinal)) { var namePrefix = ArchiveEncoding.Decode(buffer, 345, 157).TrimNulls(); diff --git a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs index 5cc805b7..f541fc8d 100644 --- a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs +++ b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.Async.cs @@ -12,10 +12,7 @@ internal abstract partial class ZipFileEntry CancellationToken cancellationToken = default ) { - if (archiveStream is null) - { - throw new ArgumentNullException(nameof(archiveStream)); - } + ThrowHelper.ThrowIfNull(archiveStream); var buffer = new byte[12]; await archiveStream.ReadFullyAsync(buffer, 0, 12, cancellationToken).ConfigureAwait(false); diff --git a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs index eb15ae3d..b62b6a67 100644 --- a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs +++ b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs @@ -44,10 +44,7 @@ internal abstract partial class ZipFileEntry(ZipHeaderType type, IArchiveEncodin internal PkwareTraditionalEncryptionData ComposeEncryptionData(Stream archiveStream) { - if (archiveStream is null) - { - throw new ArgumentNullException(nameof(archiveStream)); - } + ThrowHelper.ThrowIfNull(archiveStream); var buffer = new byte[12]; archiveStream.ReadFully(buffer); diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs index a17b04d2..32a0a7bf 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.Async.cs @@ -20,10 +20,7 @@ internal partial class PkwareTraditionalCryptoStream throw new NotSupportedException("This stream does not encrypt via Read()"); } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); var temp = new byte[count]; var readBytes = await _stream diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs index 65e23347..2efd036e 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalCryptoStream.cs @@ -48,10 +48,7 @@ internal partial class PkwareTraditionalCryptoStream : Stream throw new NotSupportedException("This stream does not encrypt via Read()"); } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); var temp = new byte[count]; var readBytes = _stream.Read(temp, 0, count); diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs index c0c64524..63d46bb6 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs @@ -69,10 +69,7 @@ internal class PkwareTraditionalEncryptionData public byte[] Encrypt(byte[] plainText, int length) { - if (plainText is null) - { - throw new ArgumentNullException(nameof(plainText)); - } + ThrowHelper.ThrowIfNull(plainText); if (length > plainText.Length) { diff --git a/src/SharpCompress/Compressors/ADC/ADCStream.Async.cs b/src/SharpCompress/Compressors/ADC/ADCStream.Async.cs index 22941be5..d2e535cd 100644 --- a/src/SharpCompress/Compressors/ADC/ADCStream.Async.cs +++ b/src/SharpCompress/Compressors/ADC/ADCStream.Async.cs @@ -47,18 +47,9 @@ public sealed partial class ADCStream { return 0; } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (offset < buffer.GetLowerBound(0)) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ThrowHelper.ThrowIfNull(buffer); + ThrowHelper.ThrowIfNegative(count); + ThrowHelper.ThrowIfLessThan(offset, buffer.GetLowerBound(0)); if ((offset + count) > buffer.GetLength(0)) { throw new ArgumentOutOfRangeException(nameof(count)); diff --git a/src/SharpCompress/Compressors/ADC/ADCStream.cs b/src/SharpCompress/Compressors/ADC/ADCStream.cs index cb67d9d2..f2ef155e 100644 --- a/src/SharpCompress/Compressors/ADC/ADCStream.cs +++ b/src/SharpCompress/Compressors/ADC/ADCStream.cs @@ -110,18 +110,9 @@ public sealed partial class ADCStream : Stream { return 0; } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (offset < buffer.GetLowerBound(0)) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ThrowHelper.ThrowIfNull(buffer); + ThrowHelper.ThrowIfNegative(count); + ThrowHelper.ThrowIfLessThan(offset, buffer.GetLowerBound(0)); if ((offset + count) > buffer.GetLength(0)) { throw new ArgumentOutOfRangeException(nameof(count)); diff --git a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.Async.cs b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.Async.cs index 30ae6e1b..56496c9a 100644 --- a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.Async.cs +++ b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.Async.cs @@ -34,7 +34,7 @@ public partial class ArcLzwStream totalRead += read; } var decoded = Decompress(data, _useCrunched); - var result = decoded.Count(); + var result = decoded.Count; if (_useCrunched) { var unpacked = RLE.UnpackRLE(decoded.ToArray()); diff --git a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs index ef28d866..29373659 100644 --- a/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs +++ b/src/SharpCompress/Compressors/ArcLzw/ArcLzwStream.cs @@ -176,7 +176,7 @@ public partial class ArcLzwStream : Stream var data = new byte[_compressedSize]; _stream.Read(data, 0, _compressedSize); var decoded = Decompress(data, _useCrunched); - var result = decoded.Count(); + var result = decoded.Count; if (_useCrunched) { var unpacked = RLE.UnpackRLE(decoded.ToArray()); diff --git a/src/SharpCompress/Compressors/Arj/HuffmanTree.cs b/src/SharpCompress/Compressors/Arj/HuffmanTree.cs index 63bf8968..a7426ca2 100644 --- a/src/SharpCompress/Compressors/Arj/HuffmanTree.cs +++ b/src/SharpCompress/Compressors/Arj/HuffmanTree.cs @@ -61,10 +61,7 @@ public sealed partial class HuffTree public void BuildTree(byte[] lengths, int count) { - if (lengths == null) - { - throw new ArgumentNullException(nameof(lengths)); - } + ThrowHelper.ThrowIfNull(lengths); if (count < 0 || count > lengths.Length) { @@ -85,10 +82,7 @@ public sealed partial class HuffTree public void BuildTree(byte[] valueLengths) { - if (valueLengths == null) - { - throw new ArgumentNullException(nameof(valueLengths)); - } + ThrowHelper.ThrowIfNull(valueLengths); if (valueLengths.Length > TreeEntry.MAX_INDEX / 2) { diff --git a/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs b/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs index 037c56ba..f978b0a8 100644 --- a/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs +++ b/src/SharpCompress/Compressors/Arj/LHDecoderStream.Async.cs @@ -92,10 +92,7 @@ public sealed partial class LHDecoderStream throw new ObjectDisposedException(nameof(LHDecoderStream)); } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { diff --git a/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs b/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs index 52b51a73..5b7c6ce5 100644 --- a/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs +++ b/src/SharpCompress/Compressors/Arj/LHDecoderStream.cs @@ -123,10 +123,7 @@ public sealed partial class LHDecoderStream : Stream throw new ObjectDisposedException(nameof(LHDecoderStream)); } - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { @@ -179,4 +176,14 @@ public sealed partial class LHDecoderStream : Stream public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _disposed = true; + _stream.Dispose(); + } + base.Dispose(disposing); + } } diff --git a/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs b/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs index 8bb15a14..0dfe1a88 100644 --- a/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs +++ b/src/SharpCompress/Compressors/Arj/LhaStream.Async.cs @@ -15,10 +15,7 @@ public sealed partial class LhaStream CancellationToken cancellationToken ) { - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || (offset + count) > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); diff --git a/src/SharpCompress/Compressors/Arj/LhaStream.cs b/src/SharpCompress/Compressors/Arj/LhaStream.cs index 67f65882..65195405 100644 --- a/src/SharpCompress/Compressors/Arj/LhaStream.cs +++ b/src/SharpCompress/Compressors/Arj/LhaStream.cs @@ -57,10 +57,7 @@ public sealed partial class LhaStream : Stream public override int Read(byte[] buffer, int offset, int count) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || (offset + count) > buffer.Length) { throw new ArgumentOutOfRangeException(nameof(offset)); diff --git a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs index 513029db..bd81d041 100644 --- a/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs +++ b/src/SharpCompress/Compressors/Deflate/ZlibBaseStream.cs @@ -883,18 +883,9 @@ internal class ZlibBaseStream : Stream, IStreamStack return rc; } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (offset < buffer.GetLowerBound(0)) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ThrowHelper.ThrowIfNull(buffer); + ThrowHelper.ThrowIfNegative(count); + ThrowHelper.ThrowIfLessThan(offset, buffer.GetLowerBound(0)); if ((offset + count) > buffer.GetLength(0)) { throw new ArgumentOutOfRangeException(nameof(count)); @@ -1073,18 +1064,9 @@ internal class ZlibBaseStream : Stream, IStreamStack return rc; } - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } - if (offset < buffer.GetLowerBound(0)) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ThrowHelper.ThrowIfNull(buffer); + ThrowHelper.ThrowIfNegative(count); + ThrowHelper.ThrowIfLessThan(offset, buffer.GetLowerBound(0)); if ((offset + count) > buffer.GetLength(0)) { throw new ArgumentOutOfRangeException(nameof(count)); diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs index 6a54e1b1..64652872 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.Async.cs @@ -11,13 +11,13 @@ namespace SharpCompress.Compressors.Deflate64; public sealed partial class Deflate64Stream { public override async Task ReadAsync( - byte[] array, + byte[] buffer, int offset, int count, CancellationToken cancellationToken ) { - ValidateParameters(array, offset, count); + ValidateParameters(buffer, offset, count); EnsureNotDisposed(); int bytesRead; @@ -26,7 +26,7 @@ public sealed partial class Deflate64Stream while (true) { - bytesRead = _inflater.Inflate(array, currentOffset, remainingCount); + bytesRead = _inflater.Inflate(buffer, currentOffset, remainingCount); currentOffset += bytesRead; remainingCount -= bytesRead; diff --git a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs index 51007377..59d40dc7 100644 --- a/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs +++ b/src/SharpCompress/Compressors/Deflate64/Deflate64Stream.cs @@ -22,10 +22,7 @@ public sealed partial class Deflate64Stream : Stream public Deflate64Stream(Stream stream, CompressionMode mode) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ThrowHelper.ThrowIfNull(stream); if (mode != CompressionMode.Decompress) { @@ -72,9 +69,9 @@ public sealed partial class Deflate64Stream : Stream public override void SetLength(long value) => throw new NotSupportedException("Deflate64: not supported"); - public override int Read(byte[] array, int offset, int count) + public override int Read(byte[] buffer, int offset, int count) { - ValidateParameters(array, offset, count); + ValidateParameters(buffer, offset, count); EnsureNotDisposed(); int bytesRead; @@ -83,7 +80,7 @@ public sealed partial class Deflate64Stream : Stream while (true) { - bytesRead = _inflater.Inflate(array, currentOffset, remainingCount); + bytesRead = _inflater.Inflate(buffer, currentOffset, remainingCount); currentOffset += bytesRead; remainingCount -= bytesRead; @@ -118,20 +115,11 @@ public sealed partial class Deflate64Stream : Stream private void ValidateParameters(byte[] array, int offset, int count) { - if (array is null) - { - throw new ArgumentNullException(nameof(array)); - } + ThrowHelper.ThrowIfNull(array); - if (offset < 0) - { - throw new ArgumentOutOfRangeException(nameof(offset)); - } + ThrowHelper.ThrowIfNegative(offset); - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } + ThrowHelper.ThrowIfNegative(count); if (array.Length - offset < count) { @@ -155,7 +143,7 @@ public sealed partial class Deflate64Stream : Stream private static void ThrowCannotWriteToDeflateManagedStreamException() => throw new InvalidOperationException("Deflate64: cannot write to this stream"); - public override void Write(byte[] array, int offset, int count) => + public override void Write(byte[] buffer, int offset, int count) => ThrowCannotWriteToDeflateManagedStreamException(); // This is called by Dispose: diff --git a/src/SharpCompress/Compressors/LZMA/LZ/LzBinTree.cs b/src/SharpCompress/Compressors/LZMA/LZ/LzBinTree.cs index b123e5d5..bc0a8a7a 100644 --- a/src/SharpCompress/Compressors/LZMA/LZ/LzBinTree.cs +++ b/src/SharpCompress/Compressors/LZMA/LZ/LzBinTree.cs @@ -91,10 +91,7 @@ internal sealed class BinTree : InWindow uint keepAddBufferAfter ) { - if (historySize > K_MAX_VAL_FOR_NORMALIZE - 256) - { - throw new ArgumentOutOfRangeException(nameof(historySize)); - } + ThrowHelper.ThrowIfGreaterThan(historySize, K_MAX_VAL_FOR_NORMALIZE - 256); _cutValue = 16 + (matchMaxLen >> 1); var windowReservSize = diff --git a/src/SharpCompress/Compressors/PPMd/I1/Model.cs b/src/SharpCompress/Compressors/PPMd/I1/Model.cs index 2c462285..7b32fc09 100644 --- a/src/SharpCompress/Compressors/PPMd/I1/Model.cs +++ b/src/SharpCompress/Compressors/PPMd/I1/Model.cs @@ -151,15 +151,9 @@ internal partial class Model /// public void Encode(Stream target, Stream source, PpmdProperties properties) { - if (target is null) - { - throw new ArgumentNullException(nameof(target)); - } + ThrowHelper.ThrowIfNull(target); - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ThrowHelper.ThrowIfNull(source); EncodeStart(properties); EncodeBlock(target, source, true); @@ -239,15 +233,9 @@ internal partial class Model /// public void Decode(Stream target, Stream source, PpmdProperties properties) { - if (target is null) - { - throw new ArgumentNullException(nameof(target)); - } + ThrowHelper.ThrowIfNull(target); - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ThrowHelper.ThrowIfNull(source); DecodeStart(source, properties); var buffer = new byte[65536]; diff --git a/src/SharpCompress/Compressors/PPMd/PpmdStream.cs b/src/SharpCompress/Compressors/PPMd/PpmdStream.cs index c8cf15d0..93791dc0 100644 --- a/src/SharpCompress/Compressors/PPMd/PpmdStream.cs +++ b/src/SharpCompress/Compressors/PPMd/PpmdStream.cs @@ -90,10 +90,7 @@ public class PpmdStream : Stream CancellationToken cancellationToken = default ) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ThrowHelper.ThrowIfNull(stream); if (properties.Version == PpmdVersion.H && compress) { @@ -171,21 +168,21 @@ public class PpmdStream : Stream public override void Flush() { } - protected override void Dispose(bool isDisposing) + protected override void Dispose(bool disposing) { if (_isDisposed) { return; } _isDisposed = true; - if (isDisposing) + if (disposing) { if (_compress) { _model.EncodeBlock(_stream, new MemoryStream(), true); } } - base.Dispose(isDisposing); + base.Dispose(disposing); } public override long Length => throw new NotSupportedException(); diff --git a/src/SharpCompress/Compressors/RLE90/RunLength90Stream.Async.cs b/src/SharpCompress/Compressors/RLE90/RunLength90Stream.Async.cs index 0b43b60b..95f82d2c 100644 --- a/src/SharpCompress/Compressors/RLE90/RunLength90Stream.Async.cs +++ b/src/SharpCompress/Compressors/RLE90/RunLength90Stream.Async.cs @@ -14,10 +14,7 @@ public partial class RunLength90Stream CancellationToken cancellationToken ) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { diff --git a/src/SharpCompress/Compressors/RLE90/RunLength90Stream.cs b/src/SharpCompress/Compressors/RLE90/RunLength90Stream.cs index 44f57b57..d7bffe85 100644 --- a/src/SharpCompress/Compressors/RLE90/RunLength90Stream.cs +++ b/src/SharpCompress/Compressors/RLE90/RunLength90Stream.cs @@ -53,10 +53,7 @@ public partial class RunLength90Stream : Stream public override int Read(byte[] buffer, int offset, int count) { - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs index c4548ef8..929c4eb7 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Unpack.cs @@ -27,7 +27,7 @@ internal sealed partial class Unpack : BitInput, IRarUnpack if (!disposed) { base.Dispose(); - if (!externalWindow && window is not null) + if (window is not null) { ArrayPool.Shared.Return(window); window = null; @@ -91,8 +91,6 @@ internal sealed partial class Unpack : BitInput, IRarUnpack private BlockTypes unpBlockType; - private bool externalWindow; - private long writtenFileSize; private bool ppmError; diff --git a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.Async.cs b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.Async.cs index 6ab2b814..e348b68f 100644 --- a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.Async.cs +++ b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.Async.cs @@ -14,10 +14,7 @@ internal static partial class MultiByteIntegers CancellationToken cancellationToken = default ) { - if (maxBytes <= 0) - { - throw new ArgumentOutOfRangeException(nameof(maxBytes)); - } + ThrowHelper.ThrowIfNegativeOrZero(maxBytes); if (maxBytes > 9) { diff --git a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs index 52dbd5b8..bf9f29d2 100644 --- a/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs +++ b/src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs @@ -10,10 +10,7 @@ internal static partial class MultiByteIntegers { public static ulong ReadXZInteger(this BinaryReader reader, int MaxBytes = 9) { - if (MaxBytes <= 0) - { - throw new ArgumentOutOfRangeException(nameof(MaxBytes)); - } + ThrowHelper.ThrowIfNegativeOrZero(MaxBytes); if (MaxBytes > 9) { diff --git a/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs b/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs index b9cbc974..432fcb2d 100644 --- a/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs +++ b/src/SharpCompress/Compressors/ZStandard/CompressionStream.cs @@ -32,20 +32,14 @@ public partial class CompressionStream : Stream bool leaveOpen = true ) { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } + SharpCompress.ThrowHelper.ThrowIfNull(stream); if (!stream.CanWrite) { throw new ArgumentException("Stream is not writable", nameof(stream)); } - if (bufferSize < 0) - { - throw new ArgumentOutOfRangeException(nameof(bufferSize)); - } + SharpCompress.ThrowHelper.ThrowIfNegative(bufferSize); innerStream = stream; this.compressor = compressor; diff --git a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs index b6568a22..abb55e09 100644 --- a/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs +++ b/src/SharpCompress/Compressors/ZStandard/DecompressionStream.cs @@ -37,20 +37,14 @@ public partial class DecompressionStream : Stream bool leaveOpen = true ) { - if (stream == null) - { - throw new ArgumentNullException(nameof(stream)); - } + SharpCompress.ThrowHelper.ThrowIfNull(stream); if (!stream.CanRead) { throw new ArgumentException("Stream is not readable", nameof(stream)); } - if (bufferSize < 0) - { - throw new ArgumentOutOfRangeException(nameof(bufferSize)); - } + SharpCompress.ThrowHelper.ThrowIfNegative(bufferSize); innerStream = stream; this.decompressor = decompressor; diff --git a/src/SharpCompress/Factories/LzwFactory.cs b/src/SharpCompress/Factories/LzwFactory.cs index c4797be5..cbfabb60 100644 --- a/src/SharpCompress/Factories/LzwFactory.cs +++ b/src/SharpCompress/Factories/LzwFactory.cs @@ -92,7 +92,7 @@ public class LzwFactory : Factory, IReaderFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return LzwReader.OpenAsyncReader(stream, options); + return LzwReader.OpenAsyncReader(stream, options, cancellationToken); } #endregion diff --git a/src/SharpCompress/IO/CountingStream.cs b/src/SharpCompress/IO/CountingStream.cs index ff6df3d5..ecc36cf7 100644 --- a/src/SharpCompress/IO/CountingStream.cs +++ b/src/SharpCompress/IO/CountingStream.cs @@ -9,7 +9,6 @@ namespace SharpCompress.IO; internal class CountingStream : Stream { private readonly Stream _stream; - private readonly bool _leaveOpen; private long _bytesWritten; public CountingStream(Stream stream) @@ -59,7 +58,7 @@ internal class CountingStream : Stream protected override void Dispose(bool disposing) { - if (disposing && !_leaveOpen) + if (disposing) { _stream.Dispose(); } diff --git a/src/SharpCompress/IO/SeekableSharpCompressStream.cs b/src/SharpCompress/IO/SeekableSharpCompressStream.cs index 28b67c4e..a2602a95 100644 --- a/src/SharpCompress/IO/SeekableSharpCompressStream.cs +++ b/src/SharpCompress/IO/SeekableSharpCompressStream.cs @@ -25,10 +25,7 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream public SeekableSharpCompressStream(Stream stream, bool leaveStreamOpen = false) : base(Null, true, false, null) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ThrowHelper.ThrowIfNull(stream); if (!stream.CanSeek) { throw new ArgumentException("Stream must be seekable", nameof(stream)); diff --git a/src/SharpCompress/NotNullExtensions.cs b/src/SharpCompress/NotNullExtensions.cs index 0157941b..4c25e008 100644 --- a/src/SharpCompress/NotNullExtensions.cs +++ b/src/SharpCompress/NotNullExtensions.cs @@ -52,7 +52,7 @@ internal static class NotNullExtensions ) where T : class { - ArgumentNullException.ThrowIfNull(obj, paramName); + ThrowHelper.ThrowIfNull(obj, paramName); return obj; } diff --git a/src/SharpCompress/Providers/CompressionProviderRegistry.cs b/src/SharpCompress/Providers/CompressionProviderRegistry.cs index 27de3181..c061dc26 100644 --- a/src/SharpCompress/Providers/CompressionProviderRegistry.cs +++ b/src/SharpCompress/Providers/CompressionProviderRegistry.cs @@ -275,10 +275,7 @@ public sealed class CompressionProviderRegistry /// If provider is null. public CompressionProviderRegistry With(ICompressionProvider provider) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ThrowHelper.ThrowIfNull(provider); var newProviders = new Dictionary(_providers) { diff --git a/src/SharpCompress/Readers/AbstractReader.Async.cs b/src/SharpCompress/Readers/AbstractReader.Async.cs index 0a507a34..3c3e01df 100644 --- a/src/SharpCompress/Readers/AbstractReader.Async.cs +++ b/src/SharpCompress/Readers/AbstractReader.Async.cs @@ -126,10 +126,7 @@ public abstract partial class AbstractReader ); } - if (writableStream is null) - { - throw new ArgumentNullException(nameof(writableStream)); - } + ThrowHelper.ThrowIfNull(writableStream); if (!writableStream.CanWrite) { throw new ArgumentException( diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index 5dd14381..73a282d4 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -190,10 +190,7 @@ public abstract partial class AbstractReader : IReader, IAsyncR throw new ArgumentException("WriteEntryTo or OpenEntryStream can only be called once."); } - if (writableStream is null) - { - throw new ArgumentNullException(nameof(writableStream)); - } + ThrowHelper.ThrowIfNull(writableStream); if (!writableStream.CanWrite) { throw new ArgumentException( diff --git a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs index a9e3e876..d3adddd0 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs @@ -15,12 +15,12 @@ public partial class AceReader /// Opens an AceReader for non-seeking usage with a single volume. /// /// The stream containing the ACE archive. - /// Reader options. + /// Reader options. /// An AceReader instance. - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new SingleVolumeAceReader(stream, options ?? new ReaderOptions()); + return new SingleVolumeAceReader(stream, readerOptions ?? new ReaderOptions()); } /// diff --git a/src/SharpCompress/Readers/Arc/ArcReader.cs b/src/SharpCompress/Readers/Arc/ArcReader.cs index d641b554..376d598e 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.cs @@ -20,12 +20,12 @@ public partial class ArcReader : AbstractReader /// Opens an ArcReader for Non-seeking usage with a single volume /// /// - /// + /// /// - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new ArcReader(stream, options ?? new ReaderOptions()); + return new ArcReader(stream, readerOptions ?? new ReaderOptions()); } protected override IEnumerable GetEntries(Stream stream) diff --git a/src/SharpCompress/Readers/Arj/ArjReader.cs b/src/SharpCompress/Readers/Arj/ArjReader.cs index 857a5ec9..d5a0ae74 100644 --- a/src/SharpCompress/Readers/Arj/ArjReader.cs +++ b/src/SharpCompress/Readers/Arj/ArjReader.cs @@ -27,12 +27,12 @@ public abstract partial class ArjReader : AbstractReader /// Opens an ArjReader for Non-seeking usage with a single volume /// /// - /// + /// /// - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new SingleVolumeArjReader(stream, options ?? new ReaderOptions()); + return new SingleVolumeArjReader(stream, readerOptions ?? new ReaderOptions()); } /// diff --git a/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs index 5f1542e9..2a05e323 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs @@ -52,9 +52,9 @@ public partial class GZipReader return OpenReader(fileInfo.OpenRead(), readerOptions); } - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new GZipReader(stream, options ?? new ReaderOptions()); + return new GZipReader(stream, readerOptions ?? new ReaderOptions()); } } diff --git a/src/SharpCompress/Readers/Lzw/LzwReader.Factory.cs b/src/SharpCompress/Readers/Lzw/LzwReader.Factory.cs index 008562c6..6e17e222 100644 --- a/src/SharpCompress/Readers/Lzw/LzwReader.Factory.cs +++ b/src/SharpCompress/Readers/Lzw/LzwReader.Factory.cs @@ -52,9 +52,9 @@ public partial class LzwReader return OpenReader(fileInfo.OpenRead(), readerOptions); } - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new LzwReader(stream, options ?? new ReaderOptions()); + return new LzwReader(stream, readerOptions ?? new ReaderOptions()); } } diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 741f7155..df006362 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -40,16 +40,16 @@ public abstract partial class RarReader : AbstractReader volume; - public static IReader OpenReader(string filePath, ReaderOptions? options = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return OpenReader(new FileInfo(filePath), options); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? options = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - options ??= new ReaderOptions { LeaveStreamOpen = false }; - return OpenReader(fileInfo.OpenRead(), options); + readerOptions ??= new ReaderOptions { LeaveStreamOpen = false }; + return OpenReader(fileInfo.OpenRead(), readerOptions); } public static IReader OpenReader(IEnumerable filePaths, ReaderOptions? options = null) @@ -67,12 +67,12 @@ public abstract partial class RarReader : AbstractReader /// - /// + /// /// - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new SingleVolumeRarReader(stream, options ?? new ReaderOptions()); + return new SingleVolumeRarReader(stream, readerOptions ?? new ReaderOptions()); } /// diff --git a/src/SharpCompress/Readers/Tar/TarReader.Factory.cs b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs index f537ecab..2bb9d849 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.Factory.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs @@ -82,16 +82,16 @@ public partial class TarReader public static async ValueTask OpenAsyncReader( Stream stream, - ReaderOptions? options = null, + ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); stream.NotNull(nameof(stream)); - options ??= new ReaderOptions(); + readerOptions ??= new ReaderOptions(); var sharpCompressStream = SharpCompressStream.Create( stream, - bufferSize: options.RewindableBufferSize + bufferSize: readerOptions.RewindableBufferSize ); long pos = sharpCompressStream.Position; foreach (var wrapper in TarWrapper.Wrappers) @@ -110,8 +110,8 @@ public partial class TarReader var testStream = await CreateProbeDecompressionStreamAsync( sharpCompressStream, wrapper.CompressionType, - options.Providers, - options, + readerOptions.Providers, + readerOptions, cancellationToken ) .ConfigureAwait(false); @@ -120,7 +120,7 @@ public partial class TarReader ) { sharpCompressStream.Position = pos; - return new TarReader(sharpCompressStream, options, wrapper.CompressionType); + return new TarReader(sharpCompressStream, readerOptions, wrapper.CompressionType); } if (wrapper.CompressionType != CompressionType.None) @@ -130,7 +130,7 @@ public partial class TarReader } sharpCompressStream.Position = pos; - return new TarReader(sharpCompressStream, options, CompressionType.None); + return new TarReader(sharpCompressStream, readerOptions, CompressionType.None); } public static ValueTask OpenAsyncReader( @@ -160,15 +160,15 @@ public partial class TarReader /// Opens a TarReader for Non-seeking usage with a single volume /// /// - /// + /// /// - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - options ??= new ReaderOptions(); + readerOptions ??= new ReaderOptions(); var sharpCompressStream = SharpCompressStream.Create( stream, - bufferSize: options.RewindableBufferSize + bufferSize: readerOptions.RewindableBufferSize ); long pos = sharpCompressStream.Position; foreach (var wrapper in TarWrapper.Wrappers) @@ -183,13 +183,13 @@ public partial class TarReader var testStream = CreateProbeDecompressionStream( sharpCompressStream, wrapper.CompressionType, - options.Providers, - options + readerOptions.Providers, + readerOptions ); if (TarArchive.IsTarFile(testStream)) { sharpCompressStream.Position = pos; - return new TarReader(sharpCompressStream, options, wrapper.CompressionType); + return new TarReader(sharpCompressStream, readerOptions, wrapper.CompressionType); } if (wrapper.CompressionType != CompressionType.None) @@ -199,6 +199,6 @@ public partial class TarReader } sharpCompressStream.Position = pos; - return new TarReader(sharpCompressStream, options, CompressionType.None); + return new TarReader(sharpCompressStream, readerOptions, CompressionType.None); } } diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index ac1b4419..91dff5bf 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -43,12 +43,12 @@ public partial class ZipReader : AbstractReader /// Opens a ZipReader for Non-seeking usage with a single volume /// /// - /// + /// /// - public static IReader OpenReader(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); - return new ZipReader(stream, options ?? new ReaderOptions()); + return new ZipReader(stream, readerOptions ?? new ReaderOptions()); } public static IReader OpenReader( diff --git a/src/SharpCompress/ThrowHelper.cs b/src/SharpCompress/ThrowHelper.cs new file mode 100644 index 00000000..9d598107 --- /dev/null +++ b/src/SharpCompress/ThrowHelper.cs @@ -0,0 +1,71 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace SharpCompress; + +internal static class ThrowHelper +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNull([NotNull] object? argument, string? paramName = null) + { + if (argument is null) + { + throw new ArgumentNullException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegative(int value, string? paramName = null) + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegative(long value, string? paramName = null) + { + if (value < 0) + { + throw new ArgumentOutOfRangeException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfNegativeOrZero(int value, string? paramName = null) + { + if (value <= 0) + { + throw new ArgumentOutOfRangeException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfLessThan(int value, int other, string? paramName = null) + { + if (value < other) + { + throw new ArgumentOutOfRangeException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfGreaterThan(int value, int other, string? paramName = null) + { + if (value > other) + { + throw new ArgumentOutOfRangeException(paramName); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfGreaterThan(uint value, uint other, string? paramName = null) + { + if (value > other) + { + throw new ArgumentOutOfRangeException(paramName); + } + } +} diff --git a/src/SharpCompress/Utility.Async.cs b/src/SharpCompress/Utility.Async.cs index f44ad40a..79df1f9c 100644 --- a/src/SharpCompress/Utility.Async.cs +++ b/src/SharpCompress/Utility.Async.cs @@ -26,13 +26,10 @@ internal static partial class Utility throw new ArgumentNullException(); } #else - ArgumentNullException.ThrowIfNull(source); + ThrowHelper.ThrowIfNull(source); #endif - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || offset > buffer.Length) { diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index 8c46be3a..cdc78549 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -253,13 +253,10 @@ internal static partial class Utility throw new ArgumentNullException(); } #else - ArgumentNullException.ThrowIfNull(source); + ThrowHelper.ThrowIfNull(source); #endif - if (buffer is null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ThrowHelper.ThrowIfNull(buffer); if (offset < 0 || offset > buffer.Length) { diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs index 715bdc22..01e6edb5 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs @@ -24,9 +24,9 @@ public partial class GZipWriter : IWriterOpenable return new GZipWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsyncWriter(string path, GZipWriterOptions writerOptions) + public static IAsyncWriter OpenAsyncWriter(string stream, GZipWriterOptions writerOptions) { - return (IAsyncWriter)OpenWriter(path, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } public static IAsyncWriter OpenAsyncWriter(Stream stream, GZipWriterOptions writerOptions) diff --git a/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs index d7077478..a3c6bf6f 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs @@ -24,9 +24,9 @@ public partial class TarWriter : IWriterOpenable return new TarWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsyncWriter(string path, TarWriterOptions writerOptions) + public static IAsyncWriter OpenAsyncWriter(string stream, TarWriterOptions writerOptions) { - return (IAsyncWriter)OpenWriter(path, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } public static IAsyncWriter OpenAsyncWriter(Stream stream, TarWriterOptions writerOptions) diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs index 53c1af45..8c53f8fa 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs @@ -14,7 +14,7 @@ public partial class ZipWriter /// Asynchronously writes an entry to the ZIP archive. /// public override async ValueTask WriteAsync( - string entryPath, + string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default @@ -22,7 +22,7 @@ public partial class ZipWriter { cancellationToken.ThrowIfCancellationRequested(); await WriteAsync( - entryPath, + filename, source, new ZipWriterEntryOptions { ModificationDateTime = modificationTime }, cancellationToken diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs index c4083aea..d21ab7e2 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs @@ -24,9 +24,9 @@ public partial class ZipWriter : IWriterOpenable return new ZipWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsyncWriter(string path, ZipWriterOptions writerOptions) + public static IAsyncWriter OpenAsyncWriter(string stream, ZipWriterOptions writerOptions) { - return (IAsyncWriter)OpenWriter(path, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } public static IAsyncWriter OpenAsyncWriter(Stream stream, ZipWriterOptions writerOptions) diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 76d46ee9..a05e8c34 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -78,9 +78,9 @@ public partial class ZipWriter : AbstractWriter _ => throw new InvalidFormatException("Invalid compression method: " + compressionType), }; - public override void Write(string entryPath, Stream source, DateTime? modificationTime) => + public override void Write(string filename, Stream source, DateTime? modificationTime) => Write( - entryPath, + filename, source, new ZipWriterEntryOptions() { ModificationDateTime = modificationTime } ); diff --git a/tests/SharpCompress.Performance/LargeMemoryStream.cs b/tests/SharpCompress.Performance/LargeMemoryStream.cs index 428c33b0..f8922f9f 100644 --- a/tests/SharpCompress.Performance/LargeMemoryStream.cs +++ b/tests/SharpCompress.Performance/LargeMemoryStream.cs @@ -85,10 +85,7 @@ public class LargeMemoryStream : Stream public override int Read(byte[] buffer, int offset, int count) { ThrowIfDisposed(); - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ArgumentNullException.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { @@ -130,10 +127,7 @@ public class LargeMemoryStream : Stream public override void Write(byte[] buffer, int offset, int count) { ThrowIfDisposed(); - if (buffer == null) - { - throw new ArgumentNullException(nameof(buffer)); - } + ArgumentNullException.ThrowIfNull(buffer); if (offset < 0 || count < 0 || offset + count > buffer.Length) { @@ -292,10 +286,7 @@ public class LargeMemoryStream : Stream private void ThrowIfDisposed() { - if (_isDisposed) - { - throw new ObjectDisposedException(GetType().Name); - } + ObjectDisposedException.ThrowIf(_isDisposed, this); } protected override void Dispose(bool disposing) diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index 4b66c0d9..2e441735 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -70,7 +70,12 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.OpenAsyncWriter(stream, _type, writerOptions); + using var writer = WriterFactory.OpenAsyncWriter( + stream, + _type, + writerOptions, + cancellationToken + ); await writer.WriteAllAsync( ORIGINAL_FILES_PATH, "*",