Exemple #1
0
        public void WritesChunksInTheRightOrder()
        {
            var bytes = new byte[]
            { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                },
                new Chunk {
                    Bytes = new byte[] { 0x77, 0x88, 0x99 }, Index = 2
                },
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0xdd, 0xee, 0xff }, Index = 4
                },
                new Chunk {
                    Bytes = new byte[] { 0xaa, 0xbb, 0xcc }, Index = 3
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 5);

            Assert.Equal(bytes, stream.ToArray());
        }
Exemple #2
0
        public void WritesNoContentForEmptyPipe()
        {
            var pipe   = new PipeMock(new Chunk[0]);
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 0);

            Assert.Equal(0, stream.Length);
        }
Exemple #3
0
        public Task Start(TaskParameters parameters)
        {
            _logger.Write($"Starting task with parameters: {parameters}");

            var cancellationTokenSource = new CancellationTokenSource();
            var inputFile  = File.OpenRead(parameters.SourceFullPath);
            var outputFile = File.Create(parameters.DestinationFullPath);
            var inputPipe  = new Pipe(parameters.MaxElementsInPipe);
            var outputPipe = new Pipe(parameters.MaxElementsInPipe);

            var expectedChunksCount = GetExpectedChunksCount(parameters, inputFile, outputFile);

            var           writer = new ChunksWriter(outputPipe, _logger);
            IChunksReader reader = null;
            IEnumerable <IChunksProcessor> processors = null;

            switch (parameters.Mode)
            {
            case ProcessorMode.Compress:
                reader     = new ChunksReader(inputPipe, parameters.ChunkSize, _logger);
                processors = Enumerable.Range(0, parameters.ParallelismDegree).Select(
                    _ => new ChunksCompressor(inputPipe, outputPipe, _logger));
                break;

            case ProcessorMode.Decompress:
                reader     = new CompressedChunksReader(inputPipe, parameters.ChunkSize, _logger);
                processors = Enumerable.Range(0, parameters.ParallelismDegree).Select(
                    _ => new ChunksDecompressor(inputPipe, outputPipe, _logger));
                break;
            }

            var actions = new Action[]
            {
                () =>
                {
                    (reader ?? throw new ArgumentNullException()).ReadFromStream(inputFile, cancellationTokenSource.Token);
                    inputFile.Close();
                },
                () =>
                {
                    writer.WriteToStream(
                        outputFile,
                        cancellationTokenSource.Token,
                        expectedChunksCount,
                        writeChunksLengths: parameters.Mode == ProcessorMode.Compress);
                    outputFile.Close();
                },
            }.Concat((processors ?? throw new ArgumentNullException())
Exemple #4
0
        public void WritesSingleChunkToStream()
        {
            var bytes = new byte[] { 0x00, 0x11, 0x22 };
            var pipe  = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = bytes
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 1);

            Assert.Equal(bytes, stream.ToArray());
        }
Exemple #5
0
        public void WritesTwoChunksConsecutive()
        {
            var bytes = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
            var pipe  = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 2);

            Assert.Equal(bytes, stream.ToArray());
        }
Exemple #6
0
        public void TestChunksLengthsWriting()
        {
            var bytes = BitConverter.GetBytes(3).Concat(new byte[] { 0x00, 0x11, 0x22 })
                        .Concat(BitConverter.GetBytes(4)).Concat(new byte[] { 0x33, 0x44, 0x55, 0x66 })
                        .ToArray();
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            writer.WriteToStream(stream, new CancellationToken(), 2, writeChunksLengths: true);

            Assert.Equal(bytes, stream.ToArray());
        }
Exemple #7
0
        public void TestCorruptedExpectedChunks()
        {
            var bytes = BitConverter.GetBytes(3).Concat(new byte[] { 0x00, 0x11, 0x22 })
                        .Concat(BitConverter.GetBytes(4)).Concat(new byte[] { 0x33, 0x44, 0x55, 0x66 })
                        .ToArray();
            var pipe = new PipeMock(new[]
            {
                new Chunk {
                    Bytes = new byte[] { 0x00, 0x11, 0x22 }, Index = 0
                },
                new Chunk {
                    Bytes = new byte[] { 0x33, 0x44, 0x55, 0x66 }, Index = 1
                }
            });
            var stream = new MemoryStream();
            var writer = new ChunksWriter(pipe, new LoggerMock());

            Assert.Throws <FileCorruptedException>(
                () => writer.WriteToStream(
                    stream,
                    new CancellationToken(),
                    expectedChunksCount: 222,
                    writeChunksLengths: true));
        }