Skip to content

Commit ad2951e

Browse files
committed
Added empty stream as fallback path
1 parent 20b7a95 commit ad2951e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Microsoft.Toolkit.HighPerformance/Streams/MemoryStream.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static Stream Create(ReadOnlyMemory<byte> memory, bool isReadOnly)
3434
return new MemoryStream<MemoryManagerOwner>(memoryManagerSpanSource, isReadOnly);
3535
}
3636

37-
throw new NotImplementedException();
37+
// Return an empty stream if the memory was empty
38+
return new MemoryStream<ArrayOwner>(ArrayOwner.Empty, isReadOnly);
3839
}
3940

4041
/// <summary>
@@ -61,7 +62,8 @@ public static Stream Create(IMemoryOwner<byte> memoryOwner)
6162
return new IMemoryOwnerStream<MemoryManagerOwner>(memoryManagerSpanSource, false, memoryOwner);
6263
}
6364

64-
throw new NotImplementedException();
65+
// Return an empty stream if the memory was empty
66+
return new IMemoryOwnerStream<ArrayOwner>(ArrayOwner.Empty, false, memoryOwner);
6567
}
6668
}
6769
}

Microsoft.Toolkit.HighPerformance/Streams/Sources/ArrayOwner.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public ArrayOwner(byte[] array, int offset, int length)
3838
this.length = length;
3939
}
4040

41+
/// <summary>
42+
/// Gets an empty <see cref="ArrayOwner"/> instance.
43+
/// </summary>
44+
public static ArrayOwner Empty
45+
{
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
get => new ArrayOwner(Array.Empty<byte>(), 0, 0);
48+
}
49+
4150
/// <inheritdoc/>
4251
public int Length
4352
{

0 commit comments

Comments
 (0)