/
StreamLineArray.cs
170 lines (137 loc) · 4.48 KB
/
StreamLineArray.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace CC_CEDICT.WindowsPhone
{
public class StreamLineArray<T> : IDisposable, IEnumerable<T> where T : ILine, new()
{
Stream stream;
enum LineEndingLength { Unknown = 0, Unix = 1, DOS = 2 };
LineEndingLength lineEndingLength = LineEndingLength.Unknown;
List<long> offsets = new List<long>();
public StreamLineArray()
{
}
protected bool Read(Stream stream)
{
this.stream = stream;
offsets.Clear();
long done = 0;
int size = 32768;
byte[] data = new byte[size];
while ((size = stream.Read(data, 0, data.Length)) > 0)
{
int i = 0;
int j = System.Array.IndexOf<byte>(data, (byte)'\n') + 1;
if (lineEndingLength == LineEndingLength.Unknown)
lineEndingLength = data[j-2].Equals((byte)'\r') ? LineEndingLength.DOS : LineEndingLength.Unix;
while (j > 0 && j <= size)
{
if (!ProcessHeader(ref data, i, j - i - (int)lineEndingLength))
{
if (offsets.Count == 0)
offsets.Add(i); // add the initial line offset as a special case
offsets.Add(done + j); // general case is to add the end of line as an offset
}
i = j; // move forward in chunk of data
j = System.Array.IndexOf<byte>(data, (byte)'\n', i) + 1;
}
done += size;
}
if (offsets.Count == 0)
return false;
if (offsets[offsets.Count - 1] != done) // newline at EOF is missing
offsets.Add(done + (int)lineEndingLength);
return true;
}
protected virtual bool ProcessHeader(ref byte[] data, int offset, int length)
{
return false;
}
public T this[int index]
{
get
{
if (index < 0 || index > this.Count - 1)
throw new IndexOutOfRangeException();
long offset = offsets[index];
long length = offsets[index + 1] - offset - (int)lineEndingLength;
byte[] data = new byte[length];
stream.Seek(offset, SeekOrigin.Begin);
stream.Read(data, 0, data.Length);
T line = new T();
line.Index = index;
line.Initialize(ref data);
return line;
}
}
public int Count
{
get
{
int n = offsets.Count - 1;
return (n < 0) ? 0 : n;
}
}
#region IDisposable implementation
public void Dispose()
{
if (stream != null)
stream.Close();
}
#endregion
#region IEnumerable implementation
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator<byte[]>)GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new LineEnumerator(this);
}
public class LineEnumerator : IEnumerator<T>
{
private StreamLineArray<T> reader;
private int index = -1;
public LineEnumerator(StreamLineArray<T> reader)
{
this.reader = reader;
}
public bool MoveNext()
{
return (++index < reader.Count);
}
public void Reset()
{
index = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public T Current
{
get
{
try
{
return reader[index];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public void Dispose()
{
// TODO: should there be some implementation here?
}
}
#endregion
}
}