MessageExtensions.cs 8.2 KB

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