Skip to content

Commit 247c9c5

Browse files
committed
Added StreamExtensions
1 parent 1ba774f commit 247c9c5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace System
8+
{
9+
internal static class StreamExtensions
10+
{
11+
public static Task<int> ReadAsync(this Stream stream, Memory<byte> memory)
12+
{
13+
return stream.ReadAsync(memory, CancellationToken.None);
14+
}
15+
16+
public static Task<int> ReadAsync(this Stream stream, Memory<byte> memory, CancellationToken cancellationToken)
17+
{
18+
if (!MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> buffer))
19+
{
20+
new NotSupportedException("This Memory does not support exposing the underlying array.");
21+
}
22+
return stream.ReadAsync(buffer.Array, buffer.Offset, buffer.Count);
23+
}
24+
25+
public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> memory)
26+
{
27+
return stream.WriteAsync(memory, CancellationToken.None);
28+
}
29+
30+
public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> memory, CancellationToken cancellationToken)
31+
{
32+
if (!MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> buffer))
33+
{
34+
new NotSupportedException("This Memory does not support exposing the underlying array.");
35+
}
36+
return stream.WriteAsync(buffer.Array, buffer.Offset, buffer.Count, cancellationToken);
37+
}
38+
}
39+
}

StandardSocketsHttpHandler/StandardSocketsHttpHandler.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
1111
<Compile Remove="HashCode.cs" />
12+
<Compile Remove="IO\Extensions\StreamExtensions.cs" />
1213
<Compile Remove="Net\Security\SslApplicationProtocol.cs" />
1314
<Compile Remove="Net\Security\SslClientAuthenticationOptions.cs" />
1415
</ItemGroup>

0 commit comments

Comments
 (0)