From 9022ac311ae0de30b03467c1bf84cfff80a1c7f2 Mon Sep 17 00:00:00 2001 From: HowToDoThis Date: Thu, 12 Aug 2021 02:16:29 +0800 Subject: [PATCH] PR #575 --- src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs | 18 ++++++++++++++++++ .../Encryption/ZipAESTransform.cs | 10 +++++----- src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs | 4 +++- .../Zip/ZipExtraData.cs | 6 ++++-- src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs | 8 ++++---- .../Zip/ZipOutputStream.cs | 4 ++-- src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs | 8 +++++--- 7 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs diff --git a/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs b/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs new file mode 100644 index 000000000..e4dd4698b --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs @@ -0,0 +1,18 @@ +using System; + +namespace ICSharpCode.SharpZipLib.Core +{ + internal static class Empty + { +#if NET45 + internal static class EmptyArray + { + public static readonly T[] Value = new T[0]; + } + + public static T[] Array() => EmptyArray.Value; +#else + public static T[] Array() => System.Array.Empty(); +#endif + } +} diff --git a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs index 0bacefed9..68c182ac2 100644 --- a/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs +++ b/src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs @@ -1,3 +1,5 @@ +using ICSharpCode.SharpZipLib.Core; + using System; using System.Security.Cryptography; @@ -159,11 +161,9 @@ public byte[] GetAuthCode() /// public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { - if (inputCount > 0) - { - throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0"); - } - return Array.Empty(); + return inputCount > 0 + ? throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0") + : Empty.Array(); } /// diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs index 09ae53703..9d087f1cd 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs @@ -1,3 +1,5 @@ +using ICSharpCode.SharpZipLib.Core; + using System; using System.IO; using System.Text; @@ -399,7 +401,7 @@ public void GetFileTarHeader(TarHeader header, string file) public TarEntry[] GetDirectoryEntries() { if ((file == null) || !Directory.Exists(file)) - return Array.Empty(); + return Empty.Array(); string[] list = Directory.GetFileSystemEntries(file); TarEntry[] result = new TarEntry[list.Length]; diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs index 8c5695e90..b5747de2c 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs @@ -1,3 +1,5 @@ +using ICSharpCode.SharpZipLib.Core; + using System; using System.IO; @@ -517,7 +519,7 @@ public ZipExtraData(byte[] data) { if (data == null) { - _data = Array.Empty(); + _data = Empty.Array(); } else { @@ -548,7 +550,7 @@ public void Clear() { if ((_data == null) || (_data.Length != 0)) { - _data = Array.Empty(); + _data = Empty.Array(); } } diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs index d64680935..79197a8da 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs @@ -530,7 +530,7 @@ public ZipFile(Stream stream, bool leaveOpen) } else { - entries_ = Array.Empty(); + entries_ = Empty.Array(); isNewArchive_ = true; } } @@ -540,7 +540,7 @@ public ZipFile(Stream stream, bool leaveOpen) /// internal ZipFile() { - entries_ = Array.Empty(); + entries_ = Empty.Array(); isNewArchive_ = true; } @@ -2325,7 +2325,7 @@ private int WriteCentralDirectoryHeader(ZipEntry entry) baseStream_.Write(centralExtraData, 0, centralExtraData.Length); } - byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : Array.Empty(); + byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : Empty.Array(); if (rawComment.Length > 0) { @@ -3232,7 +3232,7 @@ private void DisposeInternal(bool disposing) if (!isDisposed_) { isDisposed_ = true; - entries_ = Array.Empty(); + entries_ = Empty.Array(); if (IsStreamOwner && (baseStream_ != null)) { diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs index 49db587d5..672f0ea2b 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs @@ -861,7 +861,7 @@ public override void Finish() byte[] entryComment = (entry.Comment != null) ? ZipStrings.ConvertToArray(entry.Flags, entry.Comment) : - Array.Empty(); + Empty.Array(); if (entryComment.Length > 0xffff) { @@ -976,7 +976,7 @@ public override void Flush() /// /// Comment for the entire archive recorded in central header. /// - private byte[] zipComment = Array.Empty(); + private byte[] zipComment = Empty.Array(); /// /// Flag indicating that header patching is required for the current entry. diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs index b966ed673..903e0e557 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs @@ -1,4 +1,6 @@ -using System; +using ICSharpCode.SharpZipLib.Core; + +using System; using System.Text; namespace ICSharpCode.SharpZipLib.Zip @@ -174,7 +176,7 @@ public static string ConvertToStringExt(int flags, byte[] data) /// Converted array public static byte[] ConvertToArray(string str) => str == null - ? Array.Empty() + ? Empty.Array() : Encoding.GetEncoding(CodePage).GetBytes(str); /// @@ -187,7 +189,7 @@ public static byte[] ConvertToArray(string str) /// Converted array public static byte[] ConvertToArray(int flags, string str) => (string.IsNullOrEmpty(str)) - ? Array.Empty() + ? Empty.Array() : EncodingFromFlag(flags).GetBytes(str); } }