Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / FileInfoExtensions.cs @ ab2f6f79

History | View | Annotate | Download (2.5 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.Diagnostics.Contracts;
4
using System.Linq;
5
using System.Text;
6
using System.IO;
7
using System.Text.RegularExpressions;
8
using System.Threading.Tasks;
9
using Pithos.Network;
10

    
11
namespace Pithos.Core.Agents
12
{
13
    static class FileInfoExtensions
14
    {
15
        
16

    
17
        public static int Read(this FileInfo fileInfo,byte[] buffer,int offset,int count)
18
        {            
19
            //Open the stream only long enough to read a block
20
            using (var stream = fileInfo.OpenRead())
21
            {
22
                stream.Seek(offset, SeekOrigin.Begin);
23
                return  stream.Read(buffer, 0, count);                
24
            }
25
        }
26

    
27
        public static Task<int> ReadAsync(this FileInfo fileInfo, byte[] buffer, int offset, int count)
28
        {
29
            //The using statement is not used because we will work asyncronously
30
            var stream = FileAsync.OpenRead(fileInfo.FullName);
31
            try
32
            {
33
                stream.Seek(offset, SeekOrigin.Begin);
34
                var read = stream.ReadAsync(buffer, offset, count);
35
                return read.ContinueWith(t =>
36
                {
37
                    //Make sure the stream closes
38
                    stream.Close();
39
                    stream = null;
40
                    
41
                    t.PropagateExceptions();
42

    
43
                    return t.Result;
44
                });
45
            }
46
            catch (Exception)
47
            {
48
                //In case of error make sure we close the stream                
49
                //The stream may have been 
50
                if (stream!=null)
51
                    stream.Close();
52
                throw;
53
            }
54

    
55
        }
56

    
57
        public static string CalculateHash(this FileInfo info,int blockSize,string algorithm)
58
        {
59
            if (info==null)
60
                throw new ArgumentNullException("info");
61
            if (String.IsNullOrWhiteSpace(info.FullName))
62
                throw new ArgumentException("info");
63
            if (blockSize<=0)
64
                throw new ArgumentOutOfRangeException("blockSize",blockSize,"blockSize must be greater than 0");
65
            if (String.IsNullOrWhiteSpace(algorithm))
66
                throw new ArgumentNullException("algorithm");
67
            Contract.EndContractBlock();
68

    
69
            if (info.Length <= blockSize)
70
                return Signature.CalculateMD5(info.FullName);
71
            else
72
                return Signature.CalculateTreeHash(info.FullName, blockSize, algorithm).TopHash.ToHashString();
73

    
74
        }
75

    
76
    }
77
}