Example #1
0
        public void SimpleWriteAndRead()
        {
            var random = new Random(42);

            var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions()
            {
                MinimumSegmentSize = 16
            });

            var ms          = new MemoryStream();
            var stream      = manager.GetStream();
            var writeStream = new BroadcastWriteStream(ms, stream);

            var buffer = new byte[32];

            random.NextBytes(buffer);

            writeStream.Write(buffer, 0, 4);
            writeStream.Write(buffer, 0, 8);
            writeStream.Write(buffer, 0, 12);
            writeStream.Write(buffer, 0, 16);
            writeStream.Write(buffer, 0, 32);

            writeStream.Write(buffer.AsSpan(0, 4));
            writeStream.Write(buffer.AsSpan(0, 8));
            writeStream.Write(buffer.AsSpan(0, 12));
            writeStream.Write(buffer.AsSpan(0, 16));
            writeStream.Write(buffer.AsSpan(0, 32));

            AssertHelper.StreamEqual(ms, stream);
        }
Example #2
0
        public void RandomStreamWriteAndReadTest(int seed, int maxChunkSize, int minTotalSize)
        {
            var random = new Random(seed);

            var ms          = new MemoryStream();
            var stream      = new PooledMemoryStream();
            var writeStream = new BroadcastWriteStream(ms, stream);

            int length = 0;

            byte[] buffer = new byte[maxChunkSize];

            bool useSpan = false;

            while (length < minTotalSize)
            {
                int chunkSize = random.Next(1, maxChunkSize);
                random.NextBytes(buffer);

                if (useSpan)
                {
                    writeStream.Write(buffer.AsSpan(0, chunkSize));
                }
                else
                {
                    writeStream.Write(buffer, 0, chunkSize);
                }
                useSpan = !useSpan;
                length += chunkSize;
            }

            AssertHelper.StreamEqual(ms, stream);
        }
Example #3
0
        public void WriteWithSeek(bool useSpan)
        {
            var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions()
            {
                MinimumSegmentSize = 16
            });

            var ms = new MemoryStream();
            PooledMemoryStream stream = manager.GetStream();

            var writeStream = new BroadcastWriteStream(ms, stream);

            byte[] buffer = new byte[8];
            buffer.AsSpan().Fill(1);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(2);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(3);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            buffer.AsSpan().Fill(4);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 8));
            }
            else
            {
                writeStream.Write(buffer, 0, 8);
            }

            ms.Seek(24, SeekOrigin.Begin);
            stream.Seek(24, SeekOrigin.Begin);

            buffer.AsSpan().Fill(5);
            if (useSpan)
            {
                writeStream.Write(buffer.AsSpan(0, 4));
            }
            else
            {
                writeStream.Write(buffer, 0, 4);
            }

            AssertHelper.StreamEqual(ms, stream);
        }