@@ -101,20 +101,32 @@ public override IAsyncResult BeginWrite(byte[] buffer,
101
101
object ? state )
102
102
=> _stream . BeginWrite ( buffer , offset , count , callback , state ) ;
103
103
104
+ /// <inheritdoc cref="Stream.Close()" />
105
+ public override void Close ( )
106
+ {
107
+ base . Close ( ) ;
108
+ _stream . Close ( ) ;
109
+ }
110
+
104
111
/// <inheritdoc cref="Stream.CopyTo(Stream, int)" />
105
112
#if NETSTANDARD2_0 || NET462
106
- public new virtual void CopyTo ( Stream destination , int bufferSize )
107
- => _stream . CopyTo ( destination , bufferSize ) ;
113
+ public new virtual void CopyTo ( Stream destination , int bufferSize )
108
114
#else
109
115
public override void CopyTo ( Stream destination , int bufferSize )
110
- => _stream . CopyTo ( destination , bufferSize ) ;
111
116
#endif
117
+ {
118
+ ValidateCopyToArguments ( this , destination , bufferSize ) ;
119
+ _stream . CopyTo ( destination , bufferSize ) ;
120
+ }
112
121
113
122
/// <inheritdoc cref="Stream.CopyToAsync(Stream, int, CancellationToken)" />
114
123
public override Task CopyToAsync ( Stream destination ,
115
124
int bufferSize ,
116
125
CancellationToken cancellationToken )
117
- => _stream . CopyToAsync ( destination , bufferSize , cancellationToken ) ;
126
+ {
127
+ ValidateCopyToArguments ( this , destination , bufferSize ) ;
128
+ return _stream . CopyToAsync ( destination , bufferSize , cancellationToken ) ;
129
+ }
118
130
119
131
/// <inheritdoc cref="Stream.EndRead(IAsyncResult)" />
120
132
public override int EndRead ( IAsyncResult asyncResult )
@@ -141,9 +153,9 @@ public override int Read(byte[] buffer, int offset, int count)
141
153
=> _stream . Read ( buffer , offset , count ) ;
142
154
143
155
#if FEATURE_SPAN
144
- /// <inheritdoc cref="Stream.Read(Span{byte})" />
145
- public override int Read ( Span < byte > buffer )
146
- => _stream . Read ( buffer ) ;
156
+ /// <inheritdoc cref="Stream.Read(Span{byte})" />
157
+ public override int Read ( Span < byte > buffer )
158
+ => _stream . Read ( buffer ) ;
147
159
#endif
148
160
149
161
/// <inheritdoc cref="Stream.ReadAsync(byte[], int, int, CancellationToken)" />
@@ -154,10 +166,10 @@ public override Task<int> ReadAsync(byte[] buffer,
154
166
=> _stream . ReadAsync ( buffer , offset , count , cancellationToken ) ;
155
167
156
168
#if FEATURE_SPAN
157
- /// <inheritdoc cref="Stream.ReadAsync(Memory{byte}, CancellationToken)" />
158
- public override ValueTask < int > ReadAsync ( Memory < byte > buffer ,
159
- CancellationToken cancellationToken = new ( ) )
160
- => _stream . ReadAsync ( buffer , cancellationToken ) ;
169
+ /// <inheritdoc cref="Stream.ReadAsync(Memory{byte}, CancellationToken)" />
170
+ public override ValueTask < int > ReadAsync ( Memory < byte > buffer ,
171
+ CancellationToken cancellationToken = new ( ) )
172
+ => _stream . ReadAsync ( buffer , cancellationToken ) ;
161
173
#endif
162
174
163
175
/// <inheritdoc cref="Stream.ReadByte()" />
@@ -181,9 +193,9 @@ public override void Write(byte[] buffer, int offset, int count)
181
193
=> _stream . Write ( buffer , offset , count ) ;
182
194
183
195
#if FEATURE_SPAN
184
- /// <inheritdoc cref="Stream.Write(ReadOnlySpan{byte})" />
185
- public override void Write ( ReadOnlySpan < byte > buffer )
186
- => _stream . Write ( buffer ) ;
196
+ /// <inheritdoc cref="Stream.Write(ReadOnlySpan{byte})" />
197
+ public override void Write ( ReadOnlySpan < byte > buffer )
198
+ => _stream . Write ( buffer ) ;
187
199
#endif
188
200
189
201
/// <inheritdoc cref="Stream.WriteAsync(byte[], int, int, CancellationToken)" />
@@ -194,10 +206,10 @@ public override Task WriteAsync(byte[] buffer,
194
206
=> _stream . WriteAsync ( buffer , offset , count , cancellationToken ) ;
195
207
196
208
#if FEATURE_SPAN
197
- /// <inheritdoc cref="Stream.WriteAsync(ReadOnlyMemory{byte}, CancellationToken)" />
198
- public override ValueTask WriteAsync ( ReadOnlyMemory < byte > buffer ,
199
- CancellationToken cancellationToken = new ( ) )
200
- => _stream . WriteAsync ( buffer , cancellationToken ) ;
209
+ /// <inheritdoc cref="Stream.WriteAsync(ReadOnlyMemory{byte}, CancellationToken)" />
210
+ public override ValueTask WriteAsync ( ReadOnlyMemory < byte > buffer ,
211
+ CancellationToken cancellationToken = new ( ) )
212
+ => _stream . WriteAsync ( buffer , cancellationToken ) ;
201
213
#endif
202
214
203
215
/// <inheritdoc cref="Stream.WriteByte(byte)" />
@@ -211,6 +223,15 @@ protected override void Dispose(bool disposing)
211
223
base . Dispose ( disposing ) ;
212
224
}
213
225
226
+ #if FEATURE_ASYNC_FILE
227
+ /// <inheritdoc cref="Stream.DisposeAsync()" />
228
+ public override async ValueTask DisposeAsync ( )
229
+ {
230
+ await _stream . DisposeAsync ( ) ;
231
+ await base . DisposeAsync ( ) ;
232
+ }
233
+ #endif
234
+
214
235
/// <summary>
215
236
/// Allows to cast the internal Stream to a FileStream
216
237
/// </summary>
@@ -220,5 +241,38 @@ public static explicit operator FileStream(FileSystemStream fsStream)
220
241
{
221
242
return ( FileStream ) fsStream . _stream ;
222
243
}
244
+
245
+ private static void ValidateCopyToArguments ( Stream source , Stream destination , int bufferSize )
246
+ {
247
+ if ( destination == null )
248
+ {
249
+ throw new ArgumentNullException ( nameof ( destination ) , "Destination cannot be null." ) ;
250
+ }
251
+
252
+ if ( bufferSize <= 0 )
253
+ {
254
+ throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) , "Buffer size must be greater than zero." ) ;
255
+ }
256
+
257
+ if ( ! destination . CanWrite )
258
+ {
259
+ if ( destination . CanRead )
260
+ {
261
+ throw new NotSupportedException ( "Stream does not support writing." ) ;
262
+ }
263
+
264
+ throw new ObjectDisposedException ( "Cannot access a closed Stream." ) ;
265
+ }
266
+
267
+ if ( ! source . CanRead )
268
+ {
269
+ if ( source . CanWrite )
270
+ {
271
+ throw new NotSupportedException ( "Stream does not support reading." ) ;
272
+ }
273
+
274
+ throw new ObjectDisposedException ( "Cannot access a closed Stream." ) ;
275
+ }
276
+ }
223
277
}
224
278
}
0 commit comments