Improve payload handling and allocation.

This commit is contained in:
Christian Kratky
2019-04-08 22:29:29 +02:00
parent 7855eccb78
commit 45c033f401
7 changed files with 57 additions and 53 deletions
+6 -9
View File
@@ -79,24 +79,21 @@ namespace MQTTnet.Formatter
else
{
WriteWithLengthPrefix(Encoding.UTF8.GetBytes(value));
}
}
}
public void WriteWithLengthPrefix(byte[] value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
EnsureAdditionalCapacity(value.Length + 2);
if (value.Length == 0)
if (value == null || value.Length == 0)
{
Write(ZeroTwoByteIntegerArray);
Write(ZeroTwoByteIntegerArray);
}
else
{
EnsureAdditionalCapacity(value.Length + 2);
Write((ushort)value.Length);
Write(value, 0, value.Length);
}
}
}
public void Write(byte @byte)
@@ -131,7 +128,7 @@ namespace MQTTnet.Formatter
Array.Copy(buffer, offset, _buffer, _offset, count);
IncreasePosition(count);
}
public void Write(MqttPacketWriter propertyWriter)
{
if (propertyWriter == null) throw new ArgumentNullException(nameof(propertyWriter));