aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs49
-rw-r--r--src/ProtocolBuffers.Serialization/JsonFormatWriter.cs13
-rw-r--r--src/ProtocolBuffers.Serialization/XmlFormatWriter.cs14
-rw-r--r--src/ProtocolBuffers/CodedOutputStream.cs12
-rw-r--r--src/ProtocolBuffers/ICodedOutputStream.cs2
5 files changed, 72 insertions, 18 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();
+ }
}
diff --git a/src/ProtocolBuffers.Serialization/JsonFormatWriter.cs b/src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
index 12d180d8..5f396ae5 100644
--- a/src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
+++ b/src/ProtocolBuffers.Serialization/JsonFormatWriter.cs
@@ -239,6 +239,19 @@ namespace Google.ProtocolBuffers.Serialization
/// <summary> Gets or sets the whitespace to use to separate the text, default = empty </summary>
public string Whitespace { get; set; }
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && _counter.Count == 1)
+ {
+ EndMessage();
+ }
+
+ base.Dispose(disposing);
+ }
+
private void Seperator()
{
if (_counter.Count == 0)
diff --git a/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs b/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
index 97fc6b2c..a9cfcc1e 100644
--- a/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
+++ b/src/ProtocolBuffers.Serialization/XmlFormatWriter.cs
@@ -75,8 +75,13 @@ namespace Google.ProtocolBuffers.Serialization
{
if (disposing)
{
+ if (_output.WriteState != WriteState.Closed && _output.WriteState != WriteState.Start)
+ _output.WriteEndDocument();
+
_output.Close();
}
+
+ base.Dispose(disposing);
}
/// <summary>
@@ -112,6 +117,15 @@ namespace Google.ProtocolBuffers.Serialization
}
/// <summary>
+ /// Completes any pending write operations
+ /// </summary>
+ public override void Flush()
+ {
+ _output.Flush();
+ base.Flush();
+ }
+
+ /// <summary>
/// Used to write the root-message preamble, in xml this is open element for RootElementName,
/// by default "&lt;root&gt;". After this call you can call IMessageLite.MergeTo(...) and
/// complete the message with a call to EndMessage().
diff --git a/src/ProtocolBuffers/CodedOutputStream.cs b/src/ProtocolBuffers/CodedOutputStream.cs
index c14f5e0d..bb133a77 100644
--- a/src/ProtocolBuffers/CodedOutputStream.cs
+++ b/src/ProtocolBuffers/CodedOutputStream.cs
@@ -57,7 +57,7 @@ namespace Google.ProtocolBuffers
/// methods are taken from the protocol buffer type names, not .NET types.
/// (Hence WriteFloat instead of WriteSingle, and WriteBool instead of WriteBoolean.)
/// </remarks>
- public sealed partial class CodedOutputStream : ICodedOutputStream
+ public sealed partial class CodedOutputStream : ICodedOutputStream, IDisposable
{
/// <summary>
/// The buffer size used by CreateInstance(Stream).
@@ -125,6 +125,16 @@ namespace Google.ProtocolBuffers
}
#endregion
+
+ public void Dispose()
+ {
+ if (output != null)
+ {
+ if (position > 0)
+ Flush();
+ output.Dispose();
+ }
+ }
#region Writing of unknown fields
diff --git a/src/ProtocolBuffers/ICodedOutputStream.cs b/src/ProtocolBuffers/ICodedOutputStream.cs
index 861d3fec..2c324792 100644
--- a/src/ProtocolBuffers/ICodedOutputStream.cs
+++ b/src/ProtocolBuffers/ICodedOutputStream.cs
@@ -49,7 +49,7 @@ namespace Google.ProtocolBuffers
/// in their binary form by creating a instance via the CodedOutputStream.CreateInstance
/// static factory.
/// </summary>
- public interface ICodedOutputStream
+ public interface ICodedOutputStream : IDisposable
{
/// <summary>
/// Indicates that all temporary buffers be written to the final output.