mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 18:43:10 +08:00
Performance optimizations
This commit is contained in:
@@ -4,36 +4,42 @@ namespace MQTTnet.Core.Serializer
|
||||
{
|
||||
public class ByteReader
|
||||
{
|
||||
private readonly int _source;
|
||||
private int _index;
|
||||
private readonly int _byte;
|
||||
|
||||
public ByteReader(byte @byte)
|
||||
public ByteReader(int source)
|
||||
{
|
||||
_byte = @byte;
|
||||
_source = source;
|
||||
}
|
||||
|
||||
public bool Read()
|
||||
{
|
||||
if (_index >= 8)
|
||||
{
|
||||
throw new InvalidOperationException("End of the byte reached.");
|
||||
throw new InvalidOperationException("End of byte reached.");
|
||||
}
|
||||
|
||||
var result = ((1 << _index) & _byte) > 0;
|
||||
var result = ((1 << _index) & _source) > 0;
|
||||
_index++;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte Read(int count)
|
||||
{
|
||||
if (_index + count > 8)
|
||||
{
|
||||
throw new InvalidOperationException("End of byte will be reached.");
|
||||
}
|
||||
|
||||
var result = 0;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
if (Read())
|
||||
if (((1 << _index) & _source) > 0)
|
||||
{
|
||||
result |= 1 << i;
|
||||
}
|
||||
|
||||
_index++;
|
||||
}
|
||||
|
||||
return (byte)result;
|
||||
|
||||
Reference in New Issue
Block a user