MemoryStream that uses memory from the MemoryPool
Inheritance: Stream
Esempio n. 1
0
        public void PoolMemoryStreamReadWriteBytes()
        {
            //assume
            const int count = 20000;
            var bytes = new byte[count];
            for(int i = 0; i < count; i++)
            {
                bytes[i] = (byte)((i - byte.MinValue)%byte.MaxValue);
            }

            //act
            var stream = new PoolMemoryStream();
            stream.Write(bytes, 0, count);
            stream.Position = 0;

            var readBytes = new byte[count];
            int actual = 0;
            int read = 0;
            do
            {
                read = stream.Read(readBytes, read, count - read);
                actual += read;
            } while(read > 0);

            //assert
            Assert.AreEqual(stream.Length, actual, "not all written bytes can be read");
            Assert.IsTrue(readBytes.SequenceEqual(bytes), "returned bytes differ from written bytes");


            stream.Dispose();
        }
Esempio n. 2
0
        public void PoolMemoryStreamWriteBytes()
        {
            //assume
            const int count = 20000;
            var bytes = new byte[count];
            for(int i = 0; i < count; i++)
            {
                bytes[i] = (byte)((i - byte.MinValue)%byte.MaxValue);
            }

            //act
            var stream = new PoolMemoryStream();
            stream.Write(bytes, 0, count);

            //assert
            Assert.AreEqual(stream.Length, count, "Unexpected stream length");
            Assert.AreEqual(stream.Position, count, "Unexpected position");
            Assert.IsTrue(stream.Capacity >= count, "Unexpected capacity");
        }
Esempio n. 3
0
        public void PoolMemoryStreamReadWriteSingleBytes()
        {
            //assume
            const int count = 20000;
            var bytes = new byte[count];
            for (int i = 0; i < count; i++)
                bytes[i] = (byte) ((i - byte.MinValue)%byte.MaxValue);

            //act
            var stream = new PoolMemoryStream();
            for (int i = 0; i < count; i++)
                stream.WriteByte(bytes[i]);

            stream.Position = 0;

            Assert.AreEqual(stream.Length, count, "Unexpected stream length!");
            for (int i = 0; i < count; i++)
                Assert.AreEqual((byte) stream.ReadByte(), bytes[i], "Read byte is not equal to written bytes!");
        }
Esempio n. 4
0
        public void PoolMemoryStreamWriteCopyToAsync()
        {
            //assume
            const int count = 20000;
            var bytes = new byte[count];
            for(int i = 0; i < count; i++)
            {
                bytes[i] = (byte)((i - byte.MinValue)%byte.MaxValue);
            }

            //act
            var stream = new PoolMemoryStream();
            stream.Write(bytes, 0, count);
            stream.Position = 0;

            var target = new MemoryStream();
            stream.CopyToAsync(target).Wait();

            var readBytes = target.ToArray();

            //assert
            Assert.AreEqual(stream.Length, readBytes.Length, "not all written bytes are copied");
            Assert.IsTrue(readBytes.SequenceEqual(bytes), "returned bytes differ from written bytes");
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the frame bytes.
        /// </summary>
        /// <returns></returns>
        public Stream GetFrameBytes(bool compress, int compressTreshold)
        {
            var buffer = new PoolMemoryStream();
            buffer.WriteByte((byte)Version);
            buffer.WriteByte((byte)Flags);
            buffer.WriteByte(unchecked((byte)Stream));
            buffer.WriteByte((byte)OpCode);

            //write length placeholder
            buffer.WriteInt(0);

            //write uncompressed data
            WriteData(buffer);

            //compress if allowed, and buffer is large enough to compress
            if (compress && buffer.Length > compressTreshold + 8)
            {
                buffer.Position = 8;

                //compress data to temporary stream
                using (var compressed = new PoolMemoryStream())
                {
                    //compress the data to the buffer
                    int length = Compressor.Compress(buffer, compressed);

                    //add compression to flags
                    Flags |= FrameFlags.Compression;
                    buffer.Position = 1;
                    buffer.WriteByte((byte)Flags);

                    //overwrite data with compressed data
                    buffer.Position = 8;
                    compressed.Position = 0;
                    compressed.CopyTo(buffer);
                    buffer.SetLength(length + 8);
                }
            }

            //overwrite length with real value
            buffer.Position = 4;
            buffer.WriteInt((int)buffer.Length - 8);

            //reset buffer position
            buffer.Position = 0;

            //return the buffer
            return buffer;
        }
Esempio n. 6
0
        /// <summary>
        /// Gets the frame bytes.
        /// </summary>
        /// <returns> </returns>
        public PoolMemoryStream GetFrameBytes(bool compress, int compressTreshold)
        {
            var buffer = new PoolMemoryStream();

            int versionByte = (ProtocolVersion & 0x7f) | (IsRequest ? 0 : 0x80);
            buffer.WriteByte((byte)versionByte);
            buffer.WriteByte((byte)Flags);
            if(ProtocolVersion <= 2)
                buffer.WriteByte(unchecked((byte)Stream));
            else
                buffer.WriteShort(unchecked((ushort)Stream));
            buffer.WriteByte((byte)OpCode);

            //write length placeholder
            buffer.WriteInt(0);

            long headerLength = buffer.Position;

            //write uncompressed data
            WriteData(buffer);

            //compress if allowed, and buffer is large enough to compress
            if(compress && buffer.Length > headerLength + compressTreshold)
            {
                //rewind to start of content
                buffer.Position = headerLength;

                //compress data to temporary stream
                using(var compressed = new PoolMemoryStream())
                {
                    //compress the data to the buffer
                    int length = Compressor.Compress(buffer, compressed);

                    //add compression to flags
                    Flags |= FrameFlags.Compression;
                    buffer.Position = 1;
                    buffer.WriteByte((byte)Flags);

                    //overwrite data with compressed data
                    buffer.Position = headerLength;
                    compressed.Position = 0;
                    compressed.CopyTo(buffer);
                    buffer.SetLength(headerLength + length);
                }
            }

            //overwrite length with real value
            buffer.Position = headerLength - 4;
            buffer.WriteInt((int)(buffer.Length - headerLength));

            //reset buffer position
            buffer.Position = 0;

            //return the buffer
            return buffer;
        }