Merge pull request #3858 from jtattermusch/parsing_from_slice

C#: Allow message parsing from an array slice
pull/3874/head
Jan Tattermusch 7 years ago committed by GitHub
commit ce0a53273a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      csharp/src/Google.Protobuf/MessageExtensions.cs
  2. 32
      csharp/src/Google.Protobuf/MessageParser.cs

@ -53,6 +53,22 @@ namespace Google.Protobuf
input.CheckReadEndOfStreamTag();
}
/// <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>

@ -64,12 +64,25 @@ 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>
@ -77,7 +90,6 @@ namespace Google.Protobuf
/// <returns>The parsed message.</returns>
public IMessage ParseFrom(ByteString data)
{
ProtoPreconditions.CheckNotNull(data, "data");
IMessage message = factory();
message.MergeFrom(data);
return message;
@ -191,12 +203,25 @@ 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>
@ -204,7 +229,6 @@ namespace Google.Protobuf
/// <returns>The parsed message.</returns>
public new T ParseFrom(ByteString data)
{
ProtoPreconditions.CheckNotNull(data, "data");
T message = factory();
message.MergeFrom(data);
return message;

Loading…
Cancel
Save