-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncMultiFileReader.cs
134 lines (115 loc) · 4 KB
/
AsyncMultiFileReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
namespace DropboxEncrypedUploader
{
public class AsyncMultiFileReader : IDisposable
{
public byte[] CurrentBuffer { get; private set; }
public (string Name, object Tag) NextFile { get; set; }
public (string Name, object Tag) CurrentFile { get; private set; }
readonly Func<string, object, Stream> _asyncFileStreamFactory;
byte[] _buffer1;
byte[] _buffer2;
Stream _stream;
Task<(int read, Stream stream, string fileName, object tag)> _preOpenedFile;
Task<int> _latestReadBytesCount;
public AsyncMultiFileReader(int bufferSize, Func<string, object, Stream> asyncFileStreamFactory = null)
{
_asyncFileStreamFactory = asyncFileStreamFactory
?? ((fileName, _) => new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, _buffer1.Length, true));
_buffer1 = new byte[bufferSize];
_buffer2 = new byte[bufferSize];
CurrentBuffer = _buffer1;
}
(int read, Stream stream, string fileName, object tag) OpenAndRead(string fileName, object tag, byte[] buffer)
{
Stream f = null;
try
{
f = _asyncFileStreamFactory(fileName, tag);
return (f.Read(buffer, 0, buffer.Length), f, fileName, tag);
}
catch
{
f?.Dispose();
throw;
}
}
public int ReadNextBlock()
{
if (_stream == null) return 0;
int read = _latestReadBytesCount.Result;
if (_stream.Position < _stream.Length)
{
_latestReadBytesCount = _stream.ReadAsync(_buffer2, 0, _buffer2.Length);
}
else
{
_latestReadBytesCount = null;
// in threadpool thread we shouldn't block for another threadpool task
if ((NextFile.Name != null) && !Thread.CurrentThread.IsThreadPoolThread)
{
var fn2 = NextFile;
var b2 = _buffer2;
_preOpenedFile = Task.Run(() => OpenAndRead(fn2.Name, fn2.Tag, b2));
}
_stream.Dispose();
_stream = null;
}
CurrentBuffer = _buffer1;
var swap = _buffer2;
_buffer2 = _buffer1;
_buffer1 = swap;
return read;
}
/// <summary>
///
/// </summary>
/// <exception cref="AggregateException"></exception>
/// <exception cref="IOException"></exception>
public void OpenNextFile()
{
if (_stream != null)
{
_stream.Dispose();
_stream = null;
}
CurrentFile = (null, null);
_latestReadBytesCount = null;
try
{
int read;
string opened;
object openedTag;
var nextFile = NextFile;
(read, _stream, opened, openedTag) = _preOpenedFile?.Result ?? OpenAndRead(nextFile.Name, nextFile.Tag, _buffer1);
if ((opened != nextFile.Name) || (openedTag != nextFile.Tag))
{
_stream.Dispose();
(read, _stream, _, _) = OpenAndRead(nextFile.Name, nextFile.Tag, _buffer1);
}
CurrentFile = nextFile;;
_latestReadBytesCount = Task.FromResult(read);
}
finally
{
NextFile = (null, null);
_preOpenedFile = null;
}
}
public void Dispose()
{
_stream?.Dispose();
try
{
_preOpenedFile?.Result.stream.Dispose();
}
catch
{
}
}
}
}