OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2015 Google Inc. All rights reserved. | 3 // Copyright 2015 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 28 matching lines...) Expand all Loading... |
39 /// </summary> | 39 /// </summary> |
40 public static class MessageExtensions | 40 public static class MessageExtensions |
41 { | 41 { |
42 /// <summary> | 42 /// <summary> |
43 /// Merges data from the given byte array into an existing message. | 43 /// Merges data from the given byte array into an existing message. |
44 /// </summary> | 44 /// </summary> |
45 /// <param name="message">The message to merge the data into.</param> | 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> | 46 /// <param name="data">The data to merge, which must be protobuf-encoded
binary data.</param> |
47 public static void MergeFrom(this IMessage message, byte[] data) | 47 public static void MergeFrom(this IMessage message, byte[] data) |
48 { | 48 { |
49 Preconditions.CheckNotNull(message, "message"); | 49 ProtoPreconditions.CheckNotNull(message, "message"); |
50 Preconditions.CheckNotNull(data, "data"); | 50 ProtoPreconditions.CheckNotNull(data, "data"); |
51 CodedInputStream input = new CodedInputStream(data); | 51 CodedInputStream input = new CodedInputStream(data); |
52 message.MergeFrom(input); | 52 message.MergeFrom(input); |
53 input.CheckReadEndOfStreamTag(); | 53 input.CheckReadEndOfStreamTag(); |
54 } | 54 } |
55 | 55 |
56 /// <summary> | 56 /// <summary> |
57 /// Merges data from the given byte string into an existing message. | 57 /// Merges data from the given byte string into an existing message. |
58 /// </summary> | 58 /// </summary> |
59 /// <param name="message">The message to merge the data into.</param> | 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> | 60 /// <param name="data">The data to merge, which must be protobuf-encoded
binary data.</param> |
61 public static void MergeFrom(this IMessage message, ByteString data) | 61 public static void MergeFrom(this IMessage message, ByteString data) |
62 { | 62 { |
63 Preconditions.CheckNotNull(message, "message"); | 63 ProtoPreconditions.CheckNotNull(message, "message"); |
64 Preconditions.CheckNotNull(data, "data"); | 64 ProtoPreconditions.CheckNotNull(data, "data"); |
65 CodedInputStream input = data.CreateCodedInput(); | 65 CodedInputStream input = data.CreateCodedInput(); |
66 message.MergeFrom(input); | 66 message.MergeFrom(input); |
67 input.CheckReadEndOfStreamTag(); | 67 input.CheckReadEndOfStreamTag(); |
68 } | 68 } |
69 | 69 |
70 /// <summary> | 70 /// <summary> |
71 /// Merges data from the given stream into an existing message. | 71 /// Merges data from the given stream into an existing message. |
72 /// </summary> | 72 /// </summary> |
73 /// <param name="message">The message to merge the data into.</param> | 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> | 74 /// <param name="input">Stream containing the data to merge, which must
be protobuf-encoded binary data.</param> |
75 public static void MergeFrom(this IMessage message, Stream input) | 75 public static void MergeFrom(this IMessage message, Stream input) |
76 { | 76 { |
77 Preconditions.CheckNotNull(message, "message"); | 77 ProtoPreconditions.CheckNotNull(message, "message"); |
78 Preconditions.CheckNotNull(input, "input"); | 78 ProtoPreconditions.CheckNotNull(input, "input"); |
79 CodedInputStream codedInput = new CodedInputStream(input); | 79 CodedInputStream codedInput = new CodedInputStream(input); |
80 message.MergeFrom(codedInput); | 80 message.MergeFrom(codedInput); |
81 codedInput.CheckReadEndOfStreamTag(); | 81 codedInput.CheckReadEndOfStreamTag(); |
82 } | 82 } |
83 | 83 |
84 /// <summary> | 84 /// <summary> |
85 /// Merges length-delimited data from the given stream into an existing
message. | 85 /// Merges length-delimited data from the given stream into an existing
message. |
86 /// </summary> | 86 /// </summary> |
87 /// <remarks> | 87 /// <remarks> |
88 /// The stream is expected to contain a length and then the data. Only t
he amount of data | 88 /// The stream is expected to contain a length and then the data. Only t
he amount of data |
89 /// specified by the length will be consumed. | 89 /// specified by the length will be consumed. |
90 /// </remarks> | 90 /// </remarks> |
91 /// <param name="message">The message to merge the data into.</param> | 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> | 92 /// <param name="input">Stream containing the data to merge, which must
be protobuf-encoded binary data.</param> |
93 public static void MergeDelimitedFrom(this IMessage message, Stream inpu
t) | 93 public static void MergeDelimitedFrom(this IMessage message, Stream inpu
t) |
94 { | 94 { |
95 Preconditions.CheckNotNull(message, "message"); | 95 ProtoPreconditions.CheckNotNull(message, "message"); |
96 Preconditions.CheckNotNull(input, "input"); | 96 ProtoPreconditions.CheckNotNull(input, "input"); |
97 int size = (int) CodedInputStream.ReadRawVarint32(input); | 97 int size = (int) CodedInputStream.ReadRawVarint32(input); |
98 Stream limitedStream = new LimitedInputStream(input, size); | 98 Stream limitedStream = new LimitedInputStream(input, size); |
99 message.MergeFrom(limitedStream); | 99 message.MergeFrom(limitedStream); |
100 } | 100 } |
101 | 101 |
102 /// <summary> | 102 /// <summary> |
103 /// Converts the given message into a byte array in protobuf encoding. | 103 /// Converts the given message into a byte array in protobuf encoding. |
104 /// </summary> | 104 /// </summary> |
105 /// <param name="message">The message to convert.</param> | 105 /// <param name="message">The message to convert.</param> |
106 /// <returns>The message data as a byte array.</returns> | 106 /// <returns>The message data as a byte array.</returns> |
107 public static byte[] ToByteArray(this IMessage message) | 107 public static byte[] ToByteArray(this IMessage message) |
108 { | 108 { |
109 Preconditions.CheckNotNull(message, "message"); | 109 ProtoPreconditions.CheckNotNull(message, "message"); |
110 byte[] result = new byte[message.CalculateSize()]; | 110 byte[] result = new byte[message.CalculateSize()]; |
111 CodedOutputStream output = new CodedOutputStream(result); | 111 CodedOutputStream output = new CodedOutputStream(result); |
112 message.WriteTo(output); | 112 message.WriteTo(output); |
113 output.CheckNoSpaceLeft(); | 113 output.CheckNoSpaceLeft(); |
114 return result; | 114 return result; |
115 } | 115 } |
116 | 116 |
117 /// <summary> | 117 /// <summary> |
118 /// Writes the given message data to the given stream in protobuf encodi
ng. | 118 /// Writes the given message data to the given stream in protobuf encodi
ng. |
119 /// </summary> | 119 /// </summary> |
120 /// <param name="message">The message to write to the stream.</param> | 120 /// <param name="message">The message to write to the stream.</param> |
121 /// <param name="output">The stream to write to.</param> | 121 /// <param name="output">The stream to write to.</param> |
122 public static void WriteTo(this IMessage message, Stream output) | 122 public static void WriteTo(this IMessage message, Stream output) |
123 { | 123 { |
124 Preconditions.CheckNotNull(message, "message"); | 124 ProtoPreconditions.CheckNotNull(message, "message"); |
125 Preconditions.CheckNotNull(output, "output"); | 125 ProtoPreconditions.CheckNotNull(output, "output"); |
126 CodedOutputStream codedOutput = new CodedOutputStream(output); | 126 CodedOutputStream codedOutput = new CodedOutputStream(output); |
127 message.WriteTo(codedOutput); | 127 message.WriteTo(codedOutput); |
128 codedOutput.Flush(); | 128 codedOutput.Flush(); |
129 } | 129 } |
130 | 130 |
131 /// <summary> | 131 /// <summary> |
132 /// Writes the length and then data of the given message to a stream. | 132 /// Writes the length and then data of the given message to a stream. |
133 /// </summary> | 133 /// </summary> |
134 /// <param name="message">The message to write.</param> | 134 /// <param name="message">The message to write.</param> |
135 /// <param name="output">The output stream to write to.</param> | 135 /// <param name="output">The output stream to write to.</param> |
136 public static void WriteDelimitedTo(this IMessage message, Stream output
) | 136 public static void WriteDelimitedTo(this IMessage message, Stream output
) |
137 { | 137 { |
138 Preconditions.CheckNotNull(message, "message"); | 138 ProtoPreconditions.CheckNotNull(message, "message"); |
139 Preconditions.CheckNotNull(output, "output"); | 139 ProtoPreconditions.CheckNotNull(output, "output"); |
140 CodedOutputStream codedOutput = new CodedOutputStream(output); | 140 CodedOutputStream codedOutput = new CodedOutputStream(output); |
141 codedOutput.WriteRawVarint32((uint)message.CalculateSize()); | 141 codedOutput.WriteRawVarint32((uint)message.CalculateSize()); |
142 message.WriteTo(codedOutput); | 142 message.WriteTo(codedOutput); |
143 codedOutput.Flush(); | 143 codedOutput.Flush(); |
144 } | 144 } |
145 | 145 |
146 /// <summary> | 146 /// <summary> |
147 /// Converts the given message into a byte string in protobuf encoding. | 147 /// Converts the given message into a byte string in protobuf encoding. |
148 /// </summary> | 148 /// </summary> |
149 /// <param name="message">The message to convert.</param> | 149 /// <param name="message">The message to convert.</param> |
150 /// <returns>The message data as a byte string.</returns> | 150 /// <returns>The message data as a byte string.</returns> |
151 public static ByteString ToByteString(this IMessage message) | 151 public static ByteString ToByteString(this IMessage message) |
152 { | 152 { |
153 Preconditions.CheckNotNull(message, "message"); | 153 ProtoPreconditions.CheckNotNull(message, "message"); |
154 return ByteString.AttachBytes(message.ToByteArray()); | 154 return ByteString.AttachBytes(message.ToByteArray()); |
155 } | 155 } |
156 } | 156 } |
157 } | 157 } |
OLD | NEW |