MessageParser.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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;
  33. using System.IO;
  34. namespace Google.Protobuf
  35. {
  36. /// <summary>
  37. /// A general message parser, typically used by reflection-based code as all the methods
  38. /// return simple <see cref="IMessage"/>.
  39. /// </summary>
  40. public class MessageParser
  41. {
  42. private Func<IMessage> factory;
  43. internal MessageParser(Func<IMessage> factory)
  44. {
  45. this.factory = factory;
  46. }
  47. /// <summary>
  48. /// Creates a template instance ready for population.
  49. /// </summary>
  50. /// <returns>An empty message.</returns>
  51. internal IMessage CreateTemplate()
  52. {
  53. return factory();
  54. }
  55. /// <summary>
  56. /// Parses a message from a byte array.
  57. /// </summary>
  58. /// <param name="data">The byte array containing the message. Must not be null.</param>
  59. /// <returns>The newly parsed message.</returns>
  60. public IMessage ParseFrom(byte[] data)
  61. {
  62. ProtoPreconditions.CheckNotNull(data, "data");
  63. IMessage message = factory();
  64. message.MergeFrom(data);
  65. return message;
  66. }
  67. /// <summary>
  68. /// Parses a message from the given byte string.
  69. /// </summary>
  70. /// <param name="data">The data to parse.</param>
  71. /// <returns>The parsed message.</returns>
  72. public IMessage ParseFrom(ByteString data)
  73. {
  74. ProtoPreconditions.CheckNotNull(data, "data");
  75. IMessage message = factory();
  76. message.MergeFrom(data);
  77. return message;
  78. }
  79. /// <summary>
  80. /// Parses a message from the given stream.
  81. /// </summary>
  82. /// <param name="input">The stream to parse.</param>
  83. /// <returns>The parsed message.</returns>
  84. public IMessage ParseFrom(Stream input)
  85. {
  86. IMessage message = factory();
  87. message.MergeFrom(input);
  88. return message;
  89. }
  90. /// <summary>
  91. /// Parses a length-delimited message from the given stream.
  92. /// </summary>
  93. /// <remarks>
  94. /// The stream is expected to contain a length and then the data. Only the amount of data
  95. /// specified by the length will be consumed.
  96. /// </remarks>
  97. /// <param name="input">The stream to parse.</param>
  98. /// <returns>The parsed message.</returns>
  99. public IMessage ParseDelimitedFrom(Stream input)
  100. {
  101. IMessage message = factory();
  102. message.MergeDelimitedFrom(input);
  103. return message;
  104. }
  105. /// <summary>
  106. /// Parses a message from the given coded input stream.
  107. /// </summary>
  108. /// <param name="input">The stream to parse.</param>
  109. /// <returns>The parsed message.</returns>
  110. public IMessage ParseFrom(CodedInputStream input)
  111. {
  112. IMessage message = factory();
  113. message.MergeFrom(input);
  114. return message;
  115. }
  116. }
  117. /// <summary>
  118. /// A parser for a specific message type.
  119. /// </summary>
  120. /// <remarks>
  121. /// <p>
  122. /// This delegates most behavior to the
  123. /// <see cref="IMessage.MergeFrom"/> implementation within the original type, but
  124. /// provides convenient overloads to parse from a variety of sources.
  125. /// </p>
  126. /// <p>
  127. /// Most applications will never need to create their own instances of this type;
  128. /// instead, use the static <c>Parser</c> property of a generated message type to obtain a
  129. /// parser for that type.
  130. /// </p>
  131. /// </remarks>
  132. /// <typeparam name="T">The type of message to be parsed.</typeparam>
  133. public sealed class MessageParser<T> : MessageParser where T : IMessage
  134. {
  135. // Implementation note: all the methods here *could* just delegate up to the base class and cast the result.
  136. // The current implementation avoids a virtual method call and a cast, which *may* be significant in some cases.
  137. // Benchmarking work is required to measure the significance - but it's only a few lines of code in any case.
  138. // The API wouldn't change anyway - just the implementation - so this work can be deferred.
  139. private readonly Func<T> factory;
  140. /// <summary>
  141. /// Creates a new parser.
  142. /// </summary>
  143. /// <remarks>
  144. /// The factory method is effectively an optimization over using a generic constraint
  145. /// to require a parameterless constructor: delegates are significantly faster to execute.
  146. /// </remarks>
  147. /// <param name="factory">Function to invoke when a new, empty message is required.</param>
  148. public MessageParser(Func<T> factory) : base(() => factory())
  149. {
  150. this.factory = factory;
  151. }
  152. /// <summary>
  153. /// Creates a template instance ready for population.
  154. /// </summary>
  155. /// <returns>An empty message.</returns>
  156. internal new T CreateTemplate()
  157. {
  158. return factory();
  159. }
  160. /// <summary>
  161. /// Parses a message from a byte array.
  162. /// </summary>
  163. /// <param name="data">The byte array containing the message. Must not be null.</param>
  164. /// <returns>The newly parsed message.</returns>
  165. public new T ParseFrom(byte[] data)
  166. {
  167. ProtoPreconditions.CheckNotNull(data, "data");
  168. T message = factory();
  169. message.MergeFrom(data);
  170. return message;
  171. }
  172. /// <summary>
  173. /// Parses a message from the given byte string.
  174. /// </summary>
  175. /// <param name="data">The data to parse.</param>
  176. /// <returns>The parsed message.</returns>
  177. public new T ParseFrom(ByteString data)
  178. {
  179. ProtoPreconditions.CheckNotNull(data, "data");
  180. T message = factory();
  181. message.MergeFrom(data);
  182. return message;
  183. }
  184. /// <summary>
  185. /// Parses a message from the given stream.
  186. /// </summary>
  187. /// <param name="input">The stream to parse.</param>
  188. /// <returns>The parsed message.</returns>
  189. public new T ParseFrom(Stream input)
  190. {
  191. T message = factory();
  192. message.MergeFrom(input);
  193. return message;
  194. }
  195. /// <summary>
  196. /// Parses a length-delimited message from the given stream.
  197. /// </summary>
  198. /// <remarks>
  199. /// The stream is expected to contain a length and then the data. Only the amount of data
  200. /// specified by the length will be consumed.
  201. /// </remarks>
  202. /// <param name="input">The stream to parse.</param>
  203. /// <returns>The parsed message.</returns>
  204. public new T ParseDelimitedFrom(Stream input)
  205. {
  206. T message = factory();
  207. message.MergeDelimitedFrom(input);
  208. return message;
  209. }
  210. /// <summary>
  211. /// Parses a message from the given coded input stream.
  212. /// </summary>
  213. /// <param name="input">The stream to parse.</param>
  214. /// <returns>The parsed message.</returns>
  215. public new T ParseFrom(CodedInputStream input)
  216. {
  217. T message = factory();
  218. message.MergeFrom(input);
  219. return message;
  220. }
  221. }
  222. }