@@ -21,9 +21,16 @@ internal static partial class MemoryStream
2121 /// <param name="memory">The input <see cref="ReadOnlyMemory{T}"/> instance.</param>
2222 /// <param name="isReadOnly">Indicates whether or not <paramref name="memory"/> can be written to.</param>
2323 /// <returns>A <see cref="Stream"/> wrapping the underlying data for <paramref name="memory"/>.</returns>
24+ /// <exception cref="ArgumentException">Thrown when <paramref name="memory"/> has an invalid data store.</exception>
2425 [ Pure ]
2526 public static Stream Create ( ReadOnlyMemory < byte > memory , bool isReadOnly )
2627 {
28+ if ( memory . IsEmpty )
29+ {
30+ // Return an empty stream if the memory was empty
31+ return new MemoryStream < ArrayOwner > ( ArrayOwner . Empty , isReadOnly ) ;
32+ }
33+
2734 if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
2835 {
2936 var arraySpanSource = new ArrayOwner ( segment . Array ! , segment . Offset , segment . Count ) ;
@@ -38,20 +45,26 @@ public static Stream Create(ReadOnlyMemory<byte> memory, bool isReadOnly)
3845 return new MemoryStream < MemoryManagerOwner > ( memoryManagerSpanSource , isReadOnly ) ;
3946 }
4047
41- // Return an empty stream if the memory was empty
42- return new MemoryStream < ArrayOwner > ( ArrayOwner . Empty , isReadOnly ) ;
48+ return ThrowNotSupportedExceptionForInvalidMemory ( ) ;
4349 }
4450
4551 /// <summary>
4652 /// Creates a new <see cref="Stream"/> from the input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.
4753 /// </summary>
4854 /// <param name="memoryOwner">The input <see cref="IMemoryOwner{T}"/> instance.</param>
4955 /// <returns>A <see cref="Stream"/> wrapping the underlying data for <paramref name="memoryOwner"/>.</returns>
56+ /// <exception cref="ArgumentException">Thrown when <paramref name="memoryOwner"/> has an invalid data store.</exception>
5057 [ Pure ]
5158 public static Stream Create ( IMemoryOwner < byte > memoryOwner )
5259 {
5360 Memory < byte > memory = memoryOwner . Memory ;
5461
62+ if ( memory . IsEmpty )
63+ {
64+ // Return an empty stream if the memory was empty
65+ return new IMemoryOwnerStream < ArrayOwner > ( ArrayOwner . Empty , memoryOwner ) ;
66+ }
67+
5568 if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
5669 {
5770 var arraySpanSource = new ArrayOwner ( segment . Array ! , segment . Offset , segment . Count ) ;
@@ -66,8 +79,17 @@ public static Stream Create(IMemoryOwner<byte> memoryOwner)
6679 return new IMemoryOwnerStream < MemoryManagerOwner > ( memoryManagerSpanSource , memoryOwner ) ;
6780 }
6881
69- // Return an empty stream if the memory was empty
70- return new IMemoryOwnerStream < ArrayOwner > ( ArrayOwner . Empty , memoryOwner ) ;
82+ return ThrowNotSupportedExceptionForInvalidMemory ( ) ;
83+ }
84+
85+ /// <summary>
86+ /// Throws a <see cref="ArgumentException"/> when a given <see cref="Memory{T}"/>
87+ /// or <see cref="IMemoryOwner{T}"/> instance has an unsupported backing store.
88+ /// </summary>
89+ /// <returns>Nothing, this method always throws.</returns>
90+ private static Stream ThrowNotSupportedExceptionForInvalidMemory ( )
91+ {
92+ throw new ArgumentException ( "The input instance doesn't have a valid underlying data store." ) ;
7193 }
7294 }
7395}
0 commit comments