8
8
using System . Runtime . InteropServices ;
9
9
#if ! NETSTANDARD2_1_OR_GREATER
10
10
using System . Buffers ;
11
+ #endif
11
12
using System . Threading ;
12
13
using System . Threading . Tasks ;
14
+ #if NETSTANDARD2_1_OR_GREATER
15
+ using System . ComponentModel ;
13
16
#endif
14
17
15
18
namespace CommunityToolkit . HighPerformance ;
@@ -19,16 +22,22 @@ namespace CommunityToolkit.HighPerformance;
19
22
/// </summary>
20
23
public static class StreamExtensions
21
24
{
22
- #if ! NETSTANDARD2_1_OR_GREATER
23
25
/// <summary>
24
26
/// Asynchronously reads a sequence of bytes from a given <see cref="Stream"/> instance.
25
27
/// </summary>
26
28
/// <param name="stream">The source <see cref="Stream"/> to read data from.</param>
27
29
/// <param name="buffer">The destination <see cref="Memory{T}"/> to write data to.</param>
28
30
/// <param name="cancellationToken">The optional <see cref="CancellationToken"/> for the operation.</param>
29
31
/// <returns>A <see cref="ValueTask"/> representing the operation being performed.</returns>
32
+ #if NETSTANDARD2_1_OR_GREATER
33
+ [ Obsolete ( "This API is only available for binary compatibility, but Stream.ReadAsync should be used instead." ) ]
34
+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
35
+ #endif
30
36
public static ValueTask < int > ReadAsync ( this Stream stream , Memory < byte > buffer , CancellationToken cancellationToken = default )
31
37
{
38
+ #if NETSTANDARD2_1_OR_GREATER
39
+ return stream . ReadAsync ( buffer , cancellationToken ) ;
40
+ #else
32
41
if ( cancellationToken . IsCancellationRequested )
33
42
{
34
43
return new ( Task . FromCanceled < int > ( cancellationToken ) ) ;
@@ -69,6 +78,7 @@ static async Task<int> ReadAsyncFallback(Stream stream, Memory<byte> buffer, Can
69
78
}
70
79
71
80
return new ( ReadAsyncFallback ( stream , buffer , cancellationToken ) ) ;
81
+ #endif
72
82
}
73
83
74
84
/// <summary>
@@ -78,8 +88,15 @@ static async Task<int> ReadAsyncFallback(Stream stream, Memory<byte> buffer, Can
78
88
/// <param name="buffer">The source <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
79
89
/// <param name="cancellationToken">The optional <see cref="CancellationToken"/> for the operation.</param>
80
90
/// <returns>A <see cref="ValueTask"/> representing the operation being performed.</returns>
91
+ #if NETSTANDARD2_1_OR_GREATER
92
+ [ Obsolete ( "This API is only available for binary compatibility, but Stream.WriteAsync should be used instead." ) ]
93
+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
94
+ #endif
81
95
public static ValueTask WriteAsync ( this Stream stream , ReadOnlyMemory < byte > buffer , CancellationToken cancellationToken = default )
82
96
{
97
+ #if NETSTANDARD2_1_OR_GREATER
98
+ return stream . WriteAsync ( buffer , cancellationToken ) ;
99
+ #else
83
100
if ( cancellationToken . IsCancellationRequested )
84
101
{
85
102
return new ( Task . FromCanceled ( cancellationToken ) ) ;
@@ -108,6 +125,7 @@ static async Task WriteAsyncFallback(Stream stream, ReadOnlyMemory<byte> buffer,
108
125
}
109
126
110
127
return new ( WriteAsyncFallback ( stream , buffer , cancellationToken ) ) ;
128
+ #endif
111
129
}
112
130
113
131
/// <summary>
@@ -116,8 +134,15 @@ static async Task WriteAsyncFallback(Stream stream, ReadOnlyMemory<byte> buffer,
116
134
/// <param name="stream">The source <see cref="Stream"/> to read data from.</param>
117
135
/// <param name="buffer">The target <see cref="Span{T}"/> to write data to.</param>
118
136
/// <returns>The number of bytes that have been read.</returns>
137
+ #if NETSTANDARD2_1_OR_GREATER
138
+ [ Obsolete ( "This API is only available for binary compatibility, but Stream.Read should be used instead." ) ]
139
+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
140
+ #endif
119
141
public static int Read ( this Stream stream , Span < byte > buffer )
120
142
{
143
+ #if NETSTANDARD2_1_OR_GREATER
144
+ return stream . Read ( buffer ) ;
145
+ #else
121
146
byte [ ] rent = ArrayPool < byte > . Shared . Rent ( buffer . Length ) ;
122
147
123
148
try
@@ -135,15 +160,23 @@ public static int Read(this Stream stream, Span<byte> buffer)
135
160
{
136
161
ArrayPool < byte > . Shared . Return ( rent ) ;
137
162
}
163
+ #endif
138
164
}
139
165
140
166
/// <summary>
141
167
/// Writes a sequence of bytes to a given <see cref="Stream"/> instance.
142
168
/// </summary>
143
169
/// <param name="stream">The destination <see cref="Stream"/> to write data to.</param>
144
170
/// <param name="buffer">The source <see cref="Span{T}"/> to read data from.</param>
171
+ #if NETSTANDARD2_1_OR_GREATER
172
+ [ Obsolete ( "This API is only available for binary compatibility, but Stream.Read should be used instead." ) ]
173
+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
174
+ #endif
145
175
public static void Write ( this Stream stream , ReadOnlySpan < byte > buffer )
146
176
{
177
+ #if NETSTANDARD2_1_OR_GREATER
178
+ stream . Write ( buffer ) ;
179
+ #else
147
180
byte [ ] rent = ArrayPool < byte > . Shared . Rent ( buffer . Length ) ;
148
181
149
182
try
@@ -156,8 +189,8 @@ public static void Write(this Stream stream, ReadOnlySpan<byte> buffer)
156
189
{
157
190
ArrayPool < byte > . Shared . Return ( rent ) ;
158
191
}
159
- }
160
192
#endif
193
+ }
161
194
162
195
/// <summary>
163
196
/// Reads a value of a specified type from a source <see cref="Stream"/> instance.
0 commit comments