@@ -38,10 +38,12 @@ public static void EfficientCopyTo(this Stream input, Stream output)
3838
3939 public static int Read ( this Stream stream , byte [ ] buffer , int offset , int count , TimeSpan timeout , CancellationToken cancellationToken )
4040 {
41- return UseStreamWithTimeout (
41+ return ExecuteOperationWithTimeout (
4242 stream ,
43- ( str , state ) => str . Read ( state . buffer , state . offset , state . count ) ,
44- ( buffer , offset , count ) ,
43+ ( str , state ) => str . Read ( state . Buffer , state . Offset , state . Count ) ,
44+ buffer ,
45+ offset ,
46+ count ,
4547 timeout ,
4648 cancellationToken ) ;
4749 }
@@ -190,14 +192,16 @@ public static async Task ReadBytesAsync(this Stream stream, byte[] destination,
190192
191193 public static void Write ( this Stream stream , byte [ ] buffer , int offset , int count , TimeSpan timeout , CancellationToken cancellationToken )
192194 {
193- UseStreamWithTimeout (
195+ ExecuteOperationWithTimeout (
194196 stream ,
195197 ( str , state ) =>
196198 {
197- str . Write ( state . buffer , state . offset , state . count ) ;
199+ str . Write ( state . Buffer , state . Offset , state . Count ) ;
198200 return true ;
199201 } ,
200- ( buffer , offset , count ) ,
202+ buffer ,
203+ offset ,
204+ count ,
201205 timeout ,
202206 cancellationToken ) ;
203207 }
@@ -270,7 +274,7 @@ public static async Task WriteBytesAsync(this Stream stream, OperationContext op
270274 }
271275 }
272276
273- private static TResult UseStreamWithTimeout < TResult , TState > ( Stream stream , Func < Stream , TState , TResult > method , TState state , TimeSpan timeout , CancellationToken cancellationToken )
277+ private static TResult ExecuteOperationWithTimeout < TResult > ( Stream stream , Func < Stream , ( byte [ ] Buffer , int Offset , int Count ) , TResult > operation , byte [ ] buffer , int offset , int count , TimeSpan timeout , CancellationToken cancellationToken )
274278 {
275279 StreamDisposeCallbackState callbackState = null ;
276280 Timer timer = null ;
@@ -289,8 +293,8 @@ private static TResult UseStreamWithTimeout<TResult, TState>(Stream stream, Func
289293
290294 try
291295 {
292- var result = method ( stream , state ) ;
293- if ( callbackState ? . TryChangeState ( OperationState . Done ) == false )
296+ var result = operation ( stream , ( buffer , offset , count ) ) ;
297+ if ( callbackState ? . TryChangeStateFromInProgress ( OperationState . Done ) == false )
294298 {
295299 // if cannot change the state - then the stream was/will be disposed, throw here
296300 throw new IOException ( ) ;
@@ -317,7 +321,7 @@ private static TResult UseStreamWithTimeout<TResult, TState>(Stream stream, Func
317321 static void DisposeStreamCallback ( object state )
318322 {
319323 var disposeCallbackState = ( StreamDisposeCallbackState ) state ;
320- if ( ! disposeCallbackState . TryChangeState ( OperationState . Cancelled ) )
324+ if ( ! disposeCallbackState . TryChangeStateFromInProgress ( OperationState . Cancelled ) )
321325 {
322326 // if cannot change the state - then I/O was already succeeded
323327 return ;
@@ -338,12 +342,9 @@ private record StreamDisposeCallbackState(Stream Stream)
338342 {
339343 private int _operationState = 0 ;
340344
341- public OperationState OperationState
342- {
343- get => ( OperationState ) _operationState ;
344- }
345+ public OperationState OperationState => ( OperationState ) _operationState ;
345346
346- public bool TryChangeState ( OperationState newState ) =>
347+ public bool TryChangeStateFromInProgress ( OperationState newState ) =>
347348 Interlocked . CompareExchange ( ref _operationState , ( int ) newState , ( int ) OperationState . InProgress ) == ( int ) OperationState . InProgress ;
348349 }
349350
0 commit comments