Revision cfed7823 trunk/Pithos.Network/TreeHash.cs
b/trunk/Pithos.Network/TreeHash.cs | ||
---|---|---|
1 | 1 |
using System; |
2 | 2 |
using System.Collections.Concurrent; |
3 | 3 |
using System.Collections.Generic; |
4 |
using System.Diagnostics.Contracts; |
|
4 | 5 |
using System.IO; |
5 | 6 |
using System.Text; |
6 | 7 |
using System.Threading.Tasks; |
... | ... | |
16 | 17 |
private const int DEFAULT_BLOCK_SIZE = 4*1024*1024; |
17 | 18 |
public string BlockHash { get; set; } |
18 | 19 |
public int BlockSize { get; set; } |
19 |
public long Bytes { get; set; } |
|
20 |
|
|
21 |
private long _bytes; |
|
22 |
public long Bytes |
|
23 |
{ |
|
24 |
get |
|
25 |
{ |
|
26 |
Contract.Ensures(Contract.Result<long>() >= 0); |
|
27 |
return _bytes; |
|
28 |
} |
|
29 |
set |
|
30 |
{ |
|
31 |
if (value<0) |
|
32 |
throw new ArgumentOutOfRangeException("Bytes"); |
|
33 |
Contract.Requires(value >= 0); |
|
34 |
Contract.EndContractBlock(); |
|
35 |
|
|
36 |
_bytes = value; |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
|
|
20 | 41 |
|
21 | 42 |
public Guid FileId { get; set; } |
22 | 43 |
|
... | ... | |
27 | 48 |
} |
28 | 49 |
|
29 | 50 |
private IList<byte[]> _hashes; |
51 |
|
|
30 | 52 |
public IList<byte[]> Hashes |
31 | 53 |
{ |
32 | 54 |
get { return _hashes; } |
33 | 55 |
set |
34 | 56 |
{ |
35 | 57 |
_hashes = value; |
36 |
_topHash.Force();
|
|
58 |
_topHash.Force(); |
|
37 | 59 |
} |
38 | 60 |
} |
39 | 61 |
|
40 |
public TreeHash(string algorithm) |
|
62 |
[ContractInvariantMethod] |
|
63 |
private void Invariants() |
|
64 |
{ |
|
65 |
Contract.Invariant(_bytes>=0); |
|
66 |
} |
|
67 |
|
|
68 |
|
|
69 |
public TreeHash(string algorithm) |
|
41 | 70 |
{ |
42 | 71 |
BlockHash = algorithm; |
43 | 72 |
_topHash = new Lazy<byte[]>(() => |
... | ... | |
104 | 133 |
//Saves the Json representation to a file |
105 | 134 |
public Task Save(string filePath) |
106 | 135 |
{ |
136 |
if (String.IsNullOrWhiteSpace(filePath)) |
|
137 |
throw new ArgumentNullException("filePath"); |
|
138 |
Contract.EndContractBlock(); |
|
139 |
|
|
107 | 140 |
var fileName = FileId.ToString("N"); |
108 | 141 |
var path = Path.Combine(filePath, fileName); |
109 | 142 |
if (!Directory.Exists(filePath)) |
... | ... | |
139 | 172 |
public static TreeHash Parse(string json) |
140 | 173 |
{ |
141 | 174 |
if (String.IsNullOrWhiteSpace(json)) |
142 |
return Empty; |
|
175 |
return Empty;
|
|
143 | 176 |
|
144 | 177 |
var value = JsonConvert.DeserializeObject<JObject>(json); |
178 |
if (value==null) |
|
179 |
throw new ArgumentException("The json parameter doesn't contain any json data","json"); |
|
180 |
Contract.Assume(value!=null); |
|
145 | 181 |
|
146 | 182 |
var blockHash = (string) value["block_hash"]; |
147 | 183 |
var size = value.Value<int>("block_size"); |
Also available in: Unified diff