blob: d2d057c0c9204f42be8bbcd681b0f1a8892e4954 [file] [log] [blame]
Jon Skeetee835a32015-06-30 17:22:26 +01001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2015 Google Inc. All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#endregion
32
33using System.IO;
Jon Skeete38294a2015-06-09 19:30:44 +010034
35namespace Google.Protobuf
36{
Jon Skeetcdeda4b2015-06-19 17:30:13 +010037 /// <summary>
38 /// Extension methods on <see cref="IMessage"/> and <see cref="IMessage{T}"/>.
39 /// </summary>
40 public static class MessageExtensions
Jon Skeete38294a2015-06-09 19:30:44 +010041 {
Jon Skeet811fc892015-08-04 15:58:39 +010042 /// <summary>
43 /// Merges data from the given byte array into an existing message.
44 /// </summary>
45 /// <param name="message">The message to merge the data into.</param>
46 /// <param name="data">The data to merge, which must be protobuf-encoded binary data.</param>
Jon Skeete38294a2015-06-09 19:30:44 +010047 public static void MergeFrom(this IMessage message, byte[] data)
48 {
Jon Skeet68380f02015-07-30 13:03:45 +010049 Preconditions.CheckNotNull(message, "message");
50 Preconditions.CheckNotNull(data, "data");
Jon Skeet0e0e0c92015-08-03 11:08:53 +010051 CodedInputStream input = new CodedInputStream(data);
Jon Skeete38294a2015-06-09 19:30:44 +010052 message.MergeFrom(input);
Jon Skeete7f88ff2015-08-06 11:40:32 +010053 input.CheckReadEndOfStreamTag();
Jon Skeete38294a2015-06-09 19:30:44 +010054 }
55
Jon Skeet811fc892015-08-04 15:58:39 +010056 /// <summary>
57 /// Merges data from the given byte string into an existing message.
58 /// </summary>
59 /// <param name="message">The message to merge the data into.</param>
60 /// <param name="data">The data to merge, which must be protobuf-encoded binary data.</param>
Jon Skeete38294a2015-06-09 19:30:44 +010061 public static void MergeFrom(this IMessage message, ByteString data)
62 {
Jon Skeet68380f02015-07-30 13:03:45 +010063 Preconditions.CheckNotNull(message, "message");
64 Preconditions.CheckNotNull(data, "data");
Jon Skeete38294a2015-06-09 19:30:44 +010065 CodedInputStream input = data.CreateCodedInput();
66 message.MergeFrom(input);
Jon Skeete7f88ff2015-08-06 11:40:32 +010067 input.CheckReadEndOfStreamTag();
Jon Skeete38294a2015-06-09 19:30:44 +010068 }
69
Jon Skeet811fc892015-08-04 15:58:39 +010070 /// <summary>
71 /// Merges data from the given stream into an existing message.
72 /// </summary>
73 /// <param name="message">The message to merge the data into.</param>
74 /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
Jon Skeete38294a2015-06-09 19:30:44 +010075 public static void MergeFrom(this IMessage message, Stream input)
76 {
Jon Skeet68380f02015-07-30 13:03:45 +010077 Preconditions.CheckNotNull(message, "message");
78 Preconditions.CheckNotNull(input, "input");
Jon Skeet0e0e0c92015-08-03 11:08:53 +010079 CodedInputStream codedInput = new CodedInputStream(input);
Jon Skeete38294a2015-06-09 19:30:44 +010080 message.MergeFrom(codedInput);
Jon Skeete7f88ff2015-08-06 11:40:32 +010081 codedInput.CheckReadEndOfStreamTag();
Jon Skeete38294a2015-06-09 19:30:44 +010082 }
83
Jon Skeet811fc892015-08-04 15:58:39 +010084 /// <summary>
85 /// Merges length-delimited data from the given stream into an existing message.
86 /// </summary>
87 /// <remarks>
88 /// The stream is expected to contain a length and then the data. Only the amount of data
89 /// specified by the length will be consumed.
90 /// </remarks>
91 /// <param name="message">The message to merge the data into.</param>
92 /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
Jon Skeete38294a2015-06-09 19:30:44 +010093 public static void MergeDelimitedFrom(this IMessage message, Stream input)
94 {
Jon Skeet68380f02015-07-30 13:03:45 +010095 Preconditions.CheckNotNull(message, "message");
96 Preconditions.CheckNotNull(input, "input");
Jon Skeetf34d37a2015-06-30 13:16:20 +010097 int size = (int) CodedInputStream.ReadRawVarint32(input);
Jon Skeete38294a2015-06-09 19:30:44 +010098 Stream limitedStream = new LimitedInputStream(input, size);
99 message.MergeFrom(limitedStream);
100 }
101
Jon Skeet811fc892015-08-04 15:58:39 +0100102 /// <summary>
103 /// Converts the given message into a byte array in protobuf encoding.
104 /// </summary>
105 /// <param name="message">The message to convert.</param>
106 /// <returns>The message data as a byte array.</returns>
Jon Skeete38294a2015-06-09 19:30:44 +0100107 public static byte[] ToByteArray(this IMessage message)
108 {
Jon Skeet68380f02015-07-30 13:03:45 +0100109 Preconditions.CheckNotNull(message, "message");
Jon Skeete38294a2015-06-09 19:30:44 +0100110 byte[] result = new byte[message.CalculateSize()];
Jon Skeet0e0e0c92015-08-03 11:08:53 +0100111 CodedOutputStream output = new CodedOutputStream(result);
Jon Skeete38294a2015-06-09 19:30:44 +0100112 message.WriteTo(output);
113 output.CheckNoSpaceLeft();
114 return result;
115 }
116
Jon Skeet811fc892015-08-04 15:58:39 +0100117 /// <summary>
118 /// Writes the given message data to the given stream in protobuf encoding.
119 /// </summary>
120 /// <param name="message">The message to write to the stream.</param>
121 /// <param name="output">The stream to write to.</param>
Jon Skeete38294a2015-06-09 19:30:44 +0100122 public static void WriteTo(this IMessage message, Stream output)
123 {
Jon Skeet68380f02015-07-30 13:03:45 +0100124 Preconditions.CheckNotNull(message, "message");
125 Preconditions.CheckNotNull(output, "output");
Jon Skeet0e0e0c92015-08-03 11:08:53 +0100126 CodedOutputStream codedOutput = new CodedOutputStream(output);
Jon Skeete38294a2015-06-09 19:30:44 +0100127 message.WriteTo(codedOutput);
128 codedOutput.Flush();
129 }
130
Jon Skeet811fc892015-08-04 15:58:39 +0100131 /// <summary>
132 /// Writes the length and then data of the given message to a stream.
133 /// </summary>
134 /// <param name="message">The message to write.</param>
135 /// <param name="output">The output stream to write to.</param>
Jon Skeete38294a2015-06-09 19:30:44 +0100136 public static void WriteDelimitedTo(this IMessage message, Stream output)
137 {
Jon Skeet68380f02015-07-30 13:03:45 +0100138 Preconditions.CheckNotNull(message, "message");
139 Preconditions.CheckNotNull(output, "output");
Jon Skeet0e0e0c92015-08-03 11:08:53 +0100140 CodedOutputStream codedOutput = new CodedOutputStream(output);
Jon Skeete38294a2015-06-09 19:30:44 +0100141 codedOutput.WriteRawVarint32((uint)message.CalculateSize());
142 message.WriteTo(codedOutput);
143 codedOutput.Flush();
144 }
145
Jon Skeet811fc892015-08-04 15:58:39 +0100146 /// <summary>
147 /// Converts the given message into a byte string in protobuf encoding.
148 /// </summary>
149 /// <param name="message">The message to convert.</param>
150 /// <returns>The message data as a byte string.</returns>
Jon Skeete38294a2015-06-09 19:30:44 +0100151 public static ByteString ToByteString(this IMessage message)
152 {
Jon Skeet68380f02015-07-30 13:03:45 +0100153 Preconditions.CheckNotNull(message, "message");
Jon Skeete38294a2015-06-09 19:30:44 +0100154 return ByteString.AttachBytes(message.ToByteArray());
155 }
156 }
157}