Skip to content

Commit

Permalink
Added a MimeEntity.WriteTo() method that takes a bool contentOnly par…
Browse files Browse the repository at this point in the history
…ameter

Fixes issue #207
  • Loading branch information
jstedfast committed Dec 13, 2015
1 parent 8c6a452 commit 59bf991
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 13 deletions.
5 changes: 3 additions & 2 deletions MimeKit/MessagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="stream">The output stream.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
Expand All @@ -196,9 +197,9 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
base.WriteTo (options, stream, cancellationToken);
base.WriteTo (options, stream, contentOnly, cancellationToken);

if (Message != null)
Message.WriteTo (options, stream, cancellationToken);
Expand Down
149 changes: 144 additions & 5 deletions MimeKit/MimeEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ public virtual void Accept (MimeVisitor visitor)
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="stream">The output stream.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
Expand All @@ -409,15 +410,65 @@ public virtual void Accept (MimeVisitor visitor)
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public virtual void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
public virtual void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
if (options == null)
throw new ArgumentNullException ("options");

if (stream == null)
throw new ArgumentNullException ("stream");

Headers.WriteTo (options, stream, cancellationToken);
if (!contentOnly)
Headers.WriteTo (options, stream, cancellationToken);
}

/// <summary>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified output stream.
/// </summary>
/// <remarks>
/// <para>Writes the headers to the output stream, followed by a blank line.</para>
/// <para>Subclasses should override this method to write the content of the entity.</para>
/// </remarks>
/// <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 void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
{
WriteTo (options, stream, false, cancellationToken);
}

/// <summary>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified output stream.
/// </summary>
/// <remarks>
/// Writes the entity to the output stream.
/// </remarks>
/// <param name="stream">The output stream.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="stream"/> is <c>null</c>.
/// </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 void WriteTo (Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
WriteTo (FormatOptions.Default, stream, contentOnly, cancellationToken);
}

/// <summary>
Expand All @@ -439,10 +490,57 @@ public virtual void WriteTo (FormatOptions options, Stream stream, CancellationT
/// </exception>
public void WriteTo (Stream stream, CancellationToken cancellationToken = default (CancellationToken))
{
WriteTo (FormatOptions.Default, stream, cancellationToken);
WriteTo (FormatOptions.Default, stream, false, cancellationToken);
}

#if !PORTABLE && !COREFX
/// <summary>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file.
/// </summary>
/// <remarks>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file using the provided formatting options.
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="fileName">The file.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</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="fileName"/> is <c>null</c>.</para>
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="fileName"/> is a zero-length string, contains only white space, or
/// contains one or more invalid characters as defined by
/// <see cref="System.IO.Path.InvalidPathChars"/>.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.DirectoryNotFoundException">
/// <paramref name="fileName"/> is an invalid file path.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// The specified file path could not be found.
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// The user does not have access to write to the specified file.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public void WriteTo (FormatOptions options, string fileName, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
if (options == null)
throw new ArgumentNullException ("options");

if (fileName == null)
throw new ArgumentNullException ("fileName");

using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write))
WriteTo (options, stream, contentOnly, cancellationToken);
}

/// <summary>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file.
/// </summary>
Expand Down Expand Up @@ -486,7 +584,48 @@ public void WriteTo (FormatOptions options, string fileName, CancellationToken c
throw new ArgumentNullException ("fileName");

using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write))
WriteTo (options, stream, cancellationToken);
WriteTo (options, stream, false, cancellationToken);
}

/// <summary>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file.
/// </summary>
/// <remarks>
/// Writes the <see cref="MimeKit.MimeEntity"/> to the specified file using the default formatting options.
/// </remarks>
/// <param name="fileName">The file.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="fileName"/> is <c>null</c>.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <paramref name="fileName"/> is a zero-length string, contains only white space, or
/// contains one or more invalid characters as defined by
/// <see cref="System.IO.Path.InvalidPathChars"/>.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.DirectoryNotFoundException">
/// <paramref name="fileName"/> is an invalid file path.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// The specified file path could not be found.
/// </exception>
/// <exception cref="System.UnauthorizedAccessException">
/// The user does not have access to write to the specified file.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public void WriteTo (string fileName, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
if (fileName == null)
throw new ArgumentNullException ("fileName");

using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write))
WriteTo (FormatOptions.Default, stream, contentOnly, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -526,7 +665,7 @@ public void WriteTo (string fileName, CancellationToken cancellationToken = defa
throw new ArgumentNullException ("fileName");

using (var stream = File.Open (fileName, FileMode.Create, FileAccess.Write))
WriteTo (FormatOptions.Default, stream, cancellationToken);
WriteTo (FormatOptions.Default, stream, false, cancellationToken);
}
#endif

Expand Down
5 changes: 3 additions & 2 deletions MimeKit/MimePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="stream">The output stream.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
Expand All @@ -543,9 +544,9 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
base.WriteTo (options, stream, cancellationToken);
base.WriteTo (options, stream, contentOnly, cancellationToken);

if (ContentObject == null)
return;
Expand Down
9 changes: 5 additions & 4 deletions MimeKit/Multipart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// </remarks>
/// <param name="options">The formatting options.</param>
/// <param name="stream">The output stream.</param>
/// <param name="contentOnly"><c>true</c> if only the content should be written; otherwise, <c>false</c>.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <exception cref="System.ArgumentNullException">
/// <para><paramref name="options"/> is <c>null</c>.</para>
Expand All @@ -397,12 +398,12 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public override void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
public override void WriteTo (FormatOptions options, Stream stream, bool contentOnly, CancellationToken cancellationToken = default (CancellationToken))
{
if (Boundary == null)
Boundary = GenerateBoundary ();

base.WriteTo (options, stream, cancellationToken);
base.WriteTo (options, stream, contentOnly, cancellationToken);

if (ContentType.IsMimeType ("multipart", "signed")) {
// don't reformat the headers or content of any children of a multipart/signed
Expand All @@ -424,7 +425,7 @@ public override void WriteTo (FormatOptions options, Stream stream, Cancellation
for (int i = 0; i < children.Count; i++) {
cancellable.Write (boundary, 0, boundary.Length - 2, cancellationToken);
cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken);
children[i].WriteTo (options, stream, cancellationToken);
children[i].WriteTo (options, stream, false, cancellationToken);
cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken);
}

Expand All @@ -437,7 +438,7 @@ public override void WriteTo (FormatOptions options, Stream stream, Cancellation
cancellationToken.ThrowIfCancellationRequested ();
stream.Write (boundary, 0, boundary.Length - 2);
stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length);
children[i].WriteTo (options, stream, cancellationToken);
children[i].WriteTo (options, stream, false, cancellationToken);
stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length);
}

Expand Down

0 comments on commit 59bf991

Please sign in to comment.