/
P2pWriter.cs
75 lines (59 loc) · 2.51 KB
/
P2pWriter.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using IPFilter.Core;
using IPFilter.Models;
namespace IPFilter.Formats
{
/// <summary>
/// Writes out eMule DAT format, 0-padding integers to 3 digits. e.g.<c>"001.009.096.105 - 001.009.096.105 , 000 , Some organization"</c>
/// </summary>
class P2pWriter : IFormatWriter
{
readonly Stream stream;
public void Dispose()
{
stream.Dispose();
}
public P2pWriter(Stream stream)
{
this.stream = stream;
}
public async Task Write(IList<FilterEntry> entries, IProgress<ProgressModel> progress)
{
var sb = new StringBuilder(255);
var address = new StringBuilder(15);
var currentPercentage = -1;
using (Benchmark.New("Writing {0} entries", entries.Count))
using (var writer = new StreamWriter(stream, Encoding.ASCII))
{
for (var i = 1; i <= entries.Count; i++)
{
sb.Clear();
var from = BitConverter.GetBytes(entries[i - 1].From);
address.Clear();
address.Append(from[3].ToString("D3")).Append(".").Append(from[2].ToString("D3")).Append(".").Append(from[1].ToString("D3")).Append(".").Append(from[0].ToString("D3"));
sb.Append(address);
sb.Append(" - ");
var to = BitConverter.GetBytes(entries[i - 1].To);
address.Clear();
address.Append(to[3].ToString("D3")).Append(".").Append(to[2].ToString("D3")).Append(".").Append(to[1].ToString("D3")).Append(".").Append(to[0].ToString("D3"));
sb.Append(address);
sb.Append(" , ").Append(entries[i - 1].Level.ToString("D3").PadLeft(3)).Append(" , ");
sb.Append(entries[i - 1].Description);
writer.WriteLine(sb.ToString());
if (progress == null) continue;
var percent = (int)Math.Floor((double)i / entries.Count * 100);
if (percent > currentPercentage)
{
progress.Report(new ProgressModel(UpdateState.Downloading, "Writing...", percent));
}
currentPercentage = percent;
}
await writer.FlushAsync();
}
}
}
}