Beispiel #1
0
        public void ReadBlocks(int numCurrentBigBlock, int numberOfBlocks, int[] blocksLength = null)
        {
            long currentBlockToSkip = numCurrentBigBlock * Constants.BigBlockCount;
            long currentPosition    = 0;

            for (var i = 0; i < currentBlockToSkip; i++)
            {
                currentPosition += blocksLength?[i] ?? Constants.BlockLength;
            }

            _blocks = new byte[numberOfBlocks][];
            var threads = new Thread[numberOfBlocks];

            for (long i = 0; i < numberOfBlocks; i++)
            {
                var reader = new BlockReader(_sourceFile, currentPosition);
                reader.ReadedEvent += (sender, args) =>
                {
                    _blocks[args.IndexOfArray] = args.DataProcessed;
                };
                var lastBlockLength = (int)(numCurrentBigBlock == _countBigBlocks && i == numberOfBlocks - 1
                    ? _totalLength % Constants.BlockLength
                    : 0);
                threads[i]       = reader.ReadBlock(new Data(null, Work.Zip, (int)i, blocksLength?[i + currentBlockToSkip] ?? lastBlockLength));
                currentPosition += blocksLength?[i + currentBlockToSkip] ?? Constants.BlockLength;
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
            ReadedEvent?.Invoke(this, new ReadDataProcessEventArgs(_blocks, numCurrentBigBlock));
        }
Beispiel #2
0
        private void ReadBlockForLength(object inputObject)
        {
            SemRead.WaitOne();
            var dataInput = (Data)inputObject;
            var index     = dataInput.ArrayIndex;
            var length    = dataInput.Length == 0 ? (int)Constants.BlockLength : dataInput.Length;
            var block     = new byte[length];

            using (var sourceStream = new FileStream(_sourceFile, FileMode.Open, FileAccess.Read,
                                                     FileShare.Read, (int)Constants.BlockLength, true))

            {
                sourceStream.Seek(_startPosition, SeekOrigin.Begin);
                sourceStream.Read(block, 0, length);
                ReadedEvent?.Invoke(this, new DataProcessEventArgs(block, index));
            }
            SemRead.Release();
        }