Exemplo n.º 1
0
        /// <inheritdoc cref="Compress"/>
        public void Compress(Stream input, Stream output)
        {
            if (_lzEncoderAction != null)
            {
                var lzEncoder = _lzEncoderAction();
                ContractAssertions.IsNotNull(lzEncoder, nameof(lzEncoder));

                lzEncoder.Configure(_matchOptions);
                var matchParser = _matchOptions.BuildMatchParser();

                lzEncoder.Encode(input, output, matchParser?.ParseMatches(input));
            }
            else if (_huffmanEncoderAction != null)
            {
                var huffmanEncoder = _huffmanEncoderAction();
                ContractAssertions.IsNotNull(huffmanEncoder, nameof(huffmanEncoder));

                huffmanEncoder.Configure(_huffmanOptions);
                var treeBuilder = _huffmanOptions.BuildHuffmanTree();

                huffmanEncoder.Encode(input, output, treeBuilder);
            }
            else if (_lzHuffmanEncoderAction != null)
            {
                var lzHuffmanEncoder = _lzHuffmanEncoderAction();
                ContractAssertions.IsNotNull(lzHuffmanEncoder, nameof(lzHuffmanEncoder));

                lzHuffmanEncoder.Configure(_matchOptions, _huffmanOptions);
                var matchParser = _matchOptions.BuildMatchParser();
                var treeBuilder = _huffmanOptions.BuildHuffmanTree();

                lzHuffmanEncoder.Encode(input, output, matchParser?.ParseMatches(input), treeBuilder);
            }
            else if (_encoderAction != null)
            {
                var encoder = _encoderAction();
                ContractAssertions.IsNotNull(encoder, nameof(encoder));

                encoder.Encode(input, output);
            }
            else
            {
                throw new InvalidOperationException("No compression encoder was set.");
            }
        }