/// <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);
            }
        }
Beispiel #2
0
        public async Task TestWriteAsync()
        {
            using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) {
                var buffer   = new byte[1024];
                int expected = 517;

                progress = 0;
                await stream.WriteAsync(buffer, 0, expected);

                await stream.FlushAsync();

                Assert.AreEqual(expected, progress, "progress");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Writes a body to an <see cref="HttpWebRequest"/>. Writing HTTP bodies with HttpWebRequest requires
        /// certain operations be done in a specific order, an order that might not make sense for every calling
        /// site. This method enforces that order.
        /// </summary>
        /// <param name="request">The request to which to write the </param>
        /// <param name="getInfo"></param>
        /// <param name="writeBody"></param>
        /// <returns></returns>
        public static async Task WriteBodyAsync(this HttpWebRequest request, Func <BodyInfo> getInfo, Action <Stream> writeBody, IProgress prog = null)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (getInfo is null)
            {
                throw new ArgumentNullException(nameof(getInfo));
            }

            if (writeBody is null)
            {
                throw new ArgumentNullException(nameof(writeBody));
            }

            var info = getInfo();

            if (info is object)
            {
                if (info.MIMEType is object)
                {
                    request.ContentType = info.MIMEType;
                }

                if (info.Length >= 0)
                {
                    request.ContentLength = info.Length;
                    if (info.Length > 0)
                    {
                        using var stream = await request
                                           .GetRequestStreamAsync()
                                           .ConfigureAwait(false);

                        using var progStream = new ProgressStream(stream, info.Length, prog, true);
                        writeBody(stream);

                        await progStream.FlushAsync()
                        .ConfigureAwait(false);
                    }
                }
            }
        }
        /// <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);
            }
        }