diff options
author | csharptest <roger@csharptest.net> | 2011-09-08 20:28:22 -0500 |
---|---|---|
committer | rogerk <devnull@localhost> | 2011-09-08 20:28:22 -0500 |
commit | 819b7154d162f3ef4f187f19b020f999c02fcf03 (patch) | |
tree | 143a7767ed58b124c452062cecc2f85f7d3da191 /src/ProtocolBuffers.Serialization/Http | |
parent | c2d2c1adaf447bbc80194d8bce6c4e0442a7f47a (diff) | |
download | protobuf-819b7154d162f3ef4f187f19b020f999c02fcf03.tar.gz protobuf-819b7154d162f3ef4f187f19b020f999c02fcf03.tar.bz2 protobuf-819b7154d162f3ef4f187f19b020f999c02fcf03.zip |
Added IDisposable to ICodedOutputStream
Diffstat (limited to 'src/ProtocolBuffers.Serialization/Http')
-rw-r--r-- | src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs b/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs index 7173d478..8a19a8b7 100644 --- a/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs +++ b/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs @@ -58,15 +58,15 @@ namespace Google.ProtocolBuffers.Serialization ICodedInputStream codedInput = CreateInputStream(options, contentType, input);
return (TBuilder)builder.WeakMergeFrom(codedInput, options.ExtensionRegistry);
}
-
+
/// <summary>
/// Writes the message instance to the stream using the content type provided
/// </summary>
- /// <param name="message">An instance of a message</param>
/// <param name="options">Options specific to writing this message and/or content type</param>
/// <param name="contentType">The mime type of the content to be written</param>
/// <param name="output">The stream to write the message to</param>
- public static void WriteTo(this IMessageLite message, MessageFormatOptions options, string contentType, Stream output)
+ /// <remarks> If you do not dispose of ICodedOutputStream some formats may yield incomplete output </remarks>
+ public static ICodedOutputStream CreateOutputStream(MessageFormatOptions options, string contentType, Stream output)
{
FormatType outputType = ContentTypeToFormat(contentType, options.DefaultContentType);
@@ -95,15 +95,15 @@ namespace Google.ProtocolBuffers.Serialization else
{
XmlWriterSettings settings = new XmlWriterSettings()
- {
- CheckCharacters = false,
- NewLineHandling = NewLineHandling.Entitize,
- OmitXmlDeclaration = true,
- Encoding = Encoding.UTF8,
- Indent = true,
- IndentChars = " ",
- NewLineChars = Environment.NewLine,
- };
+ {
+ CheckCharacters = false,
+ NewLineHandling = NewLineHandling.Entitize,
+ OmitXmlDeclaration = true,
+ Encoding = Encoding.UTF8,
+ Indent = true,
+ IndentChars = " ",
+ NewLineChars = Environment.NewLine,
+ };
writer = XmlFormatWriter.CreateInstance(XmlWriter.Create(output, settings));
}
writer.RootElementName = options.XmlWriterRootElementName;
@@ -114,12 +114,29 @@ namespace Google.ProtocolBuffers.Serialization else
throw new NotSupportedException();
- message.WriteTo(codedOutput);
+ return codedOutput;
+ }
+
+ /// <summary>
+ /// Writes the message instance to the stream using the content type provided
+ /// </summary>
+ /// <param name="message">An instance of a message</param>
+ /// <param name="options">Options specific to writing this message and/or content type</param>
+ /// <param name="contentType">The mime type of the content to be written</param>
+ /// <param name="output">The stream to write the message to</param>
+ public static void WriteTo(this IMessageLite message, MessageFormatOptions options, string contentType, Stream output)
+ {
+ using (ICodedOutputStream codedOutput = CreateOutputStream(options, contentType, output))
+ {
+ message.WriteTo(codedOutput);
- if (codedOutput is AbstractWriter)
- ((AbstractWriter) codedOutput).EndMessage();
+ // This is effectivly done by Dispose(); however, if you need to finalize a message
+ // without disposing the underlying stream, this is the only way to do it.
+ if (codedOutput is AbstractWriter)
+ ((AbstractWriter)codedOutput).EndMessage();
- codedOutput.Flush();
+ codedOutput.Flush();
+ }
}
|