Skip to content

Commit c01fe8f

Browse files
committed
Code tweaks to MemoryStream initialization
1 parent fb0dbc7 commit c01fe8f

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

Microsoft.Toolkit.HighPerformance/Extensions/IMemoryOwnerExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Buffers;
67
using System.Diagnostics.Contracts;
78
using System.IO;
@@ -24,6 +25,7 @@ public static class IMemoryOwnerExtensions
2425
/// The caller does not need to track the lifetime of the input <see cref="IMemoryOwner{T}"/> of <see cref="byte"/>
2526
/// instance, as the returned <see cref="Stream"/> will take care of disposing that buffer when it is closed.
2627
/// </remarks>
28+
/// <exception cref="ArgumentException">Thrown when <paramref name="memoryOwner"/> has an invalid data store.</exception>
2729
[Pure]
2830
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2931
public static Stream AsStream(this IMemoryOwner<byte> memoryOwner)

Microsoft.Toolkit.HighPerformance/Extensions/MemoryExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static class MemoryExtensions
2626
/// In particular, the caller must ensure that the target buffer is not disposed as long
2727
/// as the returned <see cref="Stream"/> is in use, to avoid unexpected issues.
2828
/// </remarks>
29+
/// <exception cref="ArgumentException">Thrown when <paramref name="memory"/> has an invalid data store.</exception>
2930
[Pure]
3031
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3132
public static Stream AsStream(this Memory<byte> memory)

Microsoft.Toolkit.HighPerformance/Extensions/ReadOnlyMemoryExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class ReadOnlyMemoryExtensions
2525
/// In particular, the caller must ensure that the target buffer is not disposed as long
2626
/// as the returned <see cref="Stream"/> is in use, to avoid unexpected issues.
2727
/// </remarks>
28+
/// <exception cref="ArgumentException">Thrown when <paramref name="memory"/> has an invalid data store.</exception>
2829
[Pure]
2930
public static Stream AsStream(this ReadOnlyMemory<byte> memory)
3031
{

Microsoft.Toolkit.HighPerformance/Streams/MemoryStream.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)