88using System . Runtime . InteropServices ;
99#if ! NETSTANDARD2_1_OR_GREATER
1010using System . Buffers ;
11+ #endif
1112using System . Threading ;
1213using System . Threading . Tasks ;
14+ #if NETSTANDARD2_1_OR_GREATER
15+ using System . ComponentModel ;
1316#endif
1417
1518namespace CommunityToolkit . HighPerformance ;
@@ -19,16 +22,22 @@ namespace CommunityToolkit.HighPerformance;
1922/// </summary>
2023public static class StreamExtensions
2124{
22- #if ! NETSTANDARD2_1_OR_GREATER
2325 /// <summary>
2426 /// Asynchronously reads a sequence of bytes from a given <see cref="Stream"/> instance.
2527 /// </summary>
2628 /// <param name="stream">The source <see cref="Stream"/> to read data from.</param>
2729 /// <param name="buffer">The destination <see cref="Memory{T}"/> to write data to.</param>
2830 /// <param name="cancellationToken">The optional <see cref="CancellationToken"/> for the operation.</param>
2931 /// <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
3036 public static ValueTask < int > ReadAsync ( this Stream stream , Memory < byte > buffer , CancellationToken cancellationToken = default )
3137 {
38+ #if NETSTANDARD2_1_OR_GREATER
39+ return stream . ReadAsync ( buffer , cancellationToken ) ;
40+ #else
3241 if ( cancellationToken . IsCancellationRequested )
3342 {
3443 return new ( Task . FromCanceled < int > ( cancellationToken ) ) ;
@@ -69,6 +78,7 @@ static async Task<int> ReadAsyncFallback(Stream stream, Memory<byte> buffer, Can
6978 }
7079
7180 return new ( ReadAsyncFallback ( stream , buffer , cancellationToken ) ) ;
81+ #endif
7282 }
7383
7484 /// <summary>
@@ -78,8 +88,15 @@ static async Task<int> ReadAsyncFallback(Stream stream, Memory<byte> buffer, Can
7888 /// <param name="buffer">The source <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
7989 /// <param name="cancellationToken">The optional <see cref="CancellationToken"/> for the operation.</param>
8090 /// <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
8195 public static ValueTask WriteAsync ( this Stream stream , ReadOnlyMemory < byte > buffer , CancellationToken cancellationToken = default )
8296 {
97+ #if NETSTANDARD2_1_OR_GREATER
98+ return stream . WriteAsync ( buffer , cancellationToken ) ;
99+ #else
83100 if ( cancellationToken . IsCancellationRequested )
84101 {
85102 return new ( Task . FromCanceled ( cancellationToken ) ) ;
@@ -108,6 +125,7 @@ static async Task WriteAsyncFallback(Stream stream, ReadOnlyMemory<byte> buffer,
108125 }
109126
110127 return new ( WriteAsyncFallback ( stream , buffer , cancellationToken ) ) ;
128+ #endif
111129 }
112130
113131 /// <summary>
@@ -116,8 +134,15 @@ static async Task WriteAsyncFallback(Stream stream, ReadOnlyMemory<byte> buffer,
116134 /// <param name="stream">The source <see cref="Stream"/> to read data from.</param>
117135 /// <param name="buffer">The target <see cref="Span{T}"/> to write data to.</param>
118136 /// <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
119141 public static int Read ( this Stream stream , Span < byte > buffer )
120142 {
143+ #if NETSTANDARD2_1_OR_GREATER
144+ return stream . Read ( buffer ) ;
145+ #else
121146 byte [ ] rent = ArrayPool < byte > . Shared . Rent ( buffer . Length ) ;
122147
123148 try
@@ -135,15 +160,23 @@ public static int Read(this Stream stream, Span<byte> buffer)
135160 {
136161 ArrayPool < byte > . Shared . Return ( rent ) ;
137162 }
163+ #endif
138164 }
139165
140166 /// <summary>
141167 /// Writes a sequence of bytes to a given <see cref="Stream"/> instance.
142168 /// </summary>
143169 /// <param name="stream">The destination <see cref="Stream"/> to write data to.</param>
144170 /// <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
145175 public static void Write ( this Stream stream , ReadOnlySpan < byte > buffer )
146176 {
177+ #if NETSTANDARD2_1_OR_GREATER
178+ stream . Write ( buffer ) ;
179+ #else
147180 byte [ ] rent = ArrayPool < byte > . Shared . Rent ( buffer . Length ) ;
148181
149182 try
@@ -156,8 +189,8 @@ public static void Write(this Stream stream, ReadOnlySpan<byte> buffer)
156189 {
157190 ArrayPool < byte > . Shared . Return ( rent ) ;
158191 }
159- }
160192#endif
193+ }
161194
162195 /// <summary>
163196 /// Reads a value of a specified type from a source <see cref="Stream"/> instance.
0 commit comments