MessageParser.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 parser for a specific message type.
  38. /// </summary>
  39. /// <remarks>
  40. /// <p>
  41. /// This delegates most behavior to the
  42. /// <see cref="IMessage.MergeFrom"/> implementation within the original type, but
  43. /// provides convenient overloads to parse from a variety of sources.
  44. /// </p>
  45. /// <p>
  46. /// Most applications will never need to create their own instances of this type;
  47. /// instead, use the static <c>Parser</c> property of a generated message type to obtain a
  48. /// parser for that type.
  49. /// </p>
  50. /// </remarks>
  51. /// <typeparam name="T">The type of message to be parsed.</typeparam>
  52. public sealed class MessageParser<T> where T : IMessage<T>
  53. {
  54. private readonly Func<T> factory;
  55. /// <summary>
  56. /// Creates a new parser.
  57. /// </summary>
  58. /// <remarks>
  59. /// The factory method is effectively an optimization over using a generic constraint
  60. /// to require a parameterless constructor: delegates are significantly faster to execute.
  61. /// </remarks>
  62. /// <param name="factory">Function to invoke when a new, empty message is required.</param>
  63. public MessageParser(Func<T> factory)
  64. {
  65. this.factory = factory;
  66. }
  67. /// <summary>
  68. /// Creates a template instance ready for population.
  69. /// </summary>
  70. /// <returns>An empty message.</returns>
  71. internal T CreateTemplate()
  72. {
  73. return factory();
  74. }
  75. /// <summary>
  76. /// Parses a message from a byte array.
  77. /// </summary>
  78. /// <param name="data">The byte array containing the message. Must not be null.</param>
  79. /// <returns>The newly parsed message.</returns>
  80. public T ParseFrom(byte[] data)
  81. {
  82. Preconditions.CheckNotNull(data, "data");
  83. T message = factory();
  84. message.MergeFrom(data);
  85. return message;
  86. }
  87. public T ParseFrom(ByteString data)
  88. {
  89. Preconditions.CheckNotNull(data, "data");
  90. T message = factory();
  91. message.MergeFrom(data);
  92. return message;
  93. }
  94. public T ParseFrom(Stream input)
  95. {
  96. T message = factory();
  97. message.MergeFrom(input);
  98. return message;
  99. }
  100. public T ParseDelimitedFrom(Stream input)
  101. {
  102. T message = factory();
  103. message.MergeDelimitedFrom(input);
  104. return message;
  105. }
  106. public T ParseFrom(CodedInputStream input)
  107. {
  108. T message = factory();
  109. message.MergeFrom(input);
  110. return message;
  111. }
  112. }
  113. }