@@ -41,18 +41,12 @@ namespace System.IO
41
41
internal sealed partial class Win32FileStream : FileStreamBase
42
42
{
43
43
internal const int DefaultBufferSize = 4096 ;
44
- #if USE_OVERLAPPED
45
44
internal const bool DefaultUseAsync = true ;
46
- #else
47
- internal const bool DefaultUseAsync = false ;
48
- #endif
49
45
internal const bool DefaultIsAsync = false ;
50
46
51
47
private byte [ ] _buffer ; // Shared read/write buffer. Alloc on first use.
52
48
private String _fileName ; // Fully qualified file name.
53
- #if USE_OVERLAPPED
54
49
private bool _isAsync ; // Whether we opened the handle for overlapped IO
55
- #endif
56
50
private bool _canRead ;
57
51
private bool _canWrite ;
58
52
private bool _canSeek ;
@@ -95,13 +89,8 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
95
89
if ( mode == FileMode . Append )
96
90
mode = FileMode . OpenOrCreate ;
97
91
98
- #if USE_OVERLAPPED
99
92
if ( ( options & FileOptions . Asynchronous ) != 0 )
100
93
_isAsync = true ;
101
- #else
102
- // Disallow overlapped IO.
103
- options &= ~ FileOptions . Asynchronous ;
104
- #endif
105
94
106
95
int flagsAndAttributes = ( int ) options ;
107
96
@@ -115,9 +104,7 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
115
104
try
116
105
{
117
106
_handle = Interop . mincore . SafeCreateFile ( path , fAccess , share , ref secAttrs , mode , flagsAndAttributes , IntPtr . Zero ) ;
118
- #if USE_OVERLAPPED
119
107
_handle . IsAsync = _isAsync ;
120
- #endif
121
108
122
109
if ( _handle . IsInvalid )
123
110
{
@@ -150,7 +137,6 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
150
137
throw new NotSupportedException ( SR . NotSupported_FileStreamOnNonFiles ) ;
151
138
}
152
139
153
- #if USE_OVERLAPPED
154
140
// This is necessary for async IO using IO Completion ports via our
155
141
// managed Threadpool API's. This (theoretically) calls the OS's
156
142
// BindIoCompletionCallback method, and passes in a stub for the
@@ -179,7 +165,6 @@ private void Init(String path, FileMode mode, FileAccess access, FileShare share
179
165
}
180
166
}
181
167
}
182
- #endif
183
168
184
169
_canRead = ( access & FileAccess . Read ) != 0 ;
185
170
_canWrite = ( access & FileAccess . Write ) != 0 ;
@@ -240,9 +225,7 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
240
225
int handleType = Interop . mincore . GetFileType ( _handle ) ;
241
226
Debug . Assert ( handleType == Interop . mincore . FileTypes . FILE_TYPE_DISK || handleType == Interop . mincore . FileTypes . FILE_TYPE_PIPE || handleType == Interop . mincore . FileTypes . FILE_TYPE_CHAR , "FileStream was passed an unknown file type!" ) ;
242
227
243
- #if USE_OVERLAPPED
244
228
_isAsync = isAsync ;
245
- #endif
246
229
_canRead = 0 != ( access & FileAccess . Read ) ;
247
230
_canWrite = 0 != ( access & FileAccess . Write ) ;
248
231
_canSeek = handleType == Interop . mincore . FileTypes . FILE_TYPE_DISK ;
@@ -253,7 +236,6 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
253
236
_fileName = null ;
254
237
_isPipe = handleType == Interop . mincore . FileTypes . FILE_TYPE_PIPE ;
255
238
256
- #if USE_OVERLAPPED
257
239
// This is necessary for async IO using IO Completion ports via our
258
240
// managed Threadpool API's. This calls the OS's
259
241
// BindIoCompletionCallback method, and passes in a stub for the
@@ -280,23 +262,11 @@ private Win32FileStream(SafeFileHandle handle, FileAccess access, int bufferSize
280
262
}
281
263
}
282
264
else if ( ! _isAsync )
283
- #endif
284
265
{
285
266
if ( handleType != Interop . mincore . FileTypes . FILE_TYPE_PIPE )
286
267
VerifyHandleIsSync ( ) ;
287
268
}
288
269
289
- #if ! USE_OVERLAPPED
290
- // When USE_OVERLAPPED is not supported we ignore the isAsync property and always call
291
- // VerifyHandleIsSync above. We'll then throw here if we were told it was Async.
292
-
293
- if ( isAsync )
294
- {
295
- // Passed in a synchronous handle and told us isAsync
296
- throw new ArgumentException ( SR . Arg_HandleNotAsync , "handle" ) ;
297
- }
298
- #endif
299
-
300
270
if ( _canSeek )
301
271
SeekCore ( 0 , SeekOrigin . Current ) ;
302
272
else
@@ -385,11 +355,7 @@ public override bool CanSeek
385
355
386
356
public override bool IsAsync
387
357
{
388
- #if USE_OVERLAPPED
389
358
get { return _isAsync ; }
390
- #else
391
- get { return false ; }
392
- #endif
393
359
}
394
360
395
361
public override long Length
@@ -483,10 +449,8 @@ protected override void Dispose(bool disposing)
483
449
if ( _handle != null && ! _handle . IsClosed )
484
450
_handle . Dispose ( ) ;
485
451
486
- #if USE_OVERLAPPED
487
452
if ( _handle . ThreadPoolBinding != null )
488
453
_handle . ThreadPoolBinding . Dispose ( ) ;
489
- #endif
490
454
491
455
_canRead = false ;
492
456
_canWrite = false ;
@@ -563,7 +527,6 @@ private void FlushWrite(bool calledFromFinalizer)
563
527
{
564
528
Debug . Assert ( _readPos == 0 && _readLen == 0 , "FileStream: Read buffer must be empty in FlushWrite!" ) ;
565
529
566
- #if USE_OVERLAPPED
567
530
if ( _isAsync )
568
531
{
569
532
Task writeTask = WriteInternalCoreAsync ( _buffer , 0 , _writePos , CancellationToken . None ) ;
@@ -586,7 +549,6 @@ private void FlushWrite(bool calledFromFinalizer)
586
549
}
587
550
}
588
551
else
589
- #endif
590
552
{
591
553
WriteCore ( _buffer , 0 , _writePos ) ;
592
554
}
@@ -755,23 +717,18 @@ private unsafe int ReadCore(byte[] buffer, int offset, int count)
755
717
Debug . Assert ( _writePos == 0 , "_writePos == 0" ) ;
756
718
Debug . Assert ( offset >= 0 , "offset is negative" ) ;
757
719
Debug . Assert ( count >= 0 , "count is negative" ) ;
758
- #if USE_OVERLAPPED
759
720
if ( _isAsync )
760
721
{
761
722
return ReadInternalCoreAsync ( buffer , offset , count , 0 , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ;
762
723
}
763
- #endif
764
724
765
725
// Make sure we are reading from the right spot
766
726
if ( _exposedHandle )
767
727
VerifyOSHandlePosition ( ) ;
768
728
769
729
int errorCode = 0 ;
770
- #if USE_OVERLAPPED
771
730
int r = ReadFileNative ( _handle , buffer , offset , count , null , out errorCode ) ;
772
- #else
773
- int r = ReadFileNative ( _handle , buffer , offset , count , out errorCode ) ;
774
- #endif
731
+
775
732
if ( r == - 1 )
776
733
{
777
734
// For pipes, ERROR_BROKEN_PIPE is the normal end of the pipe.
@@ -977,13 +934,11 @@ public override void Write(byte[] array, int offset, int count)
977
934
// Reset our buffer. We essentially want to call FlushWrite
978
935
// without calling Flush on the underlying Stream.
979
936
980
- #if USE_OVERLAPPED
981
937
if ( _isAsync )
982
938
{
983
939
WriteInternalCoreAsync ( _buffer , 0 , _writePos , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ;
984
940
}
985
941
else
986
- #endif
987
942
{
988
943
WriteCore ( _buffer , 0 , _writePos ) ;
989
944
}
@@ -1015,24 +970,18 @@ private unsafe void WriteCore(byte[] buffer, int offset, int count)
1015
970
Debug . Assert ( _readPos == _readLen , "_readPos == _readLen" ) ;
1016
971
Debug . Assert ( offset >= 0 , "offset is negative" ) ;
1017
972
Debug . Assert ( count >= 0 , "count is negative" ) ;
1018
- #if USE_OVERLAPPED
1019
973
if ( _isAsync )
1020
974
{
1021
975
WriteInternalCoreAsync ( buffer , offset , count , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ;
1022
976
return ;
1023
977
}
1024
- #endif
1025
978
1026
979
// Make sure we are writing to the position that we think we are
1027
980
if ( _exposedHandle )
1028
981
VerifyOSHandlePosition ( ) ;
1029
982
1030
983
int errorCode = 0 ;
1031
- #if USE_OVERLAPPED
1032
984
int r = WriteFileNative ( _handle , buffer , offset , count , null , out errorCode ) ;
1033
- #else
1034
- int r = WriteFileNative ( _handle , buffer , offset , count , out errorCode ) ;
1035
- #endif
1036
985
1037
986
if ( r == - 1 )
1038
987
{
@@ -1057,7 +1006,6 @@ private unsafe void WriteCore(byte[] buffer, int offset, int count)
1057
1006
return ;
1058
1007
}
1059
1008
1060
- #if USE_OVERLAPPED
1061
1009
[ System . Security . SecuritySafeCritical ] // auto-generated
1062
1010
private Task < int > ReadInternalAsync ( byte [ ] array , int offset , int numBytes , CancellationToken cancellationToken )
1063
1011
{
@@ -1278,7 +1226,6 @@ unsafe private Task<int> ReadInternalCoreAsync(byte[] bytes, int offset, int num
1278
1226
1279
1227
return completionSource . Task ;
1280
1228
}
1281
- #endif
1282
1229
1283
1230
// Reads a byte from the file stream. Returns the byte cast to an int
1284
1231
// or -1 if reading from the end of the stream.
@@ -1304,7 +1251,6 @@ public override int ReadByte()
1304
1251
return result ;
1305
1252
}
1306
1253
1307
- #if USE_OVERLAPPED
1308
1254
[ System . Security . SecuritySafeCritical ] // auto-generated
1309
1255
private Task WriteInternalAsync ( byte [ ] array , int offset , int numBytes , CancellationToken cancellationToken )
1310
1256
{
@@ -1469,7 +1415,6 @@ private unsafe Task WriteInternalCoreAsync(byte[] bytes, int offset, int numByte
1469
1415
1470
1416
return completionSource . Task ;
1471
1417
}
1472
- #endif
1473
1418
1474
1419
[ System . Security . SecuritySafeCritical ] // auto-generated
1475
1420
public override void WriteByte ( byte value )
@@ -1513,11 +1458,7 @@ public override void WriteByte(byte value)
1513
1458
1514
1459
// __ConsoleStream also uses this code.
1515
1460
[ System . Security . SecurityCritical ] // auto-generated
1516
- #if USE_OVERLAPPED
1517
1461
private unsafe int ReadFileNative ( SafeFileHandle handle , byte [ ] bytes , int offset , int count , NativeOverlapped * overlapped , out int errorCode )
1518
- #else
1519
- private unsafe int ReadFileNative ( SafeFileHandle handle , byte [ ] bytes , int offset , int count , out int errorCode )
1520
- #endif
1521
1462
{
1522
1463
Contract . Requires ( handle != null , "handle != null" ) ;
1523
1464
Contract . Requires ( offset >= 0 , "offset >= 0" ) ;
@@ -1529,9 +1470,7 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
1529
1470
throw new IndexOutOfRangeException ( SR . IndexOutOfRange_IORaceCondition ) ;
1530
1471
Contract . EndContractBlock ( ) ;
1531
1472
1532
- #if USE_OVERLAPPED
1533
1473
Debug . Assert ( ( _isAsync && overlapped != null ) || ( ! _isAsync && overlapped == null ) , "Async IO and overlapped parameters inconsistent in call to ReadFileNative." ) ;
1534
- #endif
1535
1474
1536
1475
// You can't use the fixed statement on an array of length 0.
1537
1476
if ( bytes . Length == 0 )
@@ -1545,12 +1484,10 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
1545
1484
1546
1485
fixed ( byte * p = bytes )
1547
1486
{
1548
- #if USE_OVERLAPPED
1549
1487
if ( _isAsync )
1550
1488
r = Interop . mincore . ReadFile ( handle , p + offset , count , IntPtr . Zero , overlapped ) ;
1551
1489
else
1552
- #endif
1553
- r = Interop . mincore . ReadFile ( handle , p + offset , count , out numBytesRead , IntPtr . Zero ) ;
1490
+ r = Interop . mincore . ReadFile ( handle , p + offset , count , out numBytesRead , IntPtr . Zero ) ;
1554
1491
}
1555
1492
1556
1493
if ( r == 0 )
@@ -1566,13 +1503,8 @@ private unsafe int ReadFileNative(SafeFileHandle handle, byte[] bytes, int offse
1566
1503
}
1567
1504
1568
1505
[ System . Security . SecurityCritical ] // auto-generated
1569
- #if USE_OVERLAPPED
1570
1506
private unsafe int WriteFileNative ( SafeFileHandle handle , byte [ ] bytes , int offset , int count , NativeOverlapped * overlapped , out int errorCode )
1571
1507
{
1572
- #else
1573
- private unsafe int WriteFileNative ( SafeFileHandle handle , byte [ ] bytes , int offset , int count , out int errorCode )
1574
- {
1575
- #endif
1576
1508
Contract . Requires ( handle != null , "handle != null" ) ;
1577
1509
Contract . Requires ( offset >= 0 , "offset >= 0" ) ;
1578
1510
Contract . Requires ( count >= 0 , "count >= 0" ) ;
@@ -1585,9 +1517,7 @@ private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offs
1585
1517
throw new IndexOutOfRangeException ( SR . IndexOutOfRange_IORaceCondition ) ;
1586
1518
Contract . EndContractBlock ( ) ;
1587
1519
1588
- #if USE_OVERLAPPED
1589
1520
Debug . Assert ( ( _isAsync && overlapped != null ) || ( ! _isAsync && overlapped == null ) , "Async IO and overlapped parameters inconsistent in call to WriteFileNative." ) ;
1590
- #endif
1591
1521
1592
1522
// You can't use the fixed statement on an array of length 0.
1593
1523
if ( bytes . Length == 0 )
@@ -1601,12 +1531,10 @@ private unsafe int WriteFileNative(SafeFileHandle handle, byte[] bytes, int offs
1601
1531
1602
1532
fixed ( byte * p = bytes )
1603
1533
{
1604
- #if USE_OVERLAPPED
1605
1534
if ( _isAsync )
1606
1535
r = Interop . mincore . WriteFile ( handle , p + offset , count , IntPtr . Zero , overlapped ) ;
1607
1536
else
1608
- #endif
1609
- r = Interop . mincore . WriteFile ( handle , p + offset , count , out numBytesWritten , IntPtr . Zero ) ;
1537
+ r = Interop . mincore . WriteFile ( handle , p + offset , count , out numBytesWritten , IntPtr . Zero ) ;
1610
1538
}
1611
1539
1612
1540
if ( r == 0 )
@@ -1663,20 +1591,16 @@ public override Task<int> ReadAsync(Byte[] buffer, int offset, int count, Cancel
1663
1591
if ( _handle . IsClosed )
1664
1592
throw __Error . GetFileNotOpen ( ) ;
1665
1593
1666
- #if USE_OVERLAPPED
1667
1594
// If async IO is not supported on this platform or
1668
1595
// if this Win32FileStream was not opened with FileOptions.Asynchronous.
1669
1596
if ( ! _isAsync )
1670
- #endif
1671
1597
{
1672
1598
return base . ReadAsync ( buffer , offset , count , cancellationToken ) ;
1673
1599
}
1674
- #if USE_OVERLAPPED
1675
1600
else
1676
1601
{
1677
1602
return ReadInternalAsync ( buffer , offset , count , cancellationToken ) ;
1678
1603
}
1679
- #endif
1680
1604
}
1681
1605
1682
1606
[ System . Security . SecuritySafeCritical ]
@@ -1688,20 +1612,16 @@ public override Task WriteAsync(Byte[] buffer, int offset, int count, Cancellati
1688
1612
if ( _handle . IsClosed )
1689
1613
throw __Error . GetFileNotOpen ( ) ;
1690
1614
1691
- #if USE_OVERLAPPED
1692
1615
// If async IO is not supported on this platform or
1693
1616
// if this Win32FileStream was not opened with FileOptions.Asynchronous.
1694
1617
if ( ! _isAsync )
1695
- #endif
1696
1618
{
1697
1619
return base . WriteAsync ( buffer , offset , count , cancellationToken ) ;
1698
1620
}
1699
- #if USE_OVERLAPPED
1700
1621
else
1701
1622
{
1702
1623
return WriteInternalAsync ( buffer , offset , count , cancellationToken ) ;
1703
1624
}
1704
- #endif
1705
1625
}
1706
1626
1707
1627
// Unlike Flush(), FlushAsync() always flushes to disk. This is intentional.
0 commit comments