Esempio n. 1
0
        public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()
        {
            var writer = new ArrayPoolBufferWriter <byte>();

            Assert.AreEqual(writer.Capacity, 256);
            Assert.AreEqual(writer.FreeCapacity, 256);
            Assert.AreEqual(writer.WrittenCount, 0);
            Assert.IsTrue(writer.WrittenMemory.IsEmpty);
            Assert.IsTrue(writer.WrittenSpan.IsEmpty);

            Span <byte> span = writer.GetSpan(43);

            Assert.IsTrue(span.Length >= 43);

            writer.Advance(43);

            Assert.AreEqual(writer.Capacity, 256);
            Assert.AreEqual(writer.FreeCapacity, 256 - 43);
            Assert.AreEqual(writer.WrittenCount, 43);
            Assert.AreEqual(writer.WrittenMemory.Length, 43);
            Assert.AreEqual(writer.WrittenSpan.Length, 43);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.Advance(-1));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
            Assert.ThrowsException <ArgumentException>(() => writer.Advance(1024));

            writer.Dispose();

            Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenMemory);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenSpan.Length);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Capacity);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.FreeCapacity);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Clear());
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Advance(1));
        }
Esempio n. 2
0
        private async Task BufferFromStreamAsync(Stream stream, ArrayPoolBufferWriter <byte> buf, int length, CancellationToken token)
        {
            WriteHeader(buf, length);
            Memory <byte> mem     = buf.GetMemory(length).Slice(0, length);
            int           totRead = await stream.ReadAsync(mem, token).ConfigureAwait(false);

            token.ThrowIfCancellationRequested();
            if (totRead != length)
            {
                throw new StreamSerializationException($"Unexpected length read while deserializing body. Expected {length}, got {totRead}");
            }
            buf.Advance(length);
        }