Skip to content

Commit

Permalink
ProtoWriter: Add ReadRawBytes(Span) methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Executor-Cheng committed Mar 26, 2024
1 parent b296a31 commit 0d1b421
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions KonataNT.Proto/Serialization/ProtoReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ private unsafe T ReadRawIntegerUnchecked<T>(ref T ptr, nuint position) where T :
return result;
}

public ReadOnlySpan<byte> ReadRawBytesSpan(uint length)
{
if (Length - _position < length)
{
ThrowMalformedMessage();
}
var span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.Add(ref First, _position), (int)length);
_position += length;
return span;
}

public byte[] ReadRawBytes(uint length)
{
return ReadRawBytesSpan(length).ToArray();
}

[DoesNotReturn]
public static void ThrowMalformedMessage()
{
Expand Down

0 comments on commit 0d1b421

Please sign in to comment.