Esempio n. 1
0
        /// <summary>
        /// Gets the decoded text content using the provided charset to override
        /// the charset specified in the Content-Type parameters.
        /// </summary>
        /// <remarks>
        /// Uses the provided charset encoding to convert the raw text content
        /// into a unicode string, overriding any charset specified in the
        /// Content-Type header.
        /// </remarks>
        /// <returns>The decoded text.</returns>
        /// <param name="charset">The charset encoding to use.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="charset"/> is <c>null</c>.
        /// </exception>
        public string GetText(Encoding charset)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }

            using (var memory = new MemoryStream()) {
                using (var filtered = new FilteredStream(memory)) {
                    filtered.Add(new CharsetFilter(charset, Encoding.UTF8));

                    ContentObject.DecodeTo(filtered);
                    filtered.Flush();

#if PORTABLE
                    var buffer = memory.ToArray();
                    int length = buffer.Length;
#else
                    var buffer = memory.GetBuffer();
                    int length = (int)memory.Length;
#endif

                    return(Encoding.UTF8.GetString(buffer, 0, length));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Writes the <see cref="MimeKit.MimePart"/> to the specified output stream.
        /// </summary>
        /// <param name="options">The formatting options.</param>
        /// <param name="stream">The output stream.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="stream"/> is <c>null</c>.</para>
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public override void WriteTo(FormatOptions options, Stream stream, CancellationToken cancellationToken)
        {
            base.WriteTo(options, stream, cancellationToken);

            if (ContentObject == null)
            {
                return;
            }

            if (ContentObject.Encoding != encoding)
            {
                if (encoding == ContentEncoding.UUEncode)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var begin  = string.Format("begin 0644 {0}", FileName ?? "unknown");
                    var buffer = Encoding.UTF8.GetBytes(begin);
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Write(options.NewLineBytes, 0, options.NewLineBytes.Length);
                }

                // transcode the content into the desired Content-Transfer-Encoding
                using (var filtered = new FilteredStream(stream)) {
                    filtered.Add(EncoderFilter.Create(encoding));

                    if (encoding != ContentEncoding.Binary)
                    {
                        filtered.Add(options.CreateNewLineFilter());
                    }

                    ContentObject.DecodeTo(filtered, cancellationToken);
                    filtered.Flush();
                }

                if (encoding == ContentEncoding.UUEncode)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var buffer = Encoding.ASCII.GetBytes("end");
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Write(options.NewLineBytes, 0, options.NewLineBytes.Length);
                }
            }
            else if (encoding != ContentEncoding.Binary)
            {
                using (var filtered = new FilteredStream(stream)) {
                    filtered.Add(options.CreateNewLineFilter());
                    ContentObject.WriteTo(filtered, cancellationToken);
                    filtered.Flush();
                }
            }
            else
            {
                ContentObject.WriteTo(stream, cancellationToken);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the decoded text content using the provided charset to override
        /// the charset specified in the Content-Type parameters.
        /// </summary>
        /// <returns>The decoded text.</returns>
        /// <param name="charset">The charset encoding to use.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="charset"/> is <c>null</c>.
        /// </exception>
        public string GetText(Encoding charset)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }

            using (var memory = new MemoryStream()) {
                using (var filtered = new FilteredStream(memory)) {
                    filtered.Add(new CharsetFilter(charset, Encoding.UTF8));

                    ContentObject.DecodeTo(filtered);
                    filtered.Flush();

                    return(Encoding.UTF8.GetString(memory.GetBuffer(), 0, (int)memory.Length));
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the decoded text content using the provided charset encoding to
        /// override the charset specified in the Content-Type parameters.
        /// </summary>
        /// <remarks>
        /// Uses the provided charset encoding to convert the raw text content
        /// into a unicode string, overriding any charset specified in the
        /// Content-Type header.
        /// </remarks>
        /// <returns>The decoded text.</returns>
        /// <param name="encoding">The charset encoding to use.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="encoding"/> is <c>null</c>.
        /// </exception>
        public string GetText(Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            using (var memory = new MemoryStream()) {
                ContentObject.DecodeTo(memory);

#if PORTABLE
                var buffer = memory.ToArray();
#else
                var buffer = memory.GetBuffer();
#endif

                return(encoding.GetString(buffer, 0, (int)memory.Length));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Calculates the most efficient content encoding given the specified constraint.
        /// </summary>
        /// <remarks>
        /// If no <see cref="ContentObject"/> is set, <see cref="ContentEncoding.SevenBit"/> will be returned.
        /// </remarks>
        /// <returns>The most efficient content encoding.</returns>
        /// <param name="constraint">The encoding constraint.</param>
        /// <param name="maxLineLength">The maximum allowable length for a line (not counting the CRLF). Must be between <c>72</c> and <c>998</c> (inclusive).</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// <para><paramref name="maxLineLength"/> is not between <c>72</c> and <c>998</c> (inclusive).</para>
        /// <para>-or-</para>
        /// <para><paramref name="constraint"/> is not a valid value.</para>
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        /// The operation was canceled via the cancellation token.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        /// An I/O error occurred.
        /// </exception>
        public ContentEncoding GetBestEncoding(EncodingConstraint constraint, int maxLineLength, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (ContentObject == null)
            {
                return(ContentEncoding.SevenBit);
            }

            using (var measure = new MeasuringStream()) {
                using (var filtered = new FilteredStream(measure)) {
                    var filter = new BestEncodingFilter();

                    filtered.Add(filter);
                    ContentObject.DecodeTo(filtered, cancellationToken);
                    filtered.Flush();

                    return(filter.GetBestEncoding(constraint, maxLineLength));
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the decoded text content using the provided charset encoding to
        /// override the charset specified in the Content-Type parameters.
        /// </summary>
        /// <remarks>
        /// Uses the provided charset encoding to convert the raw text content
        /// into a unicode string, overriding any charset specified in the
        /// Content-Type header.
        /// </remarks>
        /// <returns>The decoded text.</returns>
        /// <param name="encoding">The charset encoding to use.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="encoding"/> is <c>null</c>.
        /// </exception>
        public string GetText(Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (ContentObject == null)
            {
                return(string.Empty);
            }

            using (var memory = new MemoryStream()) {
                ContentObject.DecodeTo(memory);

#if !PORTABLE && !NETSTANDARD
                var buffer = memory.GetBuffer();
#else
                var buffer = memory.ToArray();
#endif

                return(encoding.GetString(buffer, 0, (int)memory.Length));
            }
        }