/// <summary>
        /// Asynchronously write the literal to the specified stream.
        /// </summary>
        /// <remarks>
        /// Asynchronously writes the literal to the specified stream.
        /// </remarks>
        /// <param name="stream">The stream.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public async Task WriteToAsync(ImapStream stream, CancellationToken cancellationToken)
        {
            if (Type == ImapLiteralType.String)
            {
                var bytes = (byte[])Literal;

                await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);

                await stream.FlushAsync(cancellationToken).ConfigureAwait(false);

                return;
            }

            //if (Type == ImapLiteralType.Stream) {
            //	var literal = (Stream) Literal;
            //	var buf = new byte[4096];
            //	int nread;

            //	while ((nread = await literal.ReadAsync (buf, 0, buf.Length, cancellationToken).ConfigureAwait (false)) > 0)
            //		await stream.WriteAsync (buf, 0, nread, cancellationToken).ConfigureAwait (false);

            //	await stream.FlushAsync (cancellationToken).ConfigureAwait (false);
            //	return;
            //}

            var message = (MimeMessage)Literal;

            using (var s = new ProgressStream(stream, update)) {
                await message.WriteToAsync(format, s, cancellationToken).ConfigureAwait(false);

                await s.FlushAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Write the literal to the specified stream.
        /// </summary>
        /// <remarks>
        /// Writes the literal to the specified stream.
        /// </remarks>
        /// <param name="stream">The stream.</param>
        /// <param name="doAsync">Whether the literal should be written asynchronously or not.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public async Task WriteToAsync(ImapStream stream, bool doAsync, CancellationToken cancellationToken)
        {
            if (Type == ImapLiteralType.String)
            {
                var bytes = (byte[])Literal;

                if (doAsync)
                {
                    await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);

                    await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    stream.Write(bytes, 0, bytes.Length, cancellationToken);
                    stream.Flush(cancellationToken);
                }
                return;
            }

            if (Type == ImapLiteralType.MimeMessage)
            {
                var message = (MimeMessage)Literal;

                using (var s = new ProgressStream(stream, update)) {
                    if (doAsync)
                    {
                        await message.WriteToAsync(format, s, cancellationToken).ConfigureAwait(false);

                        await s.FlushAsync(cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        message.WriteTo(format, s, cancellationToken);
                        s.Flush(cancellationToken);
                    }
                    return;
                }
            }

            var literal = (Stream)Literal;
            var buf     = new byte[4096];
            int nread;

            if (doAsync)
            {
                while ((nread = await literal.ReadAsync(buf, 0, buf.Length, cancellationToken).ConfigureAwait(false)) > 0)
                {
                    await stream.WriteAsync(buf, 0, nread, cancellationToken).ConfigureAwait(false);
                }

                await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
            }
            else
            {
                while ((nread = literal.Read(buf, 0, buf.Length)) > 0)
                {
                    stream.Write(buf, 0, nread, cancellationToken);
                }

                stream.Flush(cancellationToken);
            }
        }