コード例 #1
0
        public static void StreamInterop()
        {
            using var writer = new PooledArrayBufferWriter <byte>();
            var span = writer.GetSpan(10);

            new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.AsSpan().CopyTo(span);
            writer.Advance(10);
            using var stream = StreamSource.GetWrittenBytesAsStream(writer);
            True(stream.CanRead);
            False(stream.CanWrite);
            Equal(0, stream.Position);
            Equal(10, stream.Length);
            var buffer = new byte[10];

            Equal(10, stream.Read(buffer, 0, 10));
            for (var i = 0; i < buffer.Length; i++)
            {
                Equal(i, buffer[i]);
            }
        }
コード例 #2
0
        public static void StressTest()
        {
            var dict = new Dictionary <string, string>
            {
                { "Key1", "Value1" },
                { "Key2", "Value2" }
            };
            var formatter = new BinaryFormatter();

            using var writer = new PooledArrayBufferWriter <byte>();
            // serialize dictionary to memory
            using (var output = StreamSource.AsStream(writer))
            {
                formatter.Serialize(output, dict);
            }
            // deserialize from memory
            using (var input = StreamSource.GetWrittenBytesAsStream(writer))
            {
                Equal(dict, formatter.Deserialize(input));
            }
        }