Beispiel #1
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="cancellationToken">The cancellation token.</param>
        public void WriteTo(ImapStream stream, CancellationToken cancellationToken)
        {
            if (Type == ImapLiteralType.String)
            {
                var bytes = (byte[])Literal;
                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)) {
                    message.WriteTo(format, s, cancellationToken);
                    s.Flush(cancellationToken);
                    return;
                }
            }

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

            while ((nread = literal.Read(buf, 0, buf.Length)) > 0)
            {
                stream.Write(buf, 0, nread, cancellationToken);
            }

            stream.Flush(cancellationToken);
        }
Beispiel #2
0
        public void Inner_Stream_Calls_Flush()
        {
            string key            = "test";
            var    mockStream     = new Mock <Stream>();
            var    progressStream = new ProgressStream(key, mockStream.Object, null, null);

            progressStream.Flush();

            mockStream.Verify(s => s.Flush(), Times.Once);
        }
Beispiel #3
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.Stream) {
            //	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);
            //	}
            //	return;
            //}

            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);
                }
            }
        }
 public static void TestPropertiesAndMethods()
 {
     using (MemoryStream memoryStream = FakeRuntimeFileInfo.ExpandableMemoryStream(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))
     {
         string kilroy = String.Empty;
         using (FakeStream testStream = new FakeStream(memoryStream, (string wasHere) => { kilroy += wasHere; }))
         {
             using (ProgressStream progressStream = new ProgressStream(testStream, new ProgressContext()))
             {
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanRead, Is.True, "The underlying stream is readable.");
                 Assert.That(kilroy, Is.EqualTo("CanRead"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanWrite, Is.True, "The underlying stream is writable.");
                 Assert.That(kilroy, Is.EqualTo("CanWrite"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.CanSeek, Is.True, "The underlying stream is seekable.");
                 Assert.That(kilroy, Is.EqualTo("CanSeek"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Flush();
                 Assert.That(kilroy, Is.EqualTo("Flush"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Length, Is.EqualTo(10), "There are 10 bytes  in the underlying stream.");
                 Assert.That(kilroy, Is.EqualTo("Length"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Seek(-4, SeekOrigin.End), Is.EqualTo(6), "4 bytes from the end of 10 should be 6.");
                 Assert.That(kilroy, Is.EqualTo("Seek"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(progressStream.Position, Is.EqualTo(6), "The position should still be at 6.");
                 Assert.That(kilroy, Is.EqualTo("getPosition"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Position = 0;
                 Assert.That(kilroy, Is.EqualTo("setPosition"), "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Write(new byte[] { 13 }, 0, 1);
                 Assert.That(kilroy.Contains("Write"), Is.True, "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 progressStream.Position = 0;
                 byte[] firstByte = new byte[1];
                 progressStream.Read(firstByte, 0, 1);
                 Assert.That(kilroy.Contains("Read"), Is.True, "ProgressStream should delegate to the underlying stream.");
                 kilroy = String.Empty;
                 Assert.That(firstByte[0], Is.EqualTo(13), "13 was just written to the first position.");
                 progressStream.SetLength(5);
                 Assert.That(kilroy, Is.EqualTo("SetLength"), "ProgressStream should delegate to the underlying stream.");
             }
         }
     }
 }
Beispiel #5
0
        public void TestWrite()
        {
            using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) {
                var buffer   = new byte[1024];
                int expected = 517;

                progress = 0;
                stream.Write(buffer, 0, expected);
                stream.Flush();
                Assert.AreEqual(expected, progress, "progress");
            }

            using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) {
                var buffer   = new byte[1024];
                int expected = 517;

                progress = 0;
                stream.Write(buffer, 0, expected, CancellationToken.None);
                stream.Flush(CancellationToken.None);
                Assert.AreEqual(expected, progress, "progress");
            }
        }
Beispiel #6
0
        private long SendBinary(ProgressStream input, String filename, Stream output, String boundary)
        {
            Write(GetFileSegmentStart(filename), output);
            output.Flush();

            byte[] tmp = new byte[BUFFER_SIZE];
            int    l;

            // Wrap the stream to get some progress updates..
            long uploadedBytes = 0;

            while ((l = input.Read(tmp, 0, BUFFER_SIZE)) != 0)
            {
                output.Write(tmp, 0, l);
                output.Flush();
                uploadedBytes += l;
            }

            Write(CRLF, output);
            input.Flush();

            return(uploadedBytes);
        }
Beispiel #7
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="cancellationToken">The cancellation token.</param>
		public void WriteTo (ImapStream stream, CancellationToken cancellationToken)
		{
			if (Type == ImapLiteralType.String) {
				var bytes = (byte[]) Literal;
				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)) {
					message.WriteTo (format, s, cancellationToken);
					s.Flush (cancellationToken);
					return;
				}
			}

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

			while ((nread = literal.Read (buf, 0, buf.Length)) > 0)
				stream.Write (buf, 0, nread, cancellationToken);

			stream.Flush (cancellationToken);
		}
Beispiel #8
0
		void Bdat (FormatOptions options, MimeMessage message, CancellationToken cancellationToken, ITransferProgress progress)
		{
			long size;

			using (var measure = new MeasuringStream ()) {
				message.WriteTo (options, measure, cancellationToken);
				size = measure.Length;
			}

			var bytes = Encoding.UTF8.GetBytes (string.Format ("BDAT {0} LAST\r\n", size));

			Stream.Write (bytes, 0, bytes.Length, cancellationToken);

			if (progress != null) {
				var ctx = new SendContext (progress, size);

				using (var stream = new ProgressStream (Stream, ctx.Update)) {
					message.WriteTo (options, stream, cancellationToken);
					stream.Flush (cancellationToken);
				}
			} else {
				message.WriteTo (options, Stream, cancellationToken);
				Stream.Flush (cancellationToken);
			}

			var response = Stream.ReadResponse (cancellationToken);

			switch (response.StatusCode) {
			default:
				throw new SmtpCommandException (SmtpErrorCode.MessageNotAccepted, response.StatusCode, response.Response);
			case SmtpStatusCode.AuthenticationRequired:
				throw new ServiceNotAuthenticatedException (response.Response);
			case SmtpStatusCode.Ok:
				OnMessageSent (new MessageSentEventArgs (message, response.Response));
				break;
			}
		}