MessageExtensions.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #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. using System.IO;
  33. namespace Google.Protobuf
  34. {
  35. /// <summary>
  36. /// Extension methods on <see cref="IMessage"/> and <see cref="IMessage{T}"/>.
  37. /// </summary>
  38. public static class MessageExtensions
  39. {
  40. /// <summary>
  41. /// Merges data from the given byte array into an existing message.
  42. /// </summary>
  43. /// <param name="message">The message to merge the data into.</param>
  44. /// <param name="data">The data to merge, which must be protobuf-encoded binary data.</param>
  45. public static void MergeFrom(this IMessage message, byte[] data)
  46. {
  47. ProtoPreconditions.CheckNotNull(message, "message");
  48. ProtoPreconditions.CheckNotNull(data, "data");
  49. CodedInputStream input = new CodedInputStream(data);
  50. message.MergeFrom(input);
  51. input.CheckReadEndOfStreamTag();
  52. }
  53. /// <summary>
  54. /// Merges data from the given byte array into an existing message.
  55. /// </summary>
  56. /// <param name="message">The message to merge the data into.</param>
  57. /// <param name="data">The data to merge, which must be protobuf-encoded binary data.</param>
  58. public static void MergeFrom(this IMessage message, byte[] data, int offset, int length)
  59. {
  60. ProtoPreconditions.CheckNotNull(message, "message");
  61. ProtoPreconditions.CheckNotNull(data, "data");
  62. CodedInputStream input = new CodedInputStream(data, offset, length);
  63. message.MergeFrom(input);
  64. input.CheckReadEndOfStreamTag();
  65. }
  66. /// <summary>
  67. /// Merges data from the given byte string into an existing message.
  68. /// </summary>
  69. /// <param name="message">The message to merge the data into.</param>
  70. /// <param name="data">The data to merge, which must be protobuf-encoded binary data.</param>
  71. public static void MergeFrom(this IMessage message, ByteString data)
  72. {
  73. ProtoPreconditions.CheckNotNull(message, "message");
  74. ProtoPreconditions.CheckNotNull(data, "data");
  75. CodedInputStream input = data.CreateCodedInput();
  76. message.MergeFrom(input);
  77. input.CheckReadEndOfStreamTag();
  78. }
  79. /// <summary>
  80. /// Merges data from the given stream into an existing message.
  81. /// </summary>
  82. /// <param name="message">The message to merge the data into.</param>
  83. /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
  84. public static void MergeFrom(this IMessage message, Stream input)
  85. {
  86. ProtoPreconditions.CheckNotNull(message, "message");
  87. ProtoPreconditions.CheckNotNull(input, "input");
  88. CodedInputStream codedInput = new CodedInputStream(input);
  89. message.MergeFrom(codedInput);
  90. codedInput.CheckReadEndOfStreamTag();
  91. }
  92. /// <summary>
  93. /// Merges length-delimited data from the given stream into an existing message.
  94. /// </summary>
  95. /// <remarks>
  96. /// The stream is expected to contain a length and then the data. Only the amount of data
  97. /// specified by the length will be consumed.
  98. /// </remarks>
  99. /// <param name="message">The message to merge the data into.</param>
  100. /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
  101. public static void MergeDelimitedFrom(this IMessage message, Stream input)
  102. {
  103. ProtoPreconditions.CheckNotNull(message, "message");
  104. ProtoPreconditions.CheckNotNull(input, "input");
  105. int size = (int) CodedInputStream.ReadRawVarint32(input);
  106. Stream limitedStream = new LimitedInputStream(input, size);
  107. message.MergeFrom(limitedStream);
  108. }
  109. /// <summary>
  110. /// Converts the given message into a byte array in protobuf encoding.
  111. /// </summary>
  112. /// <param name="message">The message to convert.</param>
  113. /// <returns>The message data as a byte array.</returns>
  114. public static byte[] ToByteArray(this IMessage message)
  115. {
  116. ProtoPreconditions.CheckNotNull(message, "message");
  117. byte[] result = new byte[message.CalculateSize()];
  118. CodedOutputStream output = new CodedOutputStream(result);
  119. message.WriteTo(output);
  120. output.CheckNoSpaceLeft();
  121. return result;
  122. }
  123. /// <summary>
  124. /// Writes the given message data to the given stream in protobuf encoding.
  125. /// </summary>
  126. /// <param name="message">The message to write to the stream.</param>
  127. /// <param name="output">The stream to write to.</param>
  128. public static void WriteTo(this IMessage message, Stream output)
  129. {
  130. ProtoPreconditions.CheckNotNull(message, "message");
  131. ProtoPreconditions.CheckNotNull(output, "output");
  132. CodedOutputStream codedOutput = new CodedOutputStream(output);
  133. message.WriteTo(codedOutput);
  134. codedOutput.Flush();
  135. }
  136. /// <summary>
  137. /// Writes the length and then data of the given message to a stream.
  138. /// </summary>
  139. /// <param name="message">The message to write.</param>
  140. /// <param name="output">The output stream to write to.</param>
  141. public static void WriteDelimitedTo(this IMessage message, Stream output)
  142. {
  143. ProtoPreconditions.CheckNotNull(message, "message");
  144. ProtoPreconditions.CheckNotNull(output, "output");
  145. CodedOutputStream codedOutput = new CodedOutputStream(output);
  146. codedOutput.WriteRawVarint32((uint)message.CalculateSize());
  147. message.WriteTo(codedOutput);
  148. codedOutput.Flush();
  149. }
  150. /// <summary>
  151. /// Converts the given message into a byte string in protobuf encoding.
  152. /// </summary>
  153. /// <param name="message">The message to convert.</param>
  154. /// <returns>The message data as a byte string.</returns>
  155. public static ByteString ToByteString(this IMessage message)
  156. {
  157. ProtoPreconditions.CheckNotNull(message, "message");
  158. return ByteString.AttachBytes(message.ToByteArray());
  159. }
  160. }
  161. }