From 084fe72b029fddbe034f194fb71325cd240da088 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 22 Oct 2025 09:17:13 +0100 Subject: [PATCH] Consolidate not null --- src/SharpCompress/Archives/ArchiveFactory.cs | 22 +++++++++---------- .../Archives/GZip/GZipArchive.cs | 10 ++++----- src/SharpCompress/Archives/Rar/RarArchive.cs | 10 ++++----- .../Archives/SevenZip/SevenZipArchive.cs | 10 ++++----- src/SharpCompress/Archives/Tar/TarArchive.cs | 10 ++++----- src/SharpCompress/Archives/Zip/ZipArchive.cs | 10 ++++----- src/SharpCompress/Factories/Factory.cs | 2 +- src/SharpCompress/LazyReadOnlyCollection.cs | 2 +- src/SharpCompress/NotNullExtensions.cs | 19 +++++++++++++--- src/SharpCompress/Readers/Arc/ArcReader.cs | 2 +- src/SharpCompress/Readers/GZip/GZipReader.cs | 2 +- src/SharpCompress/Readers/Rar/RarReader.cs | 6 ++--- src/SharpCompress/Readers/ReaderFactory.cs | 4 ++-- src/SharpCompress/Readers/Tar/TarReader.cs | 2 +- src/SharpCompress/Readers/Zip/ZipReader.cs | 4 ++-- src/SharpCompress/Utility.cs | 20 +---------------- .../SharpCompress.Test/Rar/RarReaderTests.cs | 2 +- .../SharpCompress.Test/Tar/TarReaderTests.cs | 2 +- tests/SharpCompress.Test/TestBase.cs | 1 - 19 files changed, 67 insertions(+), 73 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index c979b700..870092f1 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -45,7 +45,7 @@ public static class ArchiveFactory /// public static IArchive Open(string filePath, ReaderOptions? options = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), options); } @@ -68,7 +68,7 @@ public static class ArchiveFactory /// public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) { - fileInfos.CheckNotNull(nameof(fileInfos)); + fileInfos.NotNull(nameof(fileInfos)); var filesArray = fileInfos.ToArray(); if (filesArray.Length == 0) { @@ -81,7 +81,7 @@ public static class ArchiveFactory return Open(fileInfo, options); } - fileInfo.CheckNotNull(nameof(fileInfo)); + fileInfo.NotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; return FindFactory(fileInfo).Open(filesArray, options); @@ -94,7 +94,7 @@ public static class ArchiveFactory /// public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); var streamsArray = streams.ToArray(); if (streamsArray.Length == 0) { @@ -107,7 +107,7 @@ public static class ArchiveFactory return Open(firstStream, options); } - firstStream.CheckNotNull(nameof(firstStream)); + firstStream.NotNull(nameof(firstStream)); options ??= new ReaderOptions(); return FindFactory(firstStream).Open(streamsArray, options); @@ -129,7 +129,7 @@ public static class ArchiveFactory private static T FindFactory(FileInfo finfo) where T : IFactory { - finfo.CheckNotNull(nameof(finfo)); + finfo.NotNull(nameof(finfo)); using Stream stream = finfo.OpenRead(); return FindFactory(stream); } @@ -137,7 +137,7 @@ public static class ArchiveFactory private static T FindFactory(Stream stream) where T : IFactory { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (!stream.CanRead || !stream.CanSeek) { throw new ArgumentException("Stream should be readable and seekable"); @@ -172,7 +172,7 @@ public static class ArchiveFactory int bufferSize = ReaderOptions.DefaultBufferSize ) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); using Stream s = File.OpenRead(filePath); return IsArchive(s, out type, bufferSize); } @@ -184,7 +184,7 @@ public static class ArchiveFactory ) { type = null; - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (!stream.CanRead || !stream.CanSeek) { @@ -215,7 +215,7 @@ public static class ArchiveFactory /// public static IEnumerable GetFileParts(string part1) { - part1.CheckNotNullOrEmpty(nameof(part1)); + part1.NotNullOrEmpty(nameof(part1)); return GetFileParts(new FileInfo(part1)).Select(a => a.FullName); } @@ -226,7 +226,7 @@ public static class ArchiveFactory /// public static IEnumerable GetFileParts(FileInfo part1) { - part1.CheckNotNull(nameof(part1)); + part1.NotNull(nameof(part1)); yield return part1; foreach (var factory in Factory.Factories.OfType()) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 4437ff57..a0345cdf 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -21,7 +21,7 @@ public class GZipArchive : AbstractWritableArchive /// public static GZipArchive Open(string filePath, ReaderOptions? readerOptions = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } @@ -32,7 +32,7 @@ public class GZipArchive : AbstractWritableArchive /// public static GZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - fileInfo.CheckNotNull(nameof(fileInfo)); + fileInfo.NotNull(nameof(fileInfo)); return new GZipArchive( new SourceStream( fileInfo, @@ -52,7 +52,7 @@ public class GZipArchive : AbstractWritableArchive ReaderOptions? readerOptions = null ) { - fileInfos.CheckNotNull(nameof(fileInfos)); + fileInfos.NotNull(nameof(fileInfos)); var files = fileInfos.ToArray(); return new GZipArchive( new SourceStream( @@ -70,7 +70,7 @@ public class GZipArchive : AbstractWritableArchive /// public static GZipArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); var strms = streams.ToArray(); return new GZipArchive( new SourceStream( @@ -88,7 +88,7 @@ public class GZipArchive : AbstractWritableArchive /// public static GZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (stream is not { CanSeek: true }) { diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index ee4be60e..b3689a9f 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -95,7 +95,7 @@ public class RarArchive : AbstractArchive /// public static RarArchive Open(string filePath, ReaderOptions? options = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); var fileInfo = new FileInfo(filePath); return new RarArchive( new SourceStream( @@ -113,7 +113,7 @@ public class RarArchive : AbstractArchive /// public static RarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) { - fileInfo.CheckNotNull(nameof(fileInfo)); + fileInfo.NotNull(nameof(fileInfo)); return new RarArchive( new SourceStream( fileInfo, @@ -130,7 +130,7 @@ public class RarArchive : AbstractArchive /// public static RarArchive Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (stream is not { CanSeek: true }) { @@ -150,7 +150,7 @@ public class RarArchive : AbstractArchive ReaderOptions? readerOptions = null ) { - fileInfos.CheckNotNull(nameof(fileInfos)); + fileInfos.NotNull(nameof(fileInfos)); var files = fileInfos.ToArray(); return new RarArchive( new SourceStream( @@ -168,7 +168,7 @@ public class RarArchive : AbstractArchive /// public static RarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); var strms = streams.ToArray(); return new RarArchive( new SourceStream( diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 323f07ac..ea763409 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -21,7 +21,7 @@ public class SevenZipArchive : AbstractArchive public static SevenZipArchive Open(string filePath, ReaderOptions? readerOptions = null) { - filePath.CheckNotNullOrEmpty("filePath"); + filePath.NotNullOrEmpty("filePath"); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } @@ -32,7 +32,7 @@ public class SevenZipArchive : AbstractArchive public static SevenZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - fileInfo.CheckNotNull("fileInfo"); + fileInfo.NotNull("fileInfo"); return new SevenZipArchive( new SourceStream( fileInfo, @@ -52,7 +52,7 @@ public class SevenZipArchive : AbstractArchive public static SevenZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) { - stream.CheckNotNull("stream"); + stream.NotNull("stream"); if (stream is not { CanSeek: true }) { diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index b2a34d54..39f0fce6 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -22,7 +22,7 @@ public class TarArchive : AbstractWritableArchive /// public static TarArchive Open(string filePath, ReaderOptions? readerOptions = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } @@ -33,7 +33,7 @@ public class TarArchive : AbstractWritableArchive /// public static TarArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - fileInfo.CheckNotNull(nameof(fileInfo)); + fileInfo.NotNull(nameof(fileInfo)); return new TarArchive( new SourceStream( fileInfo, @@ -53,7 +53,7 @@ public class TarArchive : AbstractWritableArchive ReaderOptions? readerOptions = null ) { - fileInfos.CheckNotNull(nameof(fileInfos)); + fileInfos.NotNull(nameof(fileInfos)); var files = fileInfos.ToArray(); return new TarArchive( new SourceStream( @@ -71,7 +71,7 @@ public class TarArchive : AbstractWritableArchive /// public static TarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); var strms = streams.ToArray(); return new TarArchive( new SourceStream( @@ -89,7 +89,7 @@ public class TarArchive : AbstractWritableArchive /// public static TarArchive Open(Stream stream, ReaderOptions? readerOptions = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (stream is not { CanSeek: true }) { diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index a75b4095..f599d400 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -43,7 +43,7 @@ public class ZipArchive : AbstractWritableArchive /// public static ZipArchive Open(string filePath, ReaderOptions? readerOptions = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNull(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } @@ -54,7 +54,7 @@ public class ZipArchive : AbstractWritableArchive /// public static ZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { - fileInfo.CheckNotNull(nameof(fileInfo)); + fileInfo.NotNull(nameof(fileInfo)); return new ZipArchive( new SourceStream( fileInfo, @@ -74,7 +74,7 @@ public class ZipArchive : AbstractWritableArchive ReaderOptions? readerOptions = null ) { - fileInfos.CheckNotNull(nameof(fileInfos)); + fileInfos.NotNull(nameof(fileInfos)); var files = fileInfos.ToArray(); return new ZipArchive( new SourceStream( @@ -92,7 +92,7 @@ public class ZipArchive : AbstractWritableArchive /// public static ZipArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); var strms = streams.ToArray(); return new ZipArchive( new SourceStream( @@ -110,7 +110,7 @@ public class ZipArchive : AbstractWritableArchive /// public static ZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); if (stream is not { CanSeek: true }) { diff --git a/src/SharpCompress/Factories/Factory.cs b/src/SharpCompress/Factories/Factory.cs index 1b9e23e9..2462f614 100644 --- a/src/SharpCompress/Factories/Factory.cs +++ b/src/SharpCompress/Factories/Factory.cs @@ -34,7 +34,7 @@ public abstract class Factory : IFactory /// must not be null. public static void RegisterFactory(Factory factory) { - factory.CheckNotNull(nameof(factory)); + factory.NotNull(nameof(factory)); _factories.Add(factory); } diff --git a/src/SharpCompress/LazyReadOnlyCollection.cs b/src/SharpCompress/LazyReadOnlyCollection.cs index 9c1b35eb..cc9cb3fd 100644 --- a/src/SharpCompress/LazyReadOnlyCollection.cs +++ b/src/SharpCompress/LazyReadOnlyCollection.cs @@ -4,7 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; -namespace SharpCompress.Helpers; +namespace SharpCompress; internal sealed class LazyReadOnlyCollection : ICollection { diff --git a/src/SharpCompress/NotNullExtensions.cs b/src/SharpCompress/NotNullExtensions.cs index 2245612e..d0e43284 100644 --- a/src/SharpCompress/NotNullExtensions.cs +++ b/src/SharpCompress/NotNullExtensions.cs @@ -4,20 +4,20 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; -namespace SharpCompress.Helpers; +namespace SharpCompress; internal static class NotNullExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this IEnumerable? source) => - source ?? Enumerable.Empty(); + source ?? []; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Empty(this T? source) { if (source is null) { - return Enumerable.Empty(); + return []; } return source.AsEnumerable(); } @@ -68,4 +68,17 @@ internal static class NotNullExtensions return obj.Value; } #endif + + + + public static string NotNullOrEmpty(this string obj, string name) + { + obj.NotNull(name); + if (obj.Length == 0) + { + throw new ArgumentException("String is empty.", name); + } + return obj; + } + } diff --git a/src/SharpCompress/Readers/Arc/ArcReader.cs b/src/SharpCompress/Readers/Arc/ArcReader.cs index 7d58b8d4..b7cf5467 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.cs @@ -24,7 +24,7 @@ namespace SharpCompress.Readers.Arc /// public static ArcReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); return new ArcReader(stream, options ?? new ReaderOptions()); } diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index 73bc4a9d..e10d509a 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -22,7 +22,7 @@ public class GZipReader : AbstractReader /// public static GZipReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); return new GZipReader(stream, options ?? new ReaderOptions()); } diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 2a56ee35..79842e6a 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -42,7 +42,7 @@ public abstract class RarReader : AbstractReader public static RarReader Open(string filePath, ReaderOptions? options = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), options); } @@ -71,7 +71,7 @@ public abstract class RarReader : AbstractReader /// public static RarReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); return new SingleVolumeRarReader(stream, options ?? new ReaderOptions()); } @@ -83,7 +83,7 @@ public abstract class RarReader : AbstractReader /// public static RarReader Open(IEnumerable streams, ReaderOptions? options = null) { - streams.CheckNotNull(nameof(streams)); + streams.NotNull(nameof(streams)); return new MultiVolumeRarReader(streams, options ?? new ReaderOptions()); } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 9830c8fb..97846380 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -11,7 +11,7 @@ public static class ReaderFactory { public static IReader Open(string filePath, ReaderOptions? options = null) { - filePath.CheckNotNullOrEmpty(nameof(filePath)); + filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), options); } @@ -29,7 +29,7 @@ public static class ReaderFactory /// public static IReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); options ??= new ReaderOptions() { LeaveStreamOpen = false }; var bStream = new SharpCompressStream(stream, bufferSize: options.BufferSize); diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index aaa0005c..c9218885 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -55,7 +55,7 @@ public class TarReader : AbstractReader /// public static TarReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); options = options ?? new ReaderOptions(); var rewindableStream = new SharpCompressStream(stream); diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index cda3f3f8..5e82479c 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -44,7 +44,7 @@ public class ZipReader : AbstractReader /// public static ZipReader Open(Stream stream, ReaderOptions? options = null) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions()); } @@ -54,7 +54,7 @@ public class ZipReader : AbstractReader IEnumerable entries ) { - stream.CheckNotNull(nameof(stream)); + stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions(), entries); } diff --git a/src/SharpCompress/Utility.cs b/src/SharpCompress/Utility.cs index cb3d2d97..b22d8005 100644 --- a/src/SharpCompress/Utility.cs +++ b/src/SharpCompress/Utility.cs @@ -1,4 +1,3 @@ -global using SharpCompress.Helpers; using System; using System.Buffers; using System.Collections.Generic; @@ -7,7 +6,7 @@ using System.IO; using System.Text; using SharpCompress.Readers; -namespace SharpCompress.Helpers; +namespace SharpCompress; internal static class Utility { @@ -75,23 +74,6 @@ internal static class Utility yield return item; } - public static void CheckNotNull(this object obj, string name) - { - if (obj is null) - { - throw new ArgumentNullException(name); - } - } - - public static void CheckNotNullOrEmpty(this string obj, string name) - { - obj.CheckNotNull(name); - if (obj.Length == 0) - { - throw new ArgumentException("String is empty.", name); - } - } - public static void Skip(this Stream source, long advanceAmount) { if (source.CanSeek) diff --git a/tests/SharpCompress.Test/Rar/RarReaderTests.cs b/tests/SharpCompress.Test/Rar/RarReaderTests.cs index d7c02d2d..64d69d54 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderTests.cs @@ -223,7 +223,7 @@ public class RarReaderTests : ReaderTests var destinationFileName = Path.Combine(destdir, file); using var fs = File.OpenWrite(destinationFileName); - entryStream.TransferTo(fs); + entryStream.CopyTo(fs); } } } diff --git a/tests/SharpCompress.Test/Tar/TarReaderTests.cs b/tests/SharpCompress.Test/Tar/TarReaderTests.cs index a676d2da..88c30cfa 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderTests.cs @@ -85,7 +85,7 @@ public class TarReaderTests : ReaderTests var destinationFileName = Path.Combine(destdir, file.NotNull()); using var fs = File.OpenWrite(destinationFileName); - entryStream.TransferTo(fs); + entryStream.CopyTo(fs); } } } diff --git a/tests/SharpCompress.Test/TestBase.cs b/tests/SharpCompress.Test/TestBase.cs index f9cfb8a3..184e6442 100644 --- a/tests/SharpCompress.Test/TestBase.cs +++ b/tests/SharpCompress.Test/TestBase.cs @@ -1,4 +1,3 @@ -global using SharpCompress.Helpers; using System; using System.Collections.Generic; using System.IO;