Jon Skeet | ee835a3 | 2015-06-30 17:22:26 +0100 | [diff] [blame] | 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; |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 34 | |
| 35 | namespace Google.Protobuf |
| 36 | { |
Jon Skeet | cdeda4b | 2015-06-19 17:30:13 +0100 | [diff] [blame] | 37 | /// <summary> |
| 38 | /// Extension methods on <see cref="IMessage"/> and <see cref="IMessage{T}"/>. |
| 39 | /// </summary> |
| 40 | public static class MessageExtensions |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 41 | { |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 47 | public static void MergeFrom(this IMessage message, byte[] data) |
| 48 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 49 | Preconditions.CheckNotNull(message, "message"); |
| 50 | Preconditions.CheckNotNull(data, "data"); |
Jon Skeet | 0e0e0c9 | 2015-08-03 11:08:53 +0100 | [diff] [blame] | 51 | CodedInputStream input = new CodedInputStream(data); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 52 | message.MergeFrom(input); |
Jon Skeet | e7f88ff | 2015-08-06 11:40:32 +0100 | [diff] [blame^] | 53 | input.CheckReadEndOfStreamTag(); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 61 | public static void MergeFrom(this IMessage message, ByteString data) |
| 62 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 63 | Preconditions.CheckNotNull(message, "message"); |
| 64 | Preconditions.CheckNotNull(data, "data"); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 65 | CodedInputStream input = data.CreateCodedInput(); |
| 66 | message.MergeFrom(input); |
Jon Skeet | e7f88ff | 2015-08-06 11:40:32 +0100 | [diff] [blame^] | 67 | input.CheckReadEndOfStreamTag(); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 75 | public static void MergeFrom(this IMessage message, Stream input) |
| 76 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 77 | Preconditions.CheckNotNull(message, "message"); |
| 78 | Preconditions.CheckNotNull(input, "input"); |
Jon Skeet | 0e0e0c9 | 2015-08-03 11:08:53 +0100 | [diff] [blame] | 79 | CodedInputStream codedInput = new CodedInputStream(input); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 80 | message.MergeFrom(codedInput); |
Jon Skeet | e7f88ff | 2015-08-06 11:40:32 +0100 | [diff] [blame^] | 81 | codedInput.CheckReadEndOfStreamTag(); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 93 | public static void MergeDelimitedFrom(this IMessage message, Stream input) |
| 94 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 95 | Preconditions.CheckNotNull(message, "message"); |
| 96 | Preconditions.CheckNotNull(input, "input"); |
Jon Skeet | f34d37a | 2015-06-30 13:16:20 +0100 | [diff] [blame] | 97 | int size = (int) CodedInputStream.ReadRawVarint32(input); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 98 | Stream limitedStream = new LimitedInputStream(input, size); |
| 99 | message.MergeFrom(limitedStream); |
| 100 | } |
| 101 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 107 | public static byte[] ToByteArray(this IMessage message) |
| 108 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 109 | Preconditions.CheckNotNull(message, "message"); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 110 | byte[] result = new byte[message.CalculateSize()]; |
Jon Skeet | 0e0e0c9 | 2015-08-03 11:08:53 +0100 | [diff] [blame] | 111 | CodedOutputStream output = new CodedOutputStream(result); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 112 | message.WriteTo(output); |
| 113 | output.CheckNoSpaceLeft(); |
| 114 | return result; |
| 115 | } |
| 116 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 122 | public static void WriteTo(this IMessage message, Stream output) |
| 123 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 124 | Preconditions.CheckNotNull(message, "message"); |
| 125 | Preconditions.CheckNotNull(output, "output"); |
Jon Skeet | 0e0e0c9 | 2015-08-03 11:08:53 +0100 | [diff] [blame] | 126 | CodedOutputStream codedOutput = new CodedOutputStream(output); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 127 | message.WriteTo(codedOutput); |
| 128 | codedOutput.Flush(); |
| 129 | } |
| 130 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 136 | public static void WriteDelimitedTo(this IMessage message, Stream output) |
| 137 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 138 | Preconditions.CheckNotNull(message, "message"); |
| 139 | Preconditions.CheckNotNull(output, "output"); |
Jon Skeet | 0e0e0c9 | 2015-08-03 11:08:53 +0100 | [diff] [blame] | 140 | CodedOutputStream codedOutput = new CodedOutputStream(output); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 141 | codedOutput.WriteRawVarint32((uint)message.CalculateSize()); |
| 142 | message.WriteTo(codedOutput); |
| 143 | codedOutput.Flush(); |
| 144 | } |
| 145 | |
Jon Skeet | 811fc89 | 2015-08-04 15:58:39 +0100 | [diff] [blame] | 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> |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 151 | public static ByteString ToByteString(this IMessage message) |
| 152 | { |
Jon Skeet | 68380f0 | 2015-07-30 13:03:45 +0100 | [diff] [blame] | 153 | Preconditions.CheckNotNull(message, "message"); |
Jon Skeet | e38294a | 2015-06-09 19:30:44 +0100 | [diff] [blame] | 154 | return ByteString.AttachBytes(message.ToByteArray()); |
| 155 | } |
| 156 | } |
| 157 | } |