@@ -21,9 +21,16 @@ internal static partial class MemoryStream
21
21
/// <param name="memory">The input <see cref="ReadOnlyMemory{T}"/> instance.</param>
22
22
/// <param name="isReadOnly">Indicates whether or not <paramref name="memory"/> can be written to.</param>
23
23
/// <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>
24
25
[ Pure ]
25
26
public static Stream Create ( ReadOnlyMemory < byte > memory , bool isReadOnly )
26
27
{
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
+
27
34
if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
28
35
{
29
36
var arraySpanSource = new ArrayOwner ( segment . Array ! , segment . Offset , segment . Count ) ;
@@ -38,20 +45,26 @@ public static Stream Create(ReadOnlyMemory<byte> memory, bool isReadOnly)
38
45
return new MemoryStream < MemoryManagerOwner > ( memoryManagerSpanSource , isReadOnly ) ;
39
46
}
40
47
41
- // Return an empty stream if the memory was empty
42
- return new MemoryStream < ArrayOwner > ( ArrayOwner . Empty , isReadOnly ) ;
48
+ return ThrowNotSupportedExceptionForInvalidMemory ( ) ;
43
49
}
44
50
45
51
/// <summary>
46
52
/// Creates a new <see cref="Stream"/> from the input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/> instance.
47
53
/// </summary>
48
54
/// <param name="memoryOwner">The input <see cref="IMemoryOwner{T}"/> instance.</param>
49
55
/// <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>
50
57
[ Pure ]
51
58
public static Stream Create ( IMemoryOwner < byte > memoryOwner )
52
59
{
53
60
Memory < byte > memory = memoryOwner . Memory ;
54
61
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
+
55
68
if ( MemoryMarshal . TryGetArray ( memory , out ArraySegment < byte > segment ) )
56
69
{
57
70
var arraySpanSource = new ArrayOwner ( segment . Array ! , segment . Offset , segment . Count ) ;
@@ -66,8 +79,17 @@ public static Stream Create(IMemoryOwner<byte> memoryOwner)
66
79
return new IMemoryOwnerStream < MemoryManagerOwner > ( memoryManagerSpanSource , memoryOwner ) ;
67
80
}
68
81
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." ) ;
71
93
}
72
94
}
73
95
}
0 commit comments