using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
using System.IO;
using System.Text;
using System.Threading.Tasks;
private const int DEFAULT_BLOCK_SIZE = 4*1024*1024;
public string BlockHash { get; set; }
public int BlockSize { get; set; }
- public long Bytes { get; set; }
+
+ private long _bytes;
+ public long Bytes
+ {
+ get
+ {
+ Contract.Ensures(Contract.Result<long>() >= 0);
+ return _bytes;
+ }
+ set
+ {
+ if (value<0)
+ throw new ArgumentOutOfRangeException("Bytes");
+ Contract.Requires(value >= 0);
+ Contract.EndContractBlock();
+
+ _bytes = value;
+ }
+ }
+
+
public Guid FileId { get; set; }
}
private IList<byte[]> _hashes;
+
public IList<byte[]> Hashes
{
get { return _hashes; }
set
{
_hashes = value;
- _topHash.Force();
+ _topHash.Force();
}
}
- public TreeHash(string algorithm)
+ [ContractInvariantMethod]
+ private void Invariants()
+ {
+ Contract.Invariant(_bytes>=0);
+ }
+
+
+ public TreeHash(string algorithm)
{
BlockHash = algorithm;
_topHash = new Lazy<byte[]>(() =>
//Saves the Json representation to a file
public Task Save(string filePath)
{
+ if (String.IsNullOrWhiteSpace(filePath))
+ throw new ArgumentNullException("filePath");
+ Contract.EndContractBlock();
+
var fileName = FileId.ToString("N");
var path = Path.Combine(filePath, fileName);
if (!Directory.Exists(filePath))
public static TreeHash Parse(string json)
{
if (String.IsNullOrWhiteSpace(json))
- return Empty;
+ return Empty;
var value = JsonConvert.DeserializeObject<JObject>(json);
+ if (value==null)
+ throw new ArgumentException("The json parameter doesn't contain any json data","json");
+ Contract.Assume(value!=null);
var blockHash = (string) value["block_hash"];
var size = value.Value<int>("block_size");