From 435f61102bf23b9c9be2592d27b8935a8717e610 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 9 Nov 2017 14:23:14 +0100 Subject: allow message parsing from an array slice --- csharp/src/Google.Protobuf/MessageExtensions.cs | 16 ++++++++++++++++ csharp/src/Google.Protobuf/MessageParser.cs | 15 +++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'csharp') 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 @@ -53,6 +53,22 @@ namespace Google.Protobuf input.CheckReadEndOfStreamTag(); } + /// + /// Merges data from the given byte array slice into an existing message. + /// + /// The message to merge the data into. + /// The data containing the slice to merge, which must be protobuf-encoded binary data. + /// The offset of the slice to merge. + /// The length of the slice to merge. + 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(); + } + /// /// Merges data from the given byte string into an existing message. /// 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 @@ -70,6 +70,21 @@ namespace Google.Protobuf return message; } + /// + /// Parses a message from a byte array slice. + /// + /// The byte array containing the message. Must not be null. + /// The offset of the slice to parse. + /// The length of the slice to parse. + /// The newly parsed message. + public IMessage ParseFrom(byte[] data, int offset, int length) + { + ProtoPreconditions.CheckNotNull(data, "data"); + IMessage message = factory(); + message.MergeFrom(data, offset, length); + return message; + } + /// /// Parses a message from the given byte string. /// -- cgit v1.2.3