MessageExtensions.cs 8.4 KB

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