-
Notifications
You must be signed in to change notification settings - Fork 4
/
BufferedStringReader.cs
119 lines (107 loc) · 2.5 KB
/
BufferedStringReader.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
using System;
using System.IO;
using System.Threading;
namespace PacketLogConverter
{
public class BufferedStringReader : IDisposable
{
private volatile Thread m_thread;
private volatile int m_firstUncached;
private volatile int m_currentLine;
private readonly Stream m_stream;
private readonly string[] m_lines;
private readonly int m_indexMask;
private readonly AutoResetEvent m_event;
public BufferedStringReader(Stream stream, int bufferSizeBits)
{
if (stream == null)
throw new ArgumentNullException("stream");
m_currentLine = -1;
m_stream = stream;
m_indexMask = (1 << bufferSizeBits) - 1;
m_lines = new string[1 << bufferSizeBits];
m_event = new AutoResetEvent(false);
StartThread();
}
private void StartThread()
{
m_thread = new Thread(new ThreadStart(BufferingThread));
m_thread.IsBackground = true;
m_thread.Name = "StringCache";
m_thread.Start();
}
private void BufferingThread()
{
try
{
StreamReader s = new StreamReader(m_stream);
{
do
{
int lastLine = m_firstUncached;
int count = m_lines.Length - (lastLine - m_currentLine);
if (count <= 0)
{
Thread.Sleep(1);
// m_event.WaitOne();
continue;
}
for (int i = 0; i < count; i++)
{
string line = s.ReadLine();
if (line == null)
{
m_thread = null;
m_firstUncached = lastLine + i;
return;
}
m_lines[(lastLine + i) & m_indexMask] = line;
// m_event.Set();
}
m_firstUncached = lastLine + count;
} while (true);
}
}
catch (ThreadAbortException)
{
}
}
public static int moveNextSpin;
public bool MoveNext()
{
int cur = m_currentLine + 1;
while (cur >= m_firstUncached)
{
if (m_thread == null)
return false;
int start = Environment.TickCount;
// m_event.Set();
// m_event.WaitOne();
Thread.Sleep(1);
// Thread.SpinWait(1);
moveNextSpin += Environment.TickCount - start;
}
m_currentLine = cur;
return true;
}
public string Current
{
get { return m_lines[m_currentLine & m_indexMask]; }
}
protected virtual void Dispose(bool disposing)
{
if (m_thread != null)
m_thread.Abort();
if (disposing)
GC.SuppressFinalize(this);
}
public void Dispose()
{
Dispose(true);
}
~BufferedStringReader()
{
Dispose(false);
}
}
}