| OLD | NEW |
| (Empty) | |
| 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 |
| 33 using System; |
| 34 using System.IO; |
| 35 |
| 36 namespace Google.Protobuf |
| 37 { |
| 38 /// <summary> |
| 39 /// A parser for a specific message type. |
| 40 /// </summary> |
| 41 /// <remarks> |
| 42 /// <p> |
| 43 /// This delegates most behavior to the |
| 44 /// <see cref="IMessage.MergeFrom"/> implementation within the original type
, but |
| 45 /// provides convenient overloads to parse from a variety of sources. |
| 46 /// </p> |
| 47 /// <p> |
| 48 /// Most applications will never need to create their own instances of this
type; |
| 49 /// instead, use the static <c>Parser</c> property of a generated message ty
pe to obtain a |
| 50 /// parser for that type. |
| 51 /// </p> |
| 52 /// </remarks> |
| 53 /// <typeparam name="T">The type of message to be parsed.</typeparam> |
| 54 public sealed class MessageParser<T> where T : IMessage<T> |
| 55 { |
| 56 private readonly Func<T> factory; |
| 57 |
| 58 /// <summary> |
| 59 /// Creates a new parser. |
| 60 /// </summary> |
| 61 /// <remarks> |
| 62 /// The factory method is effectively an optimization over using a gener
ic constraint |
| 63 /// to require a parameterless constructor: delegates are significantly
faster to execute. |
| 64 /// </remarks> |
| 65 /// <param name="factory">Function to invoke when a new, empty message i
s required.</param> |
| 66 public MessageParser(Func<T> factory) |
| 67 { |
| 68 this.factory = factory; |
| 69 } |
| 70 |
| 71 /// <summary> |
| 72 /// Creates a template instance ready for population. |
| 73 /// </summary> |
| 74 /// <returns>An empty message.</returns> |
| 75 internal T CreateTemplate() |
| 76 { |
| 77 return factory(); |
| 78 } |
| 79 |
| 80 /// <summary> |
| 81 /// Parses a message from a byte array. |
| 82 /// </summary> |
| 83 /// <param name="data">The byte array containing the message. Must not b
e null.</param> |
| 84 /// <returns>The newly parsed message.</returns> |
| 85 public T ParseFrom(byte[] data) |
| 86 { |
| 87 Preconditions.CheckNotNull(data, "data"); |
| 88 T message = factory(); |
| 89 message.MergeFrom(data); |
| 90 return message; |
| 91 } |
| 92 |
| 93 /// <summary> |
| 94 /// Parses a message from the given byte string. |
| 95 /// </summary> |
| 96 /// <param name="data">The data to parse.</param> |
| 97 /// <returns>The parsed message.</returns> |
| 98 public T ParseFrom(ByteString data) |
| 99 { |
| 100 Preconditions.CheckNotNull(data, "data"); |
| 101 T message = factory(); |
| 102 message.MergeFrom(data); |
| 103 return message; |
| 104 } |
| 105 |
| 106 /// <summary> |
| 107 /// Parses a message from the given stream. |
| 108 /// </summary> |
| 109 /// <param name="input">The stream to parse.</param> |
| 110 /// <returns>The parsed message.</returns> |
| 111 public T ParseFrom(Stream input) |
| 112 { |
| 113 T message = factory(); |
| 114 message.MergeFrom(input); |
| 115 return message; |
| 116 } |
| 117 |
| 118 /// <summary> |
| 119 /// Parses a length-delimited message from the given stream. |
| 120 /// </summary> |
| 121 /// <remarks> |
| 122 /// The stream is expected to contain a length and then the data. Only t
he amount of data |
| 123 /// specified by the length will be consumed. |
| 124 /// </remarks> |
| 125 /// <param name="input">The stream to parse.</param> |
| 126 /// <returns>The parsed message.</returns> |
| 127 public T ParseDelimitedFrom(Stream input) |
| 128 { |
| 129 T message = factory(); |
| 130 message.MergeDelimitedFrom(input); |
| 131 return message; |
| 132 } |
| 133 |
| 134 /// <summary> |
| 135 /// Parses a message from the given coded input stream. |
| 136 /// </summary> |
| 137 /// <param name="input">The stream to parse.</param> |
| 138 /// <returns>The parsed message.</returns> |
| 139 public T ParseFrom(CodedInputStream input) |
| 140 { |
| 141 T message = factory(); |
| 142 message.MergeFrom(input); |
| 143 return message; |
| 144 } |
| 145 } |
| 146 } |
| OLD | NEW |