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