Beispiel #1
0
        public Result <int, ErrorCodes?> Compress([NotNull] Stream inputStream, [NotNull] Stream outputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }
            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            if (inputStream.Position == inputStream.Length)
            {
                return(ErrorCodes.NothingToCompress);
            }

            using (var workersPool = new WorkersPool.WorkersPool(settings.ThreadsCount, log))
                using (var tasksQueue = new BlockingTasksQueue(settings.CompressingQueueSize, workersPool, log))
                {
                    var outputFile = new OutputFile(outputStream, settings.OffsetLabel);
                    var(lastReadOffset, offsetError) = outputFile.GetLastOffset();
                    if (offsetError.HasValue)
                    {
                        return(offsetError);
                    }

                    var compressInputFileTask = RunEnqueueingInputFileBlockCompression(inputStream, lastReadOffset, tasksQueue);
                    workersPool.PushTask(compressInputFileTask);

                    foreach (var result in tasksQueue.ConsumeTaskResults())
                    {
                        if (result.IsFailed)
                        {
                            if (result.Exception is CompressorException fileSystemException)
                            {
                                return(fileSystemException.Error);
                            }
                            throw result.Exception;
                        }

                        var resultBlock = (Block)result.Result;

                        outputFile.Append(resultBlock.Bytes, resultBlock.Offset);
                    }

                    var commitError = outputFile.Commit();
                    if (commitError.HasValue)
                    {
                        return(commitError);
                    }

                    return(outputFile.CompressionRatio(inputStream.Length));
                }
        }
Beispiel #2
0
            public void Should_fail_when_given_negative_offset()
            {
                Action action = () => outputFile.Append(bytes, -1);

                action.Should().Throw <ArgumentException>();
            }