@@ -17,22 +17,24 @@ namespace AsyncToSyncCodeRoundtripSynchroniserMonitor
17
17
{
18
18
public static partial class FileExtensions
19
19
{
20
+ public static int MaxByteArraySize = 0x7FFFFFC7 ; //https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element?redirectedfrom=MSDN#remarks
21
+
20
22
//https://stackoverflow.com/questions/18472867/checking-equality-for-two-byte-arrays/
21
23
public static bool BinaryEqual ( Binary a , Binary b )
22
24
{
23
25
return a . Equals ( b ) ;
24
26
}
25
27
26
- public static async Task < byte [ ] > ReadAllBytesAsync ( string path , CancellationToken cancellationToken = default ( CancellationToken ) )
28
+ public static async Task < Tuple < byte [ ] , long > > ReadAllBytesAsync ( string path , CancellationToken cancellationToken = default ( CancellationToken ) , long maxFileSize = 0 )
27
29
{
28
30
while ( true )
29
31
{
30
32
if ( cancellationToken . IsCancellationRequested )
31
- return await Task . FromCanceled < byte [ ] > ( cancellationToken ) ;
33
+ return await Task . FromCanceled < Tuple < byte [ ] , long > > ( cancellationToken ) ;
32
34
33
35
try
34
36
{
35
- using ( FileStream stream = new FileStream (
37
+ using ( var stream = new FileStream (
36
38
path ,
37
39
FileMode . Open ,
38
40
FileAccess . Read ,
@@ -41,10 +43,17 @@ public static bool BinaryEqual(Binary a, Binary b)
41
43
useAsync : true
42
44
) )
43
45
{
44
- var len = ( int ) stream . Length ; //NB! the lenght might change during the code execution, so need to save it into separate variable
46
+ long len = stream . Length ; //NB! the length might change during the code execution, so need to save it into separate variable
47
+
48
+ maxFileSize = Math . Min ( MaxByteArraySize , maxFileSize ) ;
49
+ if ( maxFileSize > 0 && len > maxFileSize )
50
+ {
51
+ return new Tuple < byte [ ] , long > ( null , len ) ;
52
+ }
53
+
45
54
byte [ ] result = new byte [ len ] ;
46
- await stream . ReadAsync ( result , 0 , len , cancellationToken ) ;
47
- return result ;
55
+ await stream . ReadAsync ( result , 0 , ( int ) len , cancellationToken ) ;
56
+ return new Tuple < byte [ ] , long > ( result , len ) ;
48
57
}
49
58
}
50
59
catch ( IOException )
@@ -61,21 +70,21 @@ public static bool BinaryEqual(Binary a, Binary b)
61
70
catch ( TaskCanceledException )
62
71
{
63
72
//do nothing here
64
- return await Task . FromCanceled < byte [ ] > ( cancellationToken ) ;
73
+ return await Task . FromCanceled < Tuple < byte [ ] , long > > ( cancellationToken ) ;
65
74
}
66
75
}
67
76
}
68
77
}
69
78
70
- public static async Task WriteAllBytesAsync ( string path , byte [ ] contents , CancellationToken cancellationToken = default ( CancellationToken ) )
79
+ public static async Task WriteAllBytesAsync ( string path , byte [ ] contents , CancellationToken cancellationToken = default ( CancellationToken ) , int writeBufferKB = 0 , int bufferWriteDelayMs = 0 )
71
80
{
72
81
while ( true )
73
82
{
74
83
cancellationToken . ThrowIfCancellationRequested ( ) ;
75
84
76
85
try
77
86
{
78
- using ( FileStream stream = new FileStream (
87
+ using ( var stream = new FileStream (
79
88
path ,
80
89
FileMode . OpenOrCreate ,
81
90
FileAccess . Write ,
@@ -84,7 +93,24 @@ public static bool BinaryEqual(Binary a, Binary b)
84
93
useAsync : true
85
94
) )
86
95
{
87
- await stream . WriteAsync ( contents , 0 , contents . Length , cancellationToken ) ;
96
+ var writeBufferLength = writeBufferKB * 1024 ;
97
+ if ( writeBufferLength <= 0 || bufferWriteDelayMs <= 0 ) //NB! disable write buffer length limit if delay is 0
98
+ writeBufferLength = contents . Length ;
99
+
100
+ for ( int i = 0 ; i < contents . Length ; i += writeBufferLength )
101
+ {
102
+ if ( i > 0 && bufferWriteDelayMs > 0 )
103
+ {
104
+ #if ! NOASYNC
105
+ await Task . Delay ( bufferWriteDelayMs , cancellationToken ) ;
106
+ #else
107
+ cancellationToken . WaitHandle . WaitOne ( bufferWriteDelayMs ) ;
108
+ #endif
109
+ }
110
+
111
+ await stream . WriteAsync ( contents , i , writeBufferLength , cancellationToken ) ;
112
+ }
113
+
88
114
return ;
89
115
}
90
116
}
0 commit comments