forked from JMS-1/DVB.NET---VCR.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoubleBufferedFile.cs
More file actions
299 lines (255 loc) · 9.77 KB
/
DoubleBufferedFile.cs
File metadata and controls
299 lines (255 loc) · 9.77 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.IO;
namespace JMS.DVB.TS
{
/// <summary>
/// Helper class to write data to a <see cref="File"/> using double buffering
/// technology and asynchronous writes.
/// </summary>
public class DoubleBufferedFile : IDisposable
{
/// <summary>
/// The <see cref="File"/> we write the data to.
/// </summary>
private FileStream m_File = null;
/// <summary>
/// First buffer.
/// <seealso cref="m_Buf2"/>
/// </summary>
private byte[] m_Buf1 = null;
/// <summary>
/// Second buffer.
/// <seealso cref="m_Buf1"/>
/// </summary>
private byte[] m_Buf2 = null;
/// <summary>
/// Currently active operation
/// </summary>
private IAsyncResult m_Writer = null;
/// <summary>
/// Position in <see cref="m_Buf1"/> where next byte has to go to.
/// </summary>
private int m_Buf1Pos = 0;
/// <summary>
/// Number of times <see cref="FinishDiskWrite"/> has to wait for a disk operation
/// to finish.
/// </summary>
private uint m_Waits = 0;
/// <summary>
/// Die gesamte Anzahl von Bytes, die von <see cref="Write"/> verarbeitet wurden. Dies
/// entspricht etwa der Dateiposition, auch wenn diese durch die Doppelspeicherung
/// evtl. noch nicht beschrieben wurde.
/// </summary>
public long TotalBytesWritten { get; private set; }
/// <summary>
/// Meldet den Namen der zugeordneten Zieldatei.
/// </summary>
public string FilePath { get; private set; }
/// <summary>
/// Allocate the buffers <see cref="m_Buf1"/> and <see cref="m_Buf2"/>
/// and create <see cref="m_File"/> on the indicated <see cref="File"/>.
/// </summary>
/// <param name="sOutFileName">Output <see cref="File"/> - if the file alreay
/// exists it will be overwritten.</param>
/// <param name="nSize">Size of each buffer in bytes.</param>
public DoubleBufferedFile( string sOutFileName, int nSize )
{
// Create buffers
m_Buf1 = new byte[nSize];
m_Buf2 = new byte[nSize];
// Attach to file
m_File = new FileStream( sOutFileName, FileMode.Create, FileAccess.Write, FileShare.Read );
// Remember
FilePath = m_File.Name;
}
/// <summary>
/// Report <see cref="m_Waits"/> synchronized on the current instance.
/// </summary>
public uint DiskWaits
{
get
{
// Report
lock (this)
return m_Waits;
}
}
/// <summary>
/// Finish the current disk operation attached to <see cref="m_Writer"/>.
/// </summary>
/// <remarks>
/// If <see cref="IAsyncResult.IsCompleted"/> is not active and the parameter
/// is set <see cref="m_Waits"/> will be incremented. If necessary
/// <see cref="System.Threading.WaitHandle.WaitOne()"/>
/// on <see cref="IAsyncResult.AsyncWaitHandle"/>
/// is used to synchronize with the disk.
/// </remarks>
/// <param name="bMoreData">The file is not finished yet.</param>
private void FinishDiskWrite( bool bMoreData )
{
// Nothing to do
if (null == m_Writer)
return;
// Must wait
if (!m_Writer.IsCompleted)
{
// Increment indicator
if (bMoreData)
lock (this)
++m_Waits;
// Wait
m_Writer.AsyncWaitHandle.WaitOne();
}
// Finish
if (null != m_File)
m_File.EndWrite( m_Writer );
// Clear
m_Writer = null;
}
/// <summary>
/// It's recommended to call this method after any call to <see cref="StartWrite"/>.
/// </summary>
/// <remarks>
/// A client calls <see cref="StartWrite"/> to allocate some space in one of
/// our buffers. Then the space is filled and the reserved space is finally
/// allocated using this method. The parameter must not be negative and should
/// normally not be greated than the number of bytes requested in <see cref="StartWrite"/>.
/// <seealso cref="StartWrite"/>.
/// </remarks>
/// <param name="nBytes">Bytes used in the allocated area.</param>
public void EndWrite( int nBytes )
{
// Invalid
if ((nBytes < 0) || ((m_Buf1Pos + nBytes) > m_Buf1.Length)) throw new ArgumentOutOfRangeException( "Not enough Space in Buffer" );
// Update index
m_Buf1Pos += nBytes;
}
/// <summary>
/// Buffer management - call this method to allocate some space in one
/// of the buffers.
/// </summary>
/// <remarks>
/// The number of bytes must never be negative. If the number is zero the end
/// of the operation is indicated: all buffers will be written to the disk -
/// the file will be closed in <see cref="Dispose"/>. Else the indicated
/// number of bytes is reserved in the current buffer - if this is not
/// possible because the buffer is full it will be written to disk and
/// the secondary buffer is used.
/// <seealso cref="EndWrite"/>
/// </remarks>
/// <param name="nBytes">Number of bytes to reserve.</param>
/// <param name="nIndex">Return index where to store the data.</param>
/// <returns>Some buffer where to store the data.</returns>
/// <exception cref="ArgumentOutOfRangeException">Number of bytes
/// is negative or exceeds <see cref="Array.Length"/> of <see cref="m_Buf1"/>.
/// </exception>
public byte[] StartWrite( int nBytes, out int nIndex )
{
// Invalid
if ((nBytes < 0) || (nBytes > m_Buf1.Length))
throw new ArgumentOutOfRangeException( "Invalid Buffer Size - only 0 to " + m_Buf1.Length.ToString() + " are allowed" );
// Done
bool bFinish = (0 == nBytes);
// See if this fits into the buffer
if (!bFinish)
if ((m_Buf1Pos + nBytes) <= m_Buf1.Length)
{
// Report index
nIndex = m_Buf1Pos;
// Report buffer
return m_Buf1;
}
// Write out buffers
FinishDiskWrite( !bFinish );
// Start writing
if (m_Buf1Pos > 0)
{
// Start processing
m_Writer = m_File.BeginWrite( m_Buf1, 0, m_Buf1Pos, null, null );
}
// Restart from scratch
m_Buf1Pos = 0;
// Swap buffers
byte[] pSwap = m_Buf1;
m_Buf1 = m_Buf2;
m_Buf2 = pSwap;
// Reset index
nIndex = 0;
// Report
return m_Buf1;
}
/// <summary>
/// Überträgt einen beliebigen Speicherbereich in die angeschlossene Datei.
/// </summary>
/// <param name="buffer">Der Speicherbereich.</param>
/// <param name="offset">Der Index des ersten zu berücksichtigenden Bytes im Speicherbereich.</param>
/// <param name="length">Die Anzahl der zu berücksichtigenden Bytes.</param>
public void Write( byte[] buffer, int offset, int length )
{
// Process
while (length > 0)
{
// See how many bytes can be written
int trans = Math.Min( length, m_Buf1.Length - m_Buf1Pos );
// Copy in
if (trans > 0)
{
// Fill buffer
Array.Copy( buffer, offset, m_Buf1, m_Buf1Pos, trans );
// Adjust
m_Buf1Pos += trans;
// Prepare for next
offset += trans;
length -= trans;
// Advance counter
TotalBytesWritten += trans;
}
// May need to flush
if (m_Buf1Pos >= m_Buf1.Length)
{
// Write out buffers
FinishDiskWrite( true );
// Always start a new write
m_Writer = m_File.BeginWrite( m_Buf1, 0, m_Buf1Pos, null, null );
// Restart from scratch
m_Buf1Pos = 0;
// Swap buffers
byte[] swap = m_Buf1;
m_Buf1 = m_Buf2;
m_Buf2 = swap;
}
}
}
/// <summary>
/// Call <see cref="StartWrite"/> with zero bytes followed by
/// <see cref="FinishDiskWrite"/> to save all data to disk
/// and synchronize with the last asynchronous operation.
/// </summary>
public void Flush()
{
// Helper
int nIndex;
// Force write of last chunk
StartWrite( 0, out nIndex );
// Wait for final data block
FinishDiskWrite( false );
}
/// <summary>
/// Finish all file operations and close file.
/// <seealso cref="Flush"/>
/// </summary>
public void Dispose()
{
// Check mode
if (null != m_File)
{
// Finish all
Flush();
// Close the file
m_File.Close();
// Detach all
m_File = null;
}
}
}
}