Skip to content

Commit

Permalink
Add span-friendly overloads for reading bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Forest committed Feb 29, 2024
1 parent ed0f8e0 commit 8242005
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Hazel/MessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,18 @@ public string ReadString()
return output;
}


public void ReadBytesAndSize(byte[] destination)
{
int length = ReadPackedInt32();
if (BytesRemaining < length)
{
throw new InvalidDataException($"Read length is longer than message length: {length} of {BytesRemaining}");
}

ReadBytes(length, destination);
}

public byte[] ReadBytesAndSize()
{
int len = this.ReadPackedInt32();
Expand All @@ -388,11 +400,18 @@ public byte[] ReadBytes(int length)
if (this.BytesRemaining < length) throw new InvalidDataException($"Read length is longer than message length: {length} of {this.BytesRemaining}");

byte[] output = new byte[length];
Array.Copy(this.Buffer, this.readHead, output, 0, output.Length);
this.Position += output.Length;
ReadBytes(length, output);
return output;
}

public void ReadBytes(int length, byte[] destination)
{
if (this.BytesRemaining < length) throw new InvalidDataException($"Read length is longer than message length: {length} of {this.BytesRemaining}");

Array.Copy(this.Buffer, this.readHead, destination, 0, length);
this.Position += length;
}

///
public int ReadPackedInt32()
{
Expand Down

0 comments on commit 8242005

Please sign in to comment.