diff options
author | Jan Tattermusch <jtattermusch@google.com> | 2017-11-09 14:23:14 +0100 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@google.com> | 2017-11-10 09:00:52 +0100 |
commit | 435f61102bf23b9c9be2592d27b8935a8717e610 (patch) | |
tree | abef16d8e433a61b525dde7f5693afff312adcbc | |
parent | 5eb717c3006ad7da9aa6e49d40328a3963ad22a3 (diff) | |
download | protobuf-435f61102bf23b9c9be2592d27b8935a8717e610.tar.gz protobuf-435f61102bf23b9c9be2592d27b8935a8717e610.tar.bz2 protobuf-435f61102bf23b9c9be2592d27b8935a8717e610.zip |
allow message parsing from an array slice
-rw-r--r-- | csharp/src/Google.Protobuf/MessageExtensions.cs | 16 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf/MessageParser.cs | 15 |
2 files changed, 31 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/MessageExtensions.cs b/csharp/src/Google.Protobuf/MessageExtensions.cs index 047156c3..9dbc49d6 100644 --- a/csharp/src/Google.Protobuf/MessageExtensions.cs +++ b/csharp/src/Google.Protobuf/MessageExtensions.cs @@ -54,6 +54,22 @@ namespace Google.Protobuf } /// <summary> + /// Merges data from the given byte array slice into an existing message. + /// </summary> + /// <param name="message">The message to merge the data into.</param> + /// <param name="data">The data containing the slice to merge, which must be protobuf-encoded binary data.</param> + /// <param name="offset">The offset of the slice to merge.</param> + /// <param name="length">The length of the slice to merge.</param> + public static void MergeFrom(this IMessage message, byte[] data, int offset, int length) + { + ProtoPreconditions.CheckNotNull(message, "message"); + ProtoPreconditions.CheckNotNull(data, "data"); + CodedInputStream input = new CodedInputStream(data, offset, length); + message.MergeFrom(input); + input.CheckReadEndOfStreamTag(); + } + + /// <summary> /// Merges data from the given byte string into an existing message. /// </summary> /// <param name="message">The message to merge the data into.</param> diff --git a/csharp/src/Google.Protobuf/MessageParser.cs b/csharp/src/Google.Protobuf/MessageParser.cs index 8889638b..569414b0 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -71,6 +71,21 @@ namespace Google.Protobuf } /// <summary> + /// Parses a message from a byte array slice. + /// </summary> + /// <param name="data">The byte array containing the message. Must not be null.</param> + /// <param name="offset">The offset of the slice to parse.</param> + /// <param name="length">The length of the slice to parse.</param> + /// <returns>The newly parsed message.</returns> + public IMessage ParseFrom(byte[] data, int offset, int length) + { + ProtoPreconditions.CheckNotNull(data, "data"); + IMessage message = factory(); + message.MergeFrom(data, offset, length); + return message; + } + + /// <summary> /// Parses a message from the given byte string. /// </summary> /// <param name="data">The data to parse.</param> |