Skip to content

Commit

Permalink
improve calculation of buffer for utf 8
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen committed Jul 4, 2024
1 parent a3fb0fa commit 1b86ded
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Source/MQTTnet/Buffers/MqttBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ public void WriteString(string value)
// UTF8 chars can have a max length of 4 and the used buffer increase *2 every time.
// So the buffer should always have much more capacity left so that a correct value
// here is only waste of CPU cycles.
var byteCount = value.Length * 4;
var maxByteCount = Encoding.UTF8.GetMaxByteCount(value.Length);

EnsureAdditionalCapacity(byteCount + 2);
EnsureAdditionalCapacity(maxByteCount + 2);

var writtenBytes = Encoding.UTF8.GetBytes(value, 0, value.Length, _buffer, _position + 2);

Expand Down

0 comments on commit 1b86ded

Please sign in to comment.