public void ShrinkFromTwoSegment()
        {
            PooledMemoryStream stream = s_manager.GetStream();

            byte[] buffer = new byte[12];
            new Random(42).NextBytes(buffer);
            stream.Write(buffer);
            stream.Write(buffer);

            Assert.Equal(32, stream.Capacity);
            Assert.Equal(24, stream.Length);
            Assert.Equal(24, stream.Position);

            stream.SetLength(12);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(12, stream.Length);
            Assert.Equal(12, stream.Position);

            stream.Seek(0, SeekOrigin.Begin);

            byte[] buffer2 = new byte[12];
            Assert.Equal(12, stream.Read(buffer2));

            Assert.True(buffer2.AsSpan(0, 12).SequenceEqual(buffer.AsSpan(0, 12)));
        }
Ejemplo n.º 2
0
        internal Task ProcessAndSendMessage(MessageEventArgs e, EncryptContext context)
        {
            var         message     = (ProudMessage)e.Message;
            var         data        = message.ToArray();
            CoreMessage coreMessage = new RmiMessage(data);

            if (message.Compress)
            {
                data        = coreMessage.ToArray();
                coreMessage = new CompressedMessage(data.Length, data.CompressZLib());
            }

            if (message.Encrypt)
            {
                data = coreMessage.ToArray();
                using (var w = new PooledMemoryStream(Service.ArrayPool).ToBinaryWriter(false))
                {
                    w.Write(context.EncryptCounter);
                    w.Write(data);

                    data = w.ToArray();
                }

                data        = context.Encrypt(data);
                coreMessage = new EncryptedReliableMessage(data);
            }

            e.Message = coreMessage;
            return(base.OnSendMessage(e));
        }
Ejemplo n.º 3
0
 public byte[] GetBytes()
 {
     using var ms = new PooledMemoryStream();
     ms.Write(BitConverter.GetBytes(Version));
     ms.Write(BitConverter.GetBytes(Timestamp));
     return(ms.ToArray());
 }
Ejemplo n.º 4
0
        public void TestDispose()
        {
            var stm = new PooledMemoryStream();

            stm.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
            stm.Dispose();
            stm.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
            stm.Dispose();
        }
Ejemplo n.º 5
0
        public void Can_Rewrite_Stream()
        {
            var pool   = new BufferManagerAllocator(1024, 1024 * 1024);
            var stream = new PooledMemoryStream(pool);

            var chunk = new byte[60];

            for (var i = 0; i < 10; i++)
            {
                Fill(chunk, (byte)(i + 1));
                stream.Write(chunk, 0, chunk.Length);

                Assert.Equal((i + 1) * 60, stream.Position);
                Assert.Equal((i + 1) * 60, stream.Length);
            }

            stream.Position = 120;
            Fill(chunk, 200);
            stream.Write(chunk, 0, chunk.Length);

            stream.Position = 370;
            Fill(chunk, 100);
            stream.Write(chunk, 0, chunk.Length);

            Assert.Equal(430, stream.Position);
            Assert.Equal(600, stream.Length);

            var all = new byte[stream.Length];

            stream.Position = 0;
            stream.Read(all, 0, all.Length);

            Assert.Equal(600, stream.Position);
            Assert.Equal(600, stream.Length);

            var expected = new[]
            {
                Enumerable.Repeat(1, 60),                 // 0
                Enumerable.Repeat(2, 60),                 // 60
                Enumerable.Repeat(200, 60),               // 120
                Enumerable.Repeat(4, 60),                 // 180
                Enumerable.Repeat(5, 60),                 // 240
                Enumerable.Repeat(6, 60),                 // 300
                Enumerable.Repeat(7, 10),                 // 360
                Enumerable.Repeat(100, 60),               // 370
                Enumerable.Repeat(8, 50),                 // 430
                Enumerable.Repeat(9, 60),                 // 480
                Enumerable.Repeat(10, 60),                // 540
            };

            var expectedValue = expected.SelectMany(_ => _.Select(v => (byte)v)).ToArray();

            Assert.Equal(expectedValue.Length, all.Length);
            Assert.Equal(stream.Length, all.Length);

            AssertArray(expectedValue, all);
        }
Ejemplo n.º 6
0
        public void Segmented_Data_Written_Can_Be_Read_Back_In_Segments()
        {
            var       sizes      = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024 };
            var       pool       = new BufferManagerAllocator(1024, 1024 * 1024);
            const int STEP_WRITE = 37;

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var  remaining = size;
                    var  data      = new byte[STEP_WRITE];
                    byte value     = 1;

                    for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, data.Length);
                        value++;
                        remaining -= STEP_WRITE;
                    }

                    if (remaining > 0)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, remaining);
                    }

                    Assert.Equal(size, stream.Position);
                    Assert.Equal(size, stream.Length);

                    stream.Position = 0;
                    Assert.Equal(0, stream.Position);
                    var copy = new byte[STEP_WRITE];

                    value = 1;

                    for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
                    {
                        Fill(data, value);
                        Fill(copy, (byte)(value - 1));
                        stream.Read(copy, 0, copy.Length);
                        value++;
                        remaining -= STEP_WRITE;

                        Assert.Equal(data, copy);
                    }

                    if (remaining > 0)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, remaining);
                        Assert.Equal(data.Take(remaining), copy.Take(remaining));
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public byte[] GetBytes()
        {
            var eventTypeBytes = Encoding.Default.GetBytes(TypeFullName);

            using var ms = new PooledMemoryStream();
            ms.WriteByte((byte)TransportType.Common);
            ms.Write(BitConverter.GetBytes((ushort)eventTypeBytes.Length));
            ms.Write(Bytes);
            return(ms.ToArray());
        }
Ejemplo n.º 8
0
        public void TestWriteMiddle()
        {
            var data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared))
            {
                stm.Write(data, 0, data.Length);
                stm.Seek(4, SeekOrigin.Begin);
                stm.Write(data, 0, data.Length);
                var ar = stm.ToArray();
                Assert.Equal(data.Take(4).Concat(data), ar);
            }
        }
Ejemplo n.º 9
0
        public byte[] GetBytes()
        {
            var eventTypeBytes = Encoding.UTF8.GetBytes(EventTypeCode);

            byte[] actorIdBytes;
            if (GrainId is long id)
            {
                actorIdBytes = BitConverter.GetBytes(id);
            }
            else if (GrainId is string strId)
            {
                actorIdBytes = Encoding.UTF8.GetBytes(strId);
            }
            else
            {
                throw new PrimaryKeyTypeException(EventTypeCode);
            }
            using var ms = new PooledMemoryStream();
            ms.WriteByte((byte)TransportType.Event);
            ms.Write(BitConverter.GetBytes((ushort)eventTypeBytes.Length));
            ms.Write(BitConverter.GetBytes((ushort)actorIdBytes.Length));
            ms.Write(BitConverter.GetBytes((ushort)BaseBytes.Length));
            ms.Write(BitConverter.GetBytes(EventBytes.Length));
            ms.Write(eventTypeBytes);
            ms.Write(actorIdBytes);
            ms.Write(BaseBytes);
            ms.Write(EventBytes);
            return(ms.ToArray());
        }
Ejemplo n.º 10
0
 public byte[] GetBytes()
 {
     if (allBytes == default)
     {
         var    eventTypeBytes = Encoding.Default.GetBytes(EventType);
         byte[] actorIdBytes;
         if (ActorId is long id)
         {
             actorIdBytes = BitConverter.GetBytes(id);
         }
         else if (ActorId is string strId)
         {
             actorIdBytes = Encoding.Default.GetBytes(strId);
         }
         else
         {
             throw new PrimaryKeyTypeException(EventType);
         }
         using (var ms = new PooledMemoryStream())
         {
             ms.WriteByte((byte)TransportType.Event);
             ms.Write(BitConverter.GetBytes((ushort)eventTypeBytes.Length));
             ms.Write(BitConverter.GetBytes((ushort)actorIdBytes.Length));
             ms.Write(BitConverter.GetBytes((ushort)BaseBytes.Length));
             ms.Write(BitConverter.GetBytes(EventBytes.Length));
             ms.Write(eventTypeBytes);
             ms.Write(actorIdBytes);
             ms.Write(BaseBytes);
             ms.Write(EventBytes);
             allBytes = ms.ToArray();
         }
     }
     return(allBytes);
 }
		public void Data_Written_Can_Be_Read_Back()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			byte i = 1;

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var data = new byte[size];
					for (var j = 0; j < data.Length; j++) data[j] = i;

					stream.Write(data, 0, data.Length);
					stream.Position = 0;

					Assert.Equal(0, stream.Position);
					Assert.Equal(data.Length, stream.Length);

					var copy = new byte[size];
					stream.Read(copy, 0, copy.Length);
					Assert.Equal(data, copy);
				}

				i++;
			}
		}
Ejemplo n.º 12
0
        public void Normal()
        {
            var data = new byte[DataLength];

            for (int i = 0; i < LoopNum; i++)
            {
                using (var mstm = new PooledMemoryStream(ArrayPool <byte> .Shared, DataLength))
                {
                    #if NETCOREAPP3_0
                    mstm.Write(data.AsSpan());
                    #else
                    mstm.Write(data, 0, data.Length);
                    #endif
                }
            }
        }
        public void ExpandOnSingleSegment()
        {
            PooledMemoryStream stream = s_manager.GetStream();

            byte[] buffer = new byte[4];
            new Random(42).NextBytes(buffer);
            stream.Write(buffer);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(4, stream.Length);
            Assert.Equal(4, stream.Position);

            stream.SetLength(8);

            Assert.Equal(16, stream.Capacity);
            Assert.Equal(8, stream.Length);
            Assert.Equal(4, stream.Position);

            stream.Seek(0, SeekOrigin.Begin);

            byte[] buffer2 = new byte[8];
            Assert.Equal(8, stream.Read(buffer2));

            Assert.True(buffer2.AsSpan(0, 4).SequenceEqual(buffer.AsSpan(0, 4)));
            Assert.True(buffer2.AsSpan(4, 4).SequenceEqual(s_emptyBytes.AsSpan(0, 4)));
        }
        public static void PooledMemoryStream_Write_BeyondCapacity()
        {
            using (PooledMemoryStream memoryStream = new PooledMemoryStream())
            {
                long   origLength = memoryStream.Length;
                byte[] bytes      = new byte[10];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)i;
                }
                int spanPastEnd = 5;
                memoryStream.Seek(spanPastEnd, SeekOrigin.End);

                // Test Write
                memoryStream.Write(bytes, 0, bytes.Length);
                long pos = memoryStream.Position;
                Assert.Equal(pos, origLength + spanPastEnd + bytes.Length);
                Assert.Equal(memoryStream.Length, origLength + spanPastEnd + bytes.Length);

                // Verify bytes were correct.
                memoryStream.Position = origLength;
                byte[] newData = new byte[bytes.Length + spanPastEnd];
                int    n       = memoryStream.Read(newData, 0, newData.Length);
                Assert.Equal(n, newData.Length);
                for (int i = 0; i < spanPastEnd; i++)
                {
                    Assert.Equal(0, newData[i]);
                }
                for (int i = 0; i < bytes.Length; i++)
                {
                    Assert.Equal(bytes[i], newData[i + spanPastEnd]);
                }
            }
        }
Ejemplo n.º 15
0
        public void Data_Written_Can_Be_Read_Back()
        {
            var  sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
            var  pool  = new BufferManagerAllocator(1024, 1024 * 1024);
            byte i     = 1;

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var data = new byte[size];
                    for (var j = 0; j < data.Length; j++)
                    {
                        data[j] = i;
                    }

                    stream.Write(data, 0, data.Length);
                    stream.Position = 0;

                    Assert.Equal(0, stream.Position);
                    Assert.Equal(data.Length, stream.Length);

                    var copy = new byte[size];
                    stream.Read(copy, 0, copy.Length);
                    Assert.Equal(data, copy);
                }

                i++;
            }
        }
Ejemplo n.º 16
0
 public void PublishByCmd <T>(UInt16 cmd, T data, string exchange, string queue, bool persistent = false)
 {
     using (var ms = new PooledMemoryStream())
     {
         ms.Write(BitConverter.GetBytes(cmd), 0, 2);
         Serializer.Serialize(ms, data);
         Publish(ms.ToArray(), exchange, queue, persistent);
     }
 }
Ejemplo n.º 17
0
        internal void Receive(NetworkStream from, PooledMemoryStream to, int bytesToReceive)
        {
            var left = bytesToReceive;

            while (left != 0)
            {
                left -= to.Write(from, bytesToReceive);
            }
        }
Ejemplo n.º 18
0
 public void PooledMemoryStream()
 {
     using (var stream = new PooledMemoryStream())
     {
         for (int position = 0; position < this.Length; position += blockSize)
         {
             stream.Write(this.data, position, Math.Min(blockSize, this.Length - position));
         }
     }
 }
Ejemplo n.º 19
0
        public void TestShrink()
        {
            var data = new byte[] { 1, 2, 3, 4 };

            using (var stm = new PooledMemoryStream())
            {
                stm.Write(data, 0, data.Length);
                stm.Shrink(2);
                Assert.Equal(data.AsSpan(0, 2).ToArray(), stm.ToArray());
            }
        }
Ejemplo n.º 20
0
        public void Read_Must_Indicate_End_Of_Stream()
        {
            var pool   = new BufferManagerAllocator(1024, 1024 * 1024);
            var stream = new PooledMemoryStream(pool);
            var buffer = new byte[129];

            Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));

            stream.Write(buffer, 0, buffer.Length);
            Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));
        }
		public void Read_Must_Indicate_End_Of_Stream()
		{
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			var stream = new PooledMemoryStream(pool);
			var buffer = new byte[129];

			Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));

			stream.Write(buffer, 0, buffer.Length);
			Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));
		}
Ejemplo n.º 22
0
 public static Task PublishByCmd <T>(UInt16 cmd, T data, string exchange, string queue)
 {
     byte[] msg;
     using (var ms = new PooledMemoryStream())
     {
         ms.Write(BitConverter.GetBytes(cmd), 0, 2);
         Serializer.Serialize(ms, data);
         msg = ms.ToArray();
     }
     return(Publish(msg, exchange, queue, false));
 }
Ejemplo n.º 23
0
        public void PooledStreamBench()
        {
            var data = new byte[DataSize];

            for (int i = 0; i < MaxLoop; i++)
            {
                using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared, DataSize))
                {
                    stm.Write(data, 0, data.Length);
                }
            }
        }
Ejemplo n.º 24
0
        public void TestSetLength()
        {
            var data = new byte[] { 1, 2, 3, 4 };

            using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared))
            {
                stm.Write(data, 0, data.Length);
                stm.SetLength(128 * 1024);
                Assert.Equal(128 * 1024, stm.Length);
                var ar = stm.ToArray();
                Assert.Equal(data, ar.Take(data.Length));
            }
        }
Ejemplo n.º 25
0
        public void TestWriteTwice()
        {
            var data = new byte[] { 1, 2, 3, 4 };

            using (var stm = new PooledMemoryStream(ArrayPool <byte> .Shared))
            {
                Assert.True(stm.CanWrite);
                Assert.True(stm.CanRead);
                Assert.Equal(0, stm.Length);
                Assert.Equal(0, stm.Position);
                stm.Write(data, 0, data.Length);
                Assert.Equal(4, stm.Length);
                Assert.Equal(4, stm.Position);
                var ar = stm.ToArray();
                Assert.Equal(data, ar);
                stm.Write(data, 0, data.Length);
                Assert.Equal(8, stm.Length);
                Assert.Equal(8, stm.Position);
                ar = stm.ToArray();
                Assert.Equal(data.Concat(data), ar);
            }
        }
Ejemplo n.º 26
0
        public void TestWriteData()
        {
            using (var ms = new PooledMemoryStream())
            {
                var testData = new byte[4096];
                new Random().NextBytes(testData);

                ms.Write(testData, 0, testData.Length);

                var result = ms.ToUnsafeArraySegment();
                Assert.Equal(0, result.Offset);
                Assert.Equal(4096, result.Count);
                Assert.Equal(new ArraySegment <byte>(testData), result);
            }
        }
        public void ExpandUsingSmallSource()
        {
            var manager = new NonePooledMemoryStreamManager(new PooledMemoryStreamOptions()
            {
                MinimumSegmentSize = 16, MaximumSegmentSize = 16
            });

            PooledMemoryStream stream = manager.GetStream();

            stream.Write(new byte[10]);
            stream.SetLength(48);

            Assert.Equal(48, stream.Length);
            Assert.Equal(10, stream.Position);
            Assert.Equal(48, stream.Capacity);
        }
Ejemplo n.º 28
0
        private static PooledMemoryStream CreateExpandableMemoryStream(byte[] bytes)
        {
            PooledMemoryStream pooledMemoryStream;

            if (bytes != null)
            {
                pooledMemoryStream = new PooledMemoryStream(bytes.Length);
                pooledMemoryStream.Write(bytes, 0, bytes.Length);
                pooledMemoryStream.Seek(0L, SeekOrigin.Begin);
            }
            else
            {
                pooledMemoryStream = new PooledMemoryStream(16384);
            }
            return(pooledMemoryStream);
        }
Ejemplo n.º 29
0
        public void TestWriteDataToExistingBuffer()
        {
            var buffer = ArrayPool <byte> .Shared.Rent(4096);

            using (var ms = new PooledMemoryStream(buffer, 20, ArrayPool <byte> .Shared))
            {
                var testData = new byte[2048];
                new Random().NextBytes(testData);

                ms.Write(testData, 0, testData.Length);

                var result = ms.ToUnsafeArraySegment();
                Assert.Equal(20, result.Offset);
                Assert.Equal(2048, result.Count);
                Assert.Equal(new ArraySegment <byte>(testData), result);
            }
        }
		public void Write_Sets_Properties_Properly()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var data = new byte[size];

					stream.Write(data, 0, data.Length);

					Assert.Equal(data.Length, stream.Position);
					Assert.Equal(data.Length, stream.Length);
				}
			}
		}
Ejemplo n.º 31
0
        public void Write_Sets_Properties_Properly()
        {
            var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
            var pool  = new BufferManagerAllocator(1024, 1024 * 1024);

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var data = new byte[size];

                    stream.Write(data, 0, data.Length);

                    Assert.Equal(data.Length, stream.Position);
                    Assert.Equal(data.Length, stream.Length);
                }
            }
        }
        public static void MemoryStream_WriteToTests()
        {
            using (PooledMemoryStream ms2 = new PooledMemoryStream())
            {
                byte[] bytArrRet;
                byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 };

                // [] Write to FileStream, check the filestream
                ms2.Write(bytArr, 0, bytArr.Length);

                using (PooledMemoryStream readonlyStream = new PooledMemoryStream())
                {
                    ms2.WriteTo(readonlyStream);
                    readonlyStream.Flush();
                    readonlyStream.Position = 0;
                    bytArrRet = new byte[(int)readonlyStream.Length];
                    readonlyStream.Read(bytArrRet, 0, (int)readonlyStream.Length);
                    for (int i = 0; i < bytArr.Length; i++)
                    {
                        Assert.Equal(bytArr[i], bytArrRet[i]);
                    }
                }
            }

            // [] Write to memoryStream, check the memoryStream
            using (PooledMemoryStream ms2 = new PooledMemoryStream())
                using (PooledMemoryStream ms3 = new PooledMemoryStream())
                {
                    byte[] bytArrRet;
                    byte[] bytArr = new byte[] { byte.MinValue, byte.MaxValue, 1, 2, 3, 4, 5, 6, 128, 250 };

                    ms2.Write(bytArr, 0, bytArr.Length);
                    ms2.WriteTo(ms3);
                    ms3.Position = 0;
                    bytArrRet    = new byte[(int)ms3.Length];
                    ms3.Read(bytArrRet, 0, (int)ms3.Length);
                    for (int i = 0; i < bytArr.Length; i++)
                    {
                        Assert.Equal(bytArr[i], bytArrRet[i]);
                    }
                }
        }
Ejemplo n.º 33
0
        public void Should_be_able_to_read_from_stream()
        {
            using (var stream = new PooledMemoryStream())
            {
                var    memory = new MemoryStream();
                byte[] origin = WriteSomeBytes(memory, _dataSize);
                memory.Position = 0;

                stream.Write(memory, origin.Length);

                var actual = new byte[origin.Length];

                stream.Position = 0;
                int readBytes = stream.Read(actual, 0, actual.Length);


                readBytes.Should().Be(origin.Length);
                actual.Should().BeEquivalentTo(origin);
            }
        }
		public void Can_Rewrite_Stream()
		{
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			var stream = new PooledMemoryStream(pool);

			var chunk = new byte[60];
			for (var i = 0; i < 10; i++)
			{
				Fill(chunk, (byte)(i + 1));
				stream.Write(chunk, 0, chunk.Length);

				Assert.Equal((i + 1) * 60, stream.Position);
				Assert.Equal((i + 1) * 60, stream.Length);
			}

			stream.Position = 120;
			Fill(chunk, 200);
			stream.Write(chunk, 0, chunk.Length);

			stream.Position = 370;
			Fill(chunk, 100);
			stream.Write(chunk, 0, chunk.Length);

			Assert.Equal(430, stream.Position);
			Assert.Equal(600, stream.Length);

			var all = new byte[stream.Length];
			stream.Position = 0;
			stream.Read(all, 0, all.Length);

			Assert.Equal(600, stream.Position);
			Assert.Equal(600, stream.Length);

			var expected = new[]
			{
				Enumerable.Repeat(1, 60), // 0
				Enumerable.Repeat(2, 60), // 60
				Enumerable.Repeat(200, 60),	// 120
				Enumerable.Repeat(4, 60), // 180
				Enumerable.Repeat(5, 60), // 240
				Enumerable.Repeat(6, 60), // 300
				Enumerable.Repeat(7, 10), // 360
				Enumerable.Repeat(100, 60),	// 370
				Enumerable.Repeat(8, 50), // 430
				Enumerable.Repeat(9, 60), // 480
				Enumerable.Repeat(10, 60), // 540
			};

			var expectedValue = expected.SelectMany(_ => _.Select(v => (byte)v)).ToArray();

			Assert.Equal(expectedValue.Length, all.Length);
			Assert.Equal(stream.Length, all.Length);

			AssertArray(expectedValue, all);
		}
		public void Segmented_Data_Written_Can_Be_Read_Back_In_Segments()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			const int STEP_WRITE = 37;

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var remaining = size;
					var data = new byte[STEP_WRITE];
					byte value = 1;

					for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
					{
						Fill(data, value);
						stream.Write(data, 0, data.Length);
						value++;
						remaining -= STEP_WRITE;
					}

					if (remaining > 0)
					{
						Fill(data, value);
						stream.Write(data, 0, remaining);
					}

					Assert.Equal(size, stream.Position);
					Assert.Equal(size, stream.Length);

					stream.Position = 0;
					Assert.Equal(0, stream.Position);
					var copy = new byte[STEP_WRITE];

					value = 1;

					for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
					{
						Fill(data, value);
						Fill(copy, (byte)(value - 1));
						stream.Read(copy, 0, copy.Length);
						value++;
						remaining -= STEP_WRITE;

						Assert.Equal(data, copy);
					}

					if (remaining > 0)
					{
						Fill(data, value);
						stream.Write(data, 0, remaining);
						Assert.Equal(data.Take(remaining), copy.Take(remaining));
					}
				}
			}
		}