Skip to content

Commit b765406

Browse files
authored
Add Stream.Write(ReadOnlySpan<byte>) (#487)
* Add Stream.Write(ReadOnlySpan<byte>) * Update PolyfillTests_Stream.cs * Update Directory.Build.props
1 parent d7a9590 commit b765406

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+5473
-396
lines changed

TestResults/ApiBuilderTests-windows-net11.0-report.html

Lines changed: 1593 additions & 0 deletions
Large diffs are not rendered by default.

TestResults/Tests-windows-net10.0-report.html

Lines changed: 1593 additions & 0 deletions
Large diffs are not rendered by default.

TestResults/Tests-windows-net48-report.html

Lines changed: 1593 additions & 0 deletions
Large diffs are not rendered by default.

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 748**
1+
**API count: 749**

api_list.include.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@
787787
* `void ReadExactly(Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactly?view=net-11.0#system-io-stream-readexactly(system-span((system-byte))))
788788
* `ValueTask ReadExactlyAsync(byte[], int, int, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactlyasync?view=net-11.0#system-io-stream-readexactlyasync(system-byte()-system-int32-system-int32-system-threading-cancellationtoken))
789789
* `ValueTask ReadExactlyAsync(Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactlyasync?view=net-11.0#system-io-stream-readexactlyasync(system-memory((system-byte))-system-threading-cancellationtoken))
790+
* `void Write(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.write?view=net-11.0#system-io-stream-write(system-readonlyspan((system-byte))))
790791
* `ValueTask WriteAsync(ReadOnlyMemory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync?view=net-11.0#system-io-stream-writeasync(system-readonlymemory((system-byte))-system-threading-cancellationtoken))
791792

792793

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
1313
* `uap10`
1414

1515

16-
**API count: 748**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
16+
**API count: 749**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
1717

1818

1919
**See [Milestones](../../milestones?state=closed) for release notes.**
@@ -1290,6 +1290,7 @@ The class `Polyfill` includes the following extension methods:
12901290
* `void ReadExactly(Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactly?view=net-11.0#system-io-stream-readexactly(system-span((system-byte))))
12911291
* `ValueTask ReadExactlyAsync(byte[], int, int, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactlyasync?view=net-11.0#system-io-stream-readexactlyasync(system-byte()-system-int32-system-int32-system-threading-cancellationtoken))
12921292
* `ValueTask ReadExactlyAsync(Memory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readexactlyasync?view=net-11.0#system-io-stream-readexactlyasync(system-memory((system-byte))-system-threading-cancellationtoken))
1293+
* `void Write(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.write?view=net-11.0#system-io-stream-write(system-readonlyspan((system-byte))))
12931294
* `ValueTask WriteAsync(ReadOnlyMemory<byte>, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync?view=net-11.0#system-io-stream-writeasync(system-readonlymemory((system-byte))-system-threading-cancellationtoken))
12941295

12951296

src/Consume/Consume.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ void File_Methods()
648648
using var nullHandle = File.OpenNullHandle();
649649
#endif
650650

651-
FileSystemInfo hardLink = File.CreateHardLink("hardlink.txt", TestFilePath);
651+
var hardLink = File.CreateHardLink("hardlink.txt", TestFilePath);
652652
var fileInfo = new FileInfo("hardlink2.txt");
653653
fileInfo.CreateAsHardLink(TestFilePath);
654654
}
@@ -1005,6 +1005,9 @@ async Task Stream_Methods()
10051005
var input = new byte[] {1, 2};
10061006
using var stream = new MemoryStream(input);
10071007
var result = new byte[2];
1008+
#if FeatureMemory
1009+
((Stream)stream).Write((ReadOnlySpan<byte>)input);
1010+
#endif
10081011
#if FeatureMemory && FeatureValueTask
10091012
var memory = new Memory<byte>(result);
10101013
var read = await stream.ReadAsync(memory);

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;NETSDK1138;NU1901;NU1902;NU1903;CA1822;CA1847;CA1861;NU1510;NU1608;NU1109</NoWarn>
5-
<Version>9.13.0</Version>
5+
<Version>9.14.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<PackageTags>Polyfill</PackageTags>
88
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>

src/Polyfill/Polyfill_Stream.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,12 @@ namespace Polyfills;
33
// ReSharper disable once RedundantUsingDirective
44
using System;
55
using System.IO;
6-
using System.Runtime.InteropServices;
76
using System.Threading;
87
using System.Threading.Tasks;
98

109
static partial class Polyfill
1110
{
1211
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
13-
#if FeatureMemory && FeatureValueTask
14-
15-
/// <summary>
16-
/// Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by
17-
/// the number of bytes read, and monitors cancellation requests.
18-
/// </summary>
19-
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync?view=net-11.0#system-io-stream-readasync(system-memory((system-byte))-system-threading-cancellationtoken)
20-
public static ValueTask<int> ReadAsync(
21-
this Stream target,
22-
Memory<byte> buffer,
23-
CancellationToken cancellationToken = default)
24-
{
25-
if (!MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>) buffer, out var segment))
26-
{
27-
segment = new(buffer.ToArray());
28-
}
29-
30-
var task = target.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken);
31-
return new(task);
32-
}
33-
34-
/// <summary>
35-
/// Asynchronously writes a sequence of bytes to the current stream, advances the current position
36-
/// within this stream by the number of bytes written, and monitors cancellation requests.
37-
/// </summary>
38-
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.writeasync?view=net-11.0#system-io-stream-writeasync(system-readonlymemory((system-byte))-system-threading-cancellationtoken)
39-
public static ValueTask WriteAsync(
40-
this Stream target,
41-
ReadOnlyMemory<byte> buffer,
42-
CancellationToken cancellationToken = default)
43-
{
44-
if (!MemoryMarshal.TryGetArray(buffer, out var segment))
45-
{
46-
segment = new(buffer.ToArray());
47-
}
48-
49-
var task = target.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken);
50-
return new(task);
51-
}
52-
53-
#endif
5412

5513
/// <summary>
5614
/// Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified

src/Polyfill/Polyfill_Stream_Read.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,37 @@ namespace Polyfills;
33
using System;
44
using System.IO;
55
// ReSharper disable RedundantUsingDirective
6+
using System.Runtime.InteropServices;
67
using System.Threading;
78
using System.Threading.Tasks;
89
// ReSharper restore RedundantUsingDirective
910

1011
static partial class Polyfill
1112
{
13+
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER
14+
#if FeatureMemory && FeatureValueTask
15+
16+
/// <summary>
17+
/// Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by
18+
/// the number of bytes read, and monitors cancellation requests.
19+
/// </summary>
20+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync?view=net-11.0#system-io-stream-readasync(system-memory((system-byte))-system-threading-cancellationtoken)
21+
public static ValueTask<int> ReadAsync(
22+
this Stream target,
23+
Memory<byte> buffer,
24+
CancellationToken cancellationToken = default)
25+
{
26+
if (!MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>) buffer, out var segment))
27+
{
28+
segment = new(buffer.ToArray());
29+
}
30+
31+
var task = target.ReadAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken);
32+
return new(task);
33+
}
34+
35+
#endif
36+
#endif
1237
#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1 && FeatureMemory
1338
/// <summary>
1439
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

0 commit comments

Comments
 (0)