Reader/Writer: sort WriteBytes(AndSize) functions

This commit is contained in:
mischa 2023-08-12 19:04:12 +04:00
parent f05df29639
commit 4d7c5b6e41
2 changed files with 18 additions and 15 deletions

View File

@ -78,16 +78,6 @@ public static string ReadString(this NetworkReader reader)
return reader.encoding.GetString(data.Array, data.Offset, data.Count);
}
/// <exception cref="T:OverflowException">if count is invalid</exception>
public static byte[] ReadBytesAndSize(this NetworkReader reader)
{
// count = 0 means the array was null
// otherwise count -1 is the length of the array
uint count = reader.ReadUInt();
// Use checked() to force it to throw OverflowException if data is invalid
return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
}
public static byte[] ReadBytes(this NetworkReader reader, int count)
{
// prevent allocation attacks with a reasonable limit.
@ -104,6 +94,17 @@ public static byte[] ReadBytes(this NetworkReader reader, int count)
return bytes;
}
/// <exception cref="T:OverflowException">if count is invalid</exception>
public static byte[] ReadBytesAndSize(this NetworkReader reader)
{
// count = 0 means the array was null
// otherwise count -1 is the length of the array
uint count = reader.ReadUInt();
// Use checked() to force it to throw OverflowException if data is invalid
return count == 0 ? null : reader.ReadBytes(checked((int)(count - 1u)));
}
// Reads ArraySegment and size header
/// <exception cref="T:OverflowException">if count is invalid</exception>
public static ArraySegment<byte> ReadBytesAndSizeSegment(this NetworkReader reader)
{

View File

@ -82,11 +82,6 @@ public static void WriteString(this NetworkWriter writer, string value)
writer.Position += written;
}
public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment<byte> buffer)
{
writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
}
// Weaver needs a write function with just one byte[] parameter
// (we don't name it .Write(byte[]) because it's really a WriteBytesAndSize since we write size / null info too)
@ -113,6 +108,13 @@ public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer, i
writer.WriteBytes(buffer, offset, count);
}
// writes ArraySegment and size header
public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment<byte> buffer)
{
writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
}
// writes ArraySegment and size header
public static void WriteArraySegment<T>(this NetworkWriter writer, ArraySegment<T> segment)
{
int length = segment.Count;