Skip to content

Commit 1c1df7b

Browse files
authored
PR #575: Replace uses of new T[0] with Array.Empty<T>
* Add the 'EmptyRefs' helper, to support Array.Empty on .NET 4.5 (ref: #501) * Replace uses of 'new T[0]' with 'Empty.Array<T>'
1 parent 765eb69 commit 1c1df7b

File tree

7 files changed

+33
-12
lines changed

7 files changed

+33
-12
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace ICSharpCode.SharpZipLib.Core
4+
{
5+
internal static class Empty
6+
{
7+
#if NET45
8+
internal static class EmptyArray<T>
9+
{
10+
public static readonly T[] Value = new T[0];
11+
}
12+
public static T[] Array<T>() => EmptyArray<T>.Value;
13+
#else
14+
public static T[] Array<T>() => System.Array.Empty<T>();
15+
#endif
16+
}
17+
}

src/ICSharpCode.SharpZipLib/Encryption/ZipAESTransform.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Security.Cryptography;
3+
using ICSharpCode.SharpZipLib.Core;
34

45
namespace ICSharpCode.SharpZipLib.Encryption
56
{
@@ -163,7 +164,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
163164
{
164165
throw new NotImplementedException("TransformFinalBlock is not implemented and inputCount is greater than 0");
165166
}
166-
return new byte[0];
167+
return Empty.Array<byte>();
167168
}
168169

169170
/// <summary>

src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4+
using ICSharpCode.SharpZipLib.Core;
45

56
namespace ICSharpCode.SharpZipLib.Tar
67
{
@@ -465,7 +466,7 @@ public TarEntry[] GetDirectoryEntries()
465466
{
466467
if ((file == null) || !Directory.Exists(file))
467468
{
468-
return new TarEntry[0];
469+
return Empty.Array<TarEntry>();
469470
}
470471

471472
string[] list = Directory.GetFileSystemEntries(file);

src/ICSharpCode.SharpZipLib/Zip/ZipExtraData.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using ICSharpCode.SharpZipLib.Core;
34

45
namespace ICSharpCode.SharpZipLib.Zip
56
{
@@ -521,7 +522,7 @@ public ZipExtraData(byte[] data)
521522
{
522523
if (data == null)
523524
{
524-
_data = new byte[0];
525+
_data = Empty.Array<byte>();
525526
}
526527
else
527528
{
@@ -552,7 +553,7 @@ public void Clear()
552553
{
553554
if ((_data == null) || (_data.Length != 0))
554555
{
555-
_data = new byte[0];
556+
_data = Empty.Array<byte>();
556557
}
557558
}
558559

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public ZipFile(Stream stream, bool leaveOpen)
539539
}
540540
else
541541
{
542-
entries_ = new ZipEntry[0];
542+
entries_ = Empty.Array<ZipEntry>();
543543
isNewArchive_ = true;
544544
}
545545
}
@@ -549,7 +549,7 @@ public ZipFile(Stream stream, bool leaveOpen)
549549
/// </summary>
550550
internal ZipFile()
551551
{
552-
entries_ = new ZipEntry[0];
552+
entries_ = Empty.Array<ZipEntry>();
553553
isNewArchive_ = true;
554554
}
555555

@@ -2338,7 +2338,7 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
23382338
baseStream_.Write(centralExtraData, 0, centralExtraData.Length);
23392339
}
23402340

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

23432343
if (rawComment.Length > 0)
23442344
{
@@ -3347,7 +3347,7 @@ private void DisposeInternal(bool disposing)
33473347
if (!isDisposed_)
33483348
{
33493349
isDisposed_ = true;
3350-
entries_ = new ZipEntry[0];
3350+
entries_ = Empty.Array<ZipEntry>();
33513351

33523352
if (IsStreamOwner && (baseStream_ != null))
33533353
{

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ public override void Finish()
872872
byte[] entryComment =
873873
(entry.Comment != null) ?
874874
ZipStrings.ConvertToArray(entry.Flags, entry.Comment) :
875-
new byte[0];
875+
Empty.Array<byte>();
876876

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

992992
/// <summary>
993993
/// Flag indicating that header patching is required for the current entry.

src/ICSharpCode.SharpZipLib/Zip/ZipStrings.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using ICSharpCode.SharpZipLib.Core;
34

45
namespace ICSharpCode.SharpZipLib.Zip
56
{
@@ -174,7 +175,7 @@ public static string ConvertToStringExt(int flags, byte[] data)
174175
/// <returns>Converted array</returns>
175176
public static byte[] ConvertToArray(string str)
176177
=> str == null
177-
? new byte[0]
178+
? Empty.Array<byte>()
178179
: Encoding.GetEncoding(CodePage).GetBytes(str);
179180

180181
/// <summary>
@@ -187,7 +188,7 @@ public static byte[] ConvertToArray(string str)
187188
/// <returns>Converted array</returns>
188189
public static byte[] ConvertToArray(int flags, string str)
189190
=> (string.IsNullOrEmpty(str))
190-
? new byte[0]
191+
? Empty.Array<byte>()
191192
: EncodingFromFlag(flags).GetBytes(str);
192193
}
193194
}

0 commit comments

Comments
 (0)