Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uses of new T[0] with Array.Empty<T> #575

Merged
merged 2 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/ICSharpCode.SharpZipLib/Core/EmptyRefs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
}
}
3 changes: 2 additions & 1 deletion src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography;
using ICSharpCode.SharpZipLib.Core;

namespace ICSharpCode.SharpZipLib.Encryption
{
Expand Down Expand Up @@ -163,7 +164,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
{
throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0");
}
return new byte[0];
return Empty.Array<byte>();
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Core;

namespace ICSharpCode.SharpZipLib.Tar
{
Expand Down Expand Up @@ -465,7 +466,7 @@ public TarEntry[] GetDirectoryEntries()
{
if ((file == null) || !Directory.Exists(file))
{
return new TarEntry[0];
return Empty.Array<TarEntry>();
}

string[] list = Directory.GetFileSystemEntries(file);
Expand Down
5 changes: 3 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;

namespace ICSharpCode.SharpZipLib.Zip
{
Expand Down Expand Up @@ -521,7 +522,7 @@ public ZipExtraData(byte[] data)
{
if (data == null)
{
_data = new byte[0];
_data = Empty.Array<byte>();
}
else
{
Expand Down Expand Up @@ -552,7 +553,7 @@ public void Clear()
{
if ((_data == null) || (_data.Length != 0))
{
_data = new byte[0];
_data = Empty.Array<byte>();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public ZipFile(Stream stream, bool leaveOpen)
}
else
{
entries_ = new ZipEntry[0];
entries_ = Empty.Array<ZipEntry>();
isNewArchive_ = true;
}
}
Expand All @@ -549,7 +549,7 @@ public ZipFile(Stream stream, bool leaveOpen)
/// </summary>
internal ZipFile()
{
entries_ = new ZipEntry[0];
entries_ = Empty.Array<ZipEntry>();
isNewArchive_ = true;
}

Expand Down Expand Up @@ -2338,7 +2338,7 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
baseStream_.Write(centralExtraData, 0, centralExtraData.Length);
}

byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : new byte[0];
byte[] rawComment = (entry.Comment != null) ? Encoding.ASCII.GetBytes(entry.Comment) : Empty.Array<byte>();

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

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

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

/// <summary>
/// Flag indicating that header patching is required for the current entry.
Expand Down
5 changes: 3 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using ICSharpCode.SharpZipLib.Core;

namespace ICSharpCode.SharpZipLib.Zip
{
Expand Down Expand Up @@ -174,7 +175,7 @@ public static string ConvertToStringExt(int flags, byte[] data)
/// <returns>Converted array</returns>
public static byte[] ConvertToArray(string str)
=> str == null
? new byte[0]
? Empty.Array<byte>()
: Encoding.GetEncoding(CodePage).GetBytes(str);

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