@@ -1688,22 +1688,45 @@ private int GetLastWin32ErrorAndDisposeHandleIfInvalid(bool throwIfInvalidHandle
1688
1688
public override Task CopyToAsync ( Stream destination , int bufferSize , CancellationToken cancellationToken )
1689
1689
{
1690
1690
// Validate arguments as would the base implementation
1691
- if ( destination == null ) throw new ArgumentNullException ( nameof ( destination ) ) ;
1692
- if ( bufferSize <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) , SR . ArgumentOutOfRange_NeedPosNum ) ;
1691
+ if ( destination == null )
1692
+ {
1693
+ throw new ArgumentNullException ( nameof ( destination ) ) ;
1694
+ }
1695
+ if ( bufferSize <= 0 )
1696
+ {
1697
+ throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) , SR . ArgumentOutOfRange_NeedPosNum ) ;
1698
+ }
1693
1699
bool parentCanRead = _parent . CanRead ;
1694
- if ( ! parentCanRead && ! _parent . CanWrite ) throw new ObjectDisposedException ( null , SR . ObjectDisposed_StreamClosed ) ;
1700
+ if ( ! parentCanRead && ! _parent . CanWrite )
1701
+ {
1702
+ throw new ObjectDisposedException ( null , SR . ObjectDisposed_StreamClosed ) ;
1703
+ }
1695
1704
bool destinationCanWrite = destination . CanWrite ;
1696
- if ( ! destination . CanRead && ! destinationCanWrite ) throw new ObjectDisposedException ( nameof ( destination ) , SR . ObjectDisposed_StreamClosed ) ;
1697
- if ( ! parentCanRead ) throw new NotSupportedException ( SR . NotSupported_UnreadableStream ) ;
1698
- if ( ! destinationCanWrite ) throw new NotSupportedException ( SR . NotSupported_UnwritableStream ) ;
1699
- if ( _handle . IsClosed ) throw Error . GetFileNotOpen ( ) ;
1705
+ if ( ! destination . CanRead && ! destinationCanWrite )
1706
+ {
1707
+ throw new ObjectDisposedException ( nameof ( destination ) , SR . ObjectDisposed_StreamClosed ) ;
1708
+ }
1709
+ if ( ! parentCanRead )
1710
+ {
1711
+ throw new NotSupportedException ( SR . NotSupported_UnreadableStream ) ;
1712
+ }
1713
+ if ( ! destinationCanWrite )
1714
+ {
1715
+ throw new NotSupportedException ( SR . NotSupported_UnwritableStream ) ;
1716
+ }
1700
1717
1701
1718
// Bail early for cancellation if cancellation has been requested
1702
1719
if ( cancellationToken . IsCancellationRequested )
1703
1720
{
1704
1721
return Task . FromCanceled < int > ( cancellationToken ) ;
1705
1722
}
1706
1723
1724
+ // Fail if the file was closed
1725
+ if ( _handle . IsClosed )
1726
+ {
1727
+ throw Error . GetFileNotOpen ( ) ;
1728
+ }
1729
+
1707
1730
// Do the async copy, with differing implementations based on whether the FileStream was opened as async or sync
1708
1731
Debug . Assert ( ( _readPos == 0 && _readLen == 0 && _writePos >= 0 ) || ( _writePos == 0 && _readPos <= _readLen ) , "We're either reading or writing, but not both." ) ;
1709
1732
return _isAsync ?
@@ -1745,7 +1768,10 @@ private async Task AsyncModeCopyToAsync(Stream destination, int bufferSize, Canc
1745
1768
bool canSeek = _parent . CanSeek ;
1746
1769
if ( canSeek )
1747
1770
{
1748
- if ( _exposedHandle ) VerifyOSHandlePosition ( ) ;
1771
+ if ( _exposedHandle )
1772
+ {
1773
+ VerifyOSHandlePosition ( ) ;
1774
+ }
1749
1775
readAwaitable . _position = _pos ;
1750
1776
}
1751
1777
0 commit comments