MessageExtensions.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 string 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, ByteString data)
  59. {
  60. ProtoPreconditions.CheckNotNull(message, "message");
  61. ProtoPreconditions.CheckNotNull(data, "data");
  62. CodedInputStream input = data.CreateCodedInput();
  63. message.MergeFrom(input);
  64. input.CheckReadEndOfStreamTag();
  65. }
  66. /// <summary>
  67. /// Merges data from the given stream into an existing message.
  68. /// </summary>
  69. /// <param name="message">The message to merge the data into.</param>
  70. /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
  71. public static void MergeFrom(this IMessage message, Stream input)
  72. {
  73. ProtoPreconditions.CheckNotNull(message, "message");
  74. ProtoPreconditions.CheckNotNull(input, "input");
  75. CodedInputStream codedInput = new CodedInputStream(input);
  76. message.MergeFrom(codedInput);
  77. codedInput.CheckReadEndOfStreamTag();
  78. }
  79. /// <summary>
  80. /// Merges length-delimited data from the given stream into an existing message.
  81. /// </summary>
  82. /// <remarks>
  83. /// The stream is expected to contain a length and then the data. Only the amount of data
  84. /// specified by the length will be consumed.
  85. /// </remarks>
  86. /// <param name="message">The message to merge the data into.</param>
  87. /// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
  88. public static void MergeDelimitedFrom(this IMessage message, Stream input)
  89. {
  90. ProtoPreconditions.CheckNotNull(message, "message");
  91. ProtoPreconditions.CheckNotNull(input, "input");
  92. int size = (int) CodedInputStream.ReadRawVarint32(input);
  93. Stream limitedStream = new LimitedInputStream(input, size);
  94. message.MergeFrom(limitedStream);
  95. }
  96. /// <summary>
  97. /// Converts the given message into a byte array in protobuf encoding.
  98. /// </summary>
  99. /// <param name="message">The message to convert.</param>
  100. /// <returns>The message data as a byte array.</returns>
  101. public static byte[] ToByteArray(this IMessage message)
  102. {
  103. ProtoPreconditions.CheckNotNull(message, "message");
  104. byte[] result = new byte[message.CalculateSize()];
  105. CodedOutputStream output = new CodedOutputStream(result);
  106. message.WriteTo(output);
  107. output.CheckNoSpaceLeft();
  108. return result;
  109. }
  110. /// <summary>
  111. /// Writes the given message data to the given stream in protobuf encoding.
  112. /// </summary>
  113. /// <param name="message">The message to write to the stream.</param>
  114. /// <param name="output">The stream to write to.</param>
  115. public static void WriteTo(this IMessage message, Stream output)
  116. {
  117. ProtoPreconditions.CheckNotNull(message, "message");
  118. ProtoPreconditions.CheckNotNull(output, "output");
  119. CodedOutputStream codedOutput = new CodedOutputStream(output);
  120. message.WriteTo(codedOutput);
  121. codedOutput.Flush();
  122. }
  123. /// <summary>
  124. /// Writes the length and then data of the given message to a stream.
  125. /// </summary>
  126. /// <param name="message">The message to write.</param>
  127. /// <param name="output">The output stream to write to.</param>
  128. public static void WriteDelimitedTo(this IMessage message, Stream output)
  129. {
  130. ProtoPreconditions.CheckNotNull(message, "message");
  131. ProtoPreconditions.CheckNotNull(output, "output");
  132. CodedOutputStream codedOutput = new CodedOutputStream(output);
  133. codedOutput.WriteRawVarint32((uint)message.CalculateSize());
  134. message.WriteTo(codedOutput);
  135. codedOutput.Flush();
  136. }
  137. /// <summary>
  138. /// Converts the given message into a byte string in protobuf encoding.
  139. /// </summary>
  140. /// <param name="message">The message to convert.</param>
  141. /// <returns>The message data as a byte string.</returns>
  142. public static ByteString ToByteString(this IMessage message)
  143. {
  144. ProtoPreconditions.CheckNotNull(message, "message");
  145. return ByteString.AttachBytes(message.ToByteArray());
  146. }
  147. }
  148. }