public void ReadUShortSequence()
        {
            var bytes    = new byte[] { 0 };
            var sequence = new ReadOnlySequence <byte>(bytes);

            Assert.Throws <ArgumentException>(() => _ = BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian));

            bytes    = new byte[] { 48, 57, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            bytes    = new byte[] { 57, 48, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.LittleEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            var bytes1       = new byte[] { 48 };
            var bytes2       = new byte[] { 57, 0, 0, 0 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));
        }
Beispiel #2
0
 /// <summary>
 /// Reads the flags of the frame from a sequence of bytes, and slice the sequence accordingly.
 /// </summary>
 /// <param name="bytes">The sequence of bytes.</param>
 /// <returns>The flags of the frame.</returns>
 public static FrameFlags ReadFlags(ref ReadOnlySequence <byte> bytes)
 => (FrameFlags)BytesExtensions.ReadUShort(ref bytes, Endianness.LittleEndian);