Пример #1
0
        /// <summary>
        /// Combines a variable number of streams into one stream.
        /// </summary>
        /// <param name="streams">The streams to combine.</param>
        /// <returns>A variable number of <b>streams</b> combined into one <b>stream</b>.</returns>
        public static Stream CombineStreams(params Stream[] streams)
        {
            if (streams == null)
            {
                throw new ArgumentNullException(nameof(streams));
            }
            List <byte[]> bytes = new List <byte[]>();

            foreach (Stream stream in streams)
            {
                byte[] bytesFromStream = new byte[stream.Length];
                using (stream) // close and dispose the stream, as we are returning a new combined stream
                {
                    stream.Read(bytesFromStream, 0, (int)stream.Length);
                    bytes.Add(bytesFromStream);
                }
            }

            MemoryStream output;
            MemoryStream tempOutput = null;

            try
            {
                tempOutput          = new MemoryStream(ByteUtility.CombineByteArrays(bytes.ToArray()));
                tempOutput.Position = 0;
                output     = tempOutput;
                tempOutput = null;
            }
            finally
            {
                if (tempOutput != null)
                {
                    tempOutput.Dispose();
                }
            }
            output.Position = 0;
            return(output);
        }