/
DownloadWorker.cs
77 lines (66 loc) · 2.36 KB
/
DownloadWorker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Glacierizer
{
class DownloadWorker
{
private PWGlacierAPI api;
private string jobId;
private long start;
private long end;
private BufferBucket bucket;
private TransferMetric metric;
public DownloadWorker(ref PWGlacierAPI api, string jobId, long start, long end, ref BufferBucket bucket, ref TransferMetric metric)
{
this.api = api;
this.jobId = jobId;
this.start = start;
this.end = end;
this.bucket = bucket;
this.metric = metric;
}
public void Download()
{
int retries = 0;
while (retries++ < 3)
{
try
{
ArchivePartInfo info = api.DownloadArchivePart(jobId, start, end);
Stream stream = info.Stream();
List<byte> dynamicData = new List<byte>();
int data;
long bytesRead = 0;
while ((data = stream.ReadByte()) != -1)
{
bytesRead = metric.incrementTransferredBytes(1);
dynamicData.Add((byte)data);
}
bucket.Initialize(dynamicData.Count);
dynamicData.CopyTo(bucket.Data());
using (Stream hashStream = new MemoryStream(bucket.Data(), 0, dynamicData.Count))
{
string checksum = Amazon.Glacier.TreeHashGenerator.CalculateTreeHash(hashStream);
if (checksum != info.Checksum())
{
metric.removeTransferredBytes(bytesRead);
throw new Exception("Checksum mismatch");
}
bucket.Checksum(checksum);
bucket.IsReady(true);
metric.incrementTransferredParts();
}
return;
}
catch (Exception e)
{
Console.WriteLine("Downloader Exception: " + e.Message);
}
}
Console.WriteLine("Download failed");
}
}
}