Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HowToDoThis committed Aug 11, 2021
1 parent f40e24e commit 9022ac3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
18 changes: 18 additions & 0 deletions 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<T>
{
public static readonly T[] Value = new T[0];
}

public static T[] Array<T>() => EmptyArray<T>.Value;
#else
public static T[] Array<T>() => System.Array.Empty<T>();
#endif
}
}
10 changes: 5 additions & 5 deletions src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs
@@ -1,3 +1,5 @@
using ICSharpCode.SharpZipLib.Core;

using System;
using System.Security.Cryptography;

Expand Down Expand Up @@ -159,11 +161,9 @@ public byte[] GetAuthCode()
/// </summary>
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<byte>();
return inputCount > 0
? throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0")
: Empty.Array<byte>();
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
@@ -1,3 +1,5 @@
using ICSharpCode.SharpZipLib.Core;

using System;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -399,7 +401,7 @@ public void GetFileTarHeader(TarHeader header, string file)
public TarEntry[] GetDirectoryEntries()
{
if ((file == null) || !Directory.Exists(file))
return Array.Empty<TarEntry>();
return Empty.Array<TarEntry>();

string[] list = Directory.GetFileSystemEntries(file);
TarEntry[] result = new TarEntry[list.Length];
Expand Down
6 changes: 4 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs
@@ -1,3 +1,5 @@
using ICSharpCode.SharpZipLib.Core;

using System;
using System.IO;

Expand Down Expand Up @@ -517,7 +519,7 @@ public ZipExtraData(byte[] data)
{
if (data == null)
{
_data = Array.Empty<byte>();
_data = Empty.Array<byte>();
}
else
{
Expand Down Expand Up @@ -548,7 +550,7 @@ public void Clear()
{
if ((_data == null) || (_data.Length != 0))
{
_data = Array.Empty<byte>();
_data = Empty.Array<byte>();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Expand Up @@ -530,7 +530,7 @@ public ZipFile(Stream stream, bool leaveOpen)
}
else
{
entries_ = Array.Empty<ZipEntry>();
entries_ = Empty.Array<ZipEntry>();
isNewArchive_ = true;
}
}
Expand All @@ -540,7 +540,7 @@ public ZipFile(Stream stream, bool leaveOpen)
/// </summary>
internal ZipFile()
{
entries_ = Array.Empty<ZipEntry>();
entries_ = Empty.Array<ZipEntry>();
isNewArchive_ = true;
}

Expand Down Expand Up @@ -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>();
byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : Empty.Array<byte>();

if (rawComment.Length > 0)
{
Expand Down Expand Up @@ -3232,7 +3232,7 @@ private void DisposeInternal(bool disposing)
if (!isDisposed_)
{
isDisposed_ = true;
entries_ = Array.Empty<ZipEntry>();
entries_ = Empty.Array<ZipEntry>();

if (IsStreamOwner && (baseStream_ != null))
{
Expand Down
4 changes: 2 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Expand Up @@ -861,7 +861,7 @@ public override void Finish()
byte[] entryComment =
(entry.Comment != null) ?
ZipStrings.ConvertToArray(entry.Flags, entry.Comment) :
Array.Empty<byte>();
Empty.Array<byte>();

if (entryComment.Length > 0xffff)
{
Expand Down Expand Up @@ -976,7 +976,7 @@ public override void Flush()
/// <summary>
/// Comment for the entire archive recorded in central header.
/// </summary>
private byte[] zipComment = Array.Empty<byte>();
private byte[] zipComment = Empty.Array<byte>();

/// <summary>
/// Flag indicating that header patching is required for the current entry.
Expand Down
8 changes: 5 additions & 3 deletions 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
Expand Down Expand Up @@ -174,7 +176,7 @@ public static string ConvertToStringExt(int flags, byte[] data)
/// <returns>Converted array</returns>
public static byte[] ConvertToArray(string str)
=> str == null
? Array.Empty<byte>()
? Empty.Array<byte>()
: Encoding.GetEncoding(CodePage).GetBytes(str);

/// <summary>
Expand All @@ -187,7 +189,7 @@ public static byte[] ConvertToArray(string str)
/// <returns>Converted array</returns>
public static byte[] ConvertToArray(int flags, string str)
=> (string.IsNullOrEmpty(str))
? Array.Empty<byte>()
? Empty.Array<byte>()
: EncodingFromFlag(flags).GetBytes(str);
}
}

0 comments on commit 9022ac3

Please sign in to comment.