Exemplo n.º 1
0
        private static byte[][] ReadLeafs(HashAlgorithm tg, string filePath, long start, long end)
        {
            using (ThreadUtility.EnterBackgroundProcessingMode())
                using (var threadFilePtr = FileStreamFactory.CreateFileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 128 * 1024))
                {
                    var threadFileBlock = new FileBlock(start, end);
                    var LeafSize        = 1024;
                    var DataBlockSize   = LeafSize * 1024;
                    var data            = new byte[LeafSize + 1];

                    var totalLeafs = threadFileBlock.Length / LeafSize + threadFileBlock.Length % LeafSize > 0 ? 1 : 0;

                    var result = new byte[totalLeafs][];

                    threadFilePtr.Position = threadFileBlock.Start;

                    while (threadFilePtr.Position < threadFileBlock.End)
                    {
                        var leafIndex = (int)((threadFilePtr.Position - start) / 1024);

                        var dataBlock = new byte[Math.Min(threadFileBlock.End - threadFilePtr.Position, DataBlockSize)];

                        threadFilePtr.Read(dataBlock, 0, dataBlock.Length); //read block

                        var blockLeafs = dataBlock.Length / LeafSize;

                        int i;
                        for (i = 0; i < blockLeafs; i++)
                        {
                            Buffer.BlockCopy(dataBlock, i * LeafSize, data, 1, LeafSize);

                            tg.Initialize();
                            result[leafIndex++] = tg.ComputeHash(data);
                        }

                        if (i * LeafSize < dataBlock.Length)
                        {
                            data    = new byte[dataBlock.Length - blockLeafs * LeafSize + 1];
                            data[0] = LeafHash;

                            Buffer.BlockCopy(dataBlock, blockLeafs * LeafSize, data, 1, (data.Length - 1));

                            tg.Initialize();
                            result[leafIndex] = tg.ComputeHash(data);

                            data    = new byte[LeafSize + 1];
                            data[0] = LeafHash;
                        }
                    }
                    return(result);
                }
        }
Exemplo n.º 2
0
        void SplitFile()
        {
            long leafsInPart = _leafCount / ThreadCount;

            // check if file is bigger than 1 MB or don't use threads
            if (_filePtr.Length > 1024 * 1024)
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    _fileParts[i] = new FileBlock(leafsInPart * LeafSize * i,
                                                  leafsInPart * LeafSize * (i + 1));
                }
            }

            _fileParts[ThreadCount - 1].End = _filePtr.Length;
        }