diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2017-11-10 08:57:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-10 08:57:40 +0100 |
commit | ce0a53273a400369932ba788d17500336a6ecaad (patch) | |
tree | cb4690745792cad35e603cd41bdfd586eafd182e /csharp/src/Google.Protobuf | |
parent | c258fb303a804241cb2678dc732ed870a0ee97c8 (diff) | |
parent | 30b6e543277901fb95373138d358ae5d53048977 (diff) | |
download | protobuf-ce0a53273a400369932ba788d17500336a6ecaad.tar.gz protobuf-ce0a53273a400369932ba788d17500336a6ecaad.tar.bz2 protobuf-ce0a53273a400369932ba788d17500336a6ecaad.zip |
Merge pull request #3858 from jtattermusch/parsing_from_slice
C#: Allow message parsing from an array slice
Diffstat (limited to 'csharp/src/Google.Protobuf')
-rw-r--r-- | csharp/src/Google.Protobuf/MessageExtensions.cs | 16 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf/MessageParser.cs | 32 |
2 files changed, 44 insertions, 4 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..66d44135 100644 --- a/csharp/src/Google.Protobuf/MessageParser.cs +++ b/csharp/src/Google.Protobuf/MessageParser.cs @@ -64,20 +64,32 @@ namespace Google.Protobuf /// <returns>The newly parsed message.</returns> public IMessage ParseFrom(byte[] data) { - ProtoPreconditions.CheckNotNull(data, "data"); IMessage message = factory(); message.MergeFrom(data); return message; } /// <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) + { + 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> /// <returns>The parsed message.</returns> public IMessage ParseFrom(ByteString data) { - ProtoPreconditions.CheckNotNull(data, "data"); IMessage message = factory(); message.MergeFrom(data); return message; @@ -191,20 +203,32 @@ namespace Google.Protobuf /// <returns>The newly parsed message.</returns> public new T ParseFrom(byte[] data) { - ProtoPreconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; } /// <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 new T ParseFrom(byte[] data, int offset, int length) + { + T 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> /// <returns>The parsed message.</returns> public new T ParseFrom(ByteString data) { - ProtoPreconditions.CheckNotNull(data, "data"); T message = factory(); message.MergeFrom(data); return message; |