From be4b415487075d9ec566246d843b6f37c234a0de Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 21 Oct 2024 19:14:25 +0200 Subject: [PATCH] Make internal & private classes sealed when possible. Avoid double lookup in dictionary --- .../Core/StringBuilderPool.cs | 2 +- .../Encryption/PkzipClassic.cs | 4 ++-- .../Encryption/ZipAESStream.cs | 2 +- .../Encryption/ZipAESTransform.cs | 2 +- .../Zip/Compression/DeflaterHuffman.cs | 2 +- .../Zip/Compression/InflaterDynHeader.cs | 2 +- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 18 +++++++++--------- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs b/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs index a1121f0cc..10d1e32bc 100644 --- a/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs +++ b/src/ICSharpCode.SharpZipLib/Core/StringBuilderPool.cs @@ -3,7 +3,7 @@ namespace ICSharpCode.SharpZipLib.Core { - internal class StringBuilderPool + internal sealed class StringBuilderPool { public static StringBuilderPool Instance { get; } = new StringBuilderPool(); private readonly ConcurrentQueue pool = new ConcurrentQueue(); diff --git a/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs b/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs index 1c7bd1f28..c9b12b936 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs @@ -130,7 +130,7 @@ protected void Reset() /// /// PkzipClassic CryptoTransform for encryption. /// - internal class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform + internal sealed class PkzipClassicEncryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform { /// /// Initialise a new instance of @@ -240,7 +240,7 @@ public void Dispose() /// /// PkzipClassic CryptoTransform for decryption. /// - internal class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform + internal sealed class PkzipClassicDecryptCryptoTransform : PkzipClassicCryptoBase, ICryptoTransform { /// /// Initialise a new instance of . diff --git a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs index 346b5484b..a4ee8367a 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESStream.cs @@ -15,7 +15,7 @@ namespace ICSharpCode.SharpZipLib.Encryption /// Based on information from http://www.winzip.com/aes_info.htm /// and http://www.gladman.me.uk/cryptography_technology/fileencrypt/ /// - internal class ZipAESStream : CryptoStream + internal sealed class ZipAESStream : CryptoStream { /// /// Constructor diff --git a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs index 32c7b8156..1341965fb 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs @@ -6,7 +6,7 @@ namespace ICSharpCode.SharpZipLib.Encryption /// /// Transforms stream using AES in CTR mode /// - internal class ZipAESTransform : ICryptoTransform + internal sealed class ZipAESTransform : ICryptoTransform { private const int PWD_VER_LENGTH = 2; diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs index 2f71366fc..ea35ad48d 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterHuffman.cs @@ -60,7 +60,7 @@ public class DeflaterHuffman private static short[] staticDCodes; private static byte[] staticDLength; - private class Tree + private sealed class Tree { #region Instance Fields diff --git a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs index 8e0196b11..e7a14e349 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/Compression/InflaterDynHeader.cs @@ -4,7 +4,7 @@ namespace ICSharpCode.SharpZipLib.Zip.Compression { - internal class InflaterDynHeader + internal sealed class InflaterDynHeader { #region Constants diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index 7fc1c5592..a08f84b06 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -2679,9 +2679,9 @@ private void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc private int FindExistingUpdate(ZipEntry entry) { int result = -1; - if (updateIndex_.ContainsKey(entry.Name)) + if (updateIndex_.TryGetValue(entry.Name, out int value)) { - result = (int)updateIndex_[entry.Name]; + result = value; } /* // This is slow like the coming of the next ice age but takes less storage and may be useful @@ -2705,9 +2705,9 @@ private int FindExistingUpdate(string fileName, bool isEntryName = false) string convertedName = !isEntryName ? GetTransformedFileName(fileName) : fileName; - if (updateIndex_.ContainsKey(convertedName)) + if (updateIndex_.TryGetValue(convertedName, out int value)) { - result = (int)updateIndex_[convertedName]; + result = value; } /* @@ -3030,7 +3030,7 @@ private void UpdateCommentOnly() /// /// Class used to sort updates. /// - private class UpdateComparer : IComparer + private sealed class UpdateComparer : IComparer { /// /// Compares two objects and returns a value indicating whether one is @@ -3252,7 +3252,7 @@ private void CheckUpdating() /// /// Represents a pending update to a Zip file. /// - private class ZipUpdate + private sealed class ZipUpdate { #region Constructors @@ -3911,7 +3911,7 @@ private static void WriteEncryptionHeader(Stream stream, long crcValue) /// /// Represents a string from a which is stored as an array of bytes. /// - private class ZipString + private sealed class ZipString { #region Constructors @@ -4086,7 +4086,7 @@ public void Dispose() /// An is a stream that you can write uncompressed data /// to and flush, but cannot read, seek or do anything else to. /// - private class UncompressedStream : Stream + private sealed class UncompressedStream : Stream { #region Constructors @@ -4241,7 +4241,7 @@ private readonly /// A is an /// whose data is only a part or subsection of a file. /// - private class PartialInputStream : Stream + private sealed class PartialInputStream : Stream { #region Constructors