Пример #1
0
        /// <summary>
        /// Removes the preamble information (if present) from the specified <see cref="Stream"/>, and <paramref name="source"/> is being closed and disposed.
        /// </summary>
        /// <param name="source">The input <see cref="Stream"/> to process.</param>
        /// <param name="encoding">The encoding to use when determining the preamble to remove.</param>
        /// <returns>A <see cref="Stream"/> without preamble information.</returns>
        public static Stream RemovePreamble(Stream source, Encoding encoding)
        {
            Validator.ThrowIfNull(source, nameof(source));

            byte[] bytes = ByteConverter.FromStream(source);
            bytes = ByteUtility.RemovePreamble(bytes, encoding);

            MemoryStream output;
            MemoryStream tempOutput = null;

            try
            {
                tempOutput          = new MemoryStream(bytes);
                tempOutput.Position = 0;
                output     = tempOutput;
                tempOutput = null;
            }
            finally
            {
                if (tempOutput != null)
                {
                    tempOutput.Dispose();
                }
                using (source) // because we return a stream, close the source
                {
                }
            }
            output.Position = 0;
            return(output);
        }
Пример #2
0
        /// <summary>
        /// Converts the entire XML <see cref="Stream"/> object from one encoding to another.
        /// </summary>
        /// <param name="source">The <see cref="Stream"/> to apply the conversion to.</param>
        /// <param name="sourceEncoding">The source encoding format.</param>
        /// <param name="targetEncoding">The target encoding format.</param>
        /// <param name="sequence">Determines whether too keep or remove any preamble sequences.</param>
        /// <param name="omitXmlDeclaration">if set to <c>true</c> omit the XML declaration; otherwise <c>false</c>. The default is false.</param>
        /// <returns>A <see cref="Stream"/> object containing the results of converting bytes from sourceEncoding to targetEncoding.</returns>
        public static Stream ChangeEncoding(Stream source, Encoding sourceEncoding, Encoding targetEncoding, PreambleSequence sequence, bool omitXmlDeclaration)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (sourceEncoding == null)
            {
                throw new ArgumentNullException(nameof(sourceEncoding));
            }
            if (targetEncoding == null)
            {
                throw new ArgumentNullException(nameof(targetEncoding));
            }
            if (sourceEncoding.Equals(targetEncoding))
            {
                return(source);
            }

            long startingPosition = -1;

            if (source.CanSeek)
            {
                startingPosition = source.Position;
                source.Position  = 0;
            }

            Stream stream;
            Stream tempStream = null;

            try
            {
                tempStream = new MemoryStream();
                XmlDocument document = new XmlDocument();
                document.Load(source);
                if (document.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
                {
                    XmlDeclaration declaration = (XmlDeclaration)document.FirstChild;
                    declaration.Encoding = targetEncoding.WebName;
                }
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding           = targetEncoding;
                settings.Indent             = true;
                settings.OmitXmlDeclaration = omitXmlDeclaration;
                using (XmlWriter writer = XmlWriter.Create(tempStream, settings))
                {
                    document.Save(writer);
                    writer.Flush();
                }

                if (source.CanSeek)
                {
                    source.Seek(startingPosition, SeekOrigin.Begin);
                }                                                                        // reset to original position
                tempStream.Position = 0;

                switch (sequence)
                {
                case PreambleSequence.Keep:
                    stream     = tempStream;
                    tempStream = null;
                    break;

                case PreambleSequence.Remove:
                    byte[] valueInBytes = ((MemoryStream)tempStream).ToArray();
                    using (tempStream)
                    {
                        valueInBytes = ByteUtility.RemovePreamble(valueInBytes, targetEncoding);
                    }
                    tempStream = StreamConverter.FromBytes(valueInBytes);
                    stream     = tempStream;
                    tempStream = null;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(sequence));
                }
            }
            finally
            {
                if (tempStream != null)
                {
                    tempStream.Dispose();
                }
            }
            return(stream);
        }