Exemple #1
0
        private static void Encode(Stream input, Stream output)
        {
            using (PaddedStream paddedInput = new PaddedStream(input, 32, PaddedStreamMode.Read))
            {
                using (XorStream xorPaddedInput = new XorStream(paddedInput))
                {
                    using (MemoryStream normalOutput = new MemoryStream())
                    {
                        using (MemoryStream xorOutput = new MemoryStream())
                        {
                            long initialPosition = input.Position;
                            long inputLength     = input.Length - initialPosition;

                            // Compress using normal encoding
                            EncodeInternal(paddedInput, normalOutput, false, inputLength);

                            // Reset the input stream and compress using XOR encoding
                            input.Position = initialPosition;
                            EncodeInternal(xorPaddedInput, xorOutput, true, inputLength);

                            long normalOutputLength = normalOutput.Length;
                            long xorOutputLength    = xorOutput.Length;

                            using (PaddedStream paddedOutput = new PaddedStream(output, 2, PaddedStreamMode.Write))
                            {
                                byte[] outputBytes =
                                    (normalOutputLength <= xorOutputLength ? normalOutput : xorOutput).ToArray();
                                paddedOutput.Write(outputBytes, 0, outputBytes.Length);
                            }
                        }
                    }
                }
            }
        }