Skip to content

Commit

Permalink
Decode short and medium strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 5, 2024
1 parent 94a4bdd commit 8cef699
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
61 changes: 61 additions & 0 deletions src/ArtemisNetCoreClient/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,65 @@ private void WriteShort(short value)
BinaryPrimitives.WriteInt16BigEndian(buffer, value);
_memoryStream.Write(buffer);
}

private short ReadShort()
{
Span<byte> buffer = stackalloc byte[sizeof(short)];
_ = _memoryStream.Read(buffer);
return BinaryPrimitives.ReadInt16BigEndian(buffer);
}

public string ReadString()
{
var length = ReadInt();
if (length < 9)
{
return ReadAsShorts(length);
}
else if (length < 0xFFF)
{
var actualByteCount = ReadShort();
if (actualByteCount < 128)
{
Span<byte> buffer = stackalloc byte[actualByteCount];
_ = _memoryStream.Read(buffer);
return Encoding.UTF8.GetString(buffer);
}
else
{

var rented = ArrayPool<byte>.Shared.Rent(actualByteCount);
try
{
_ = _memoryStream.Read(rented, 0, actualByteCount);
return Encoding.UTF8.GetString(rented, 0, actualByteCount);
}
finally
{
ArrayPool<byte>.Shared.Return(rented);
}
}
}
else
{
return ReadAsBytes();
}
}

private string ReadAsBytes()
{
return string.Empty;
}

private string ReadAsShorts(int length)
{
Span<char> chars = stackalloc char[length];
for (int i = 0; i < length; i++)
{
var c = (char) ReadShort();
chars[i] = c;
}

return new string(chars);
}
}
Loading

0 comments on commit 8cef699

Please sign in to comment.