Inheritance: System.IO.TextWriter
        public void StringOverrideBehavesAsCharArray()
        {
            var writer = new MemoryPoolTextWriter(new MemoryPool());
            var testTxt = new string('m', 260);

            writer.Write(testTxt.ToCharArray(), 0, testTxt.Length);
            writer.Flush();

            var encoding = new UTF8Encoding();
            Assert.Equal(testTxt, encoding.GetString(writer.Buffer.ToArray()));
        }
        public void CanEncodingSurrogatePairsCorrectly()
        {
            using(var writer = new MemoryPoolTextWriter(new MemoryPool()))
            {
                writer.Write("\U00024B62"[0]);
                writer.Write("\U00024B62"[1]);
                writer.Flush();

                var expected = new byte[] { 0xF0, 0xA4, 0xAD, 0xA2 };
                Assert.Equal(expected, writer.Buffer);
            }
        }
        public void CanInterleaveStringsAndRawBinary()
        {
            var buffers = new List<ArraySegment<byte>>();
            using (var writer = new MemoryPoolTextWriter(new MemoryPool()))
            {
                var encoding = new UTF8Encoding();
                writer.Write('H');
                writer.Write('e');
                writer.Write("llo ");
                writer.Write(new ArraySegment<byte>(encoding.GetBytes("World")));
                writer.Flush();

                Assert.Equal("Hello World", encoding.GetString(writer.Buffer.ToArray()));
            }
        }
        private ArraySegment <byte> SerializeMessageValue(object value)
        {
            using (var writer = new MemoryPoolTextWriter(_pool))
            {
                _serializer.Serialize(value, writer);
                writer.Flush();

                var data = writer.Buffer;

                var buffer = new byte[data.Count];

                Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count);

                return(new ArraySegment <byte>(buffer));
            }
        }
Esempio n. 5
0
        private ArraySegment<byte> SerializeMessageValue(object value)
        {
            using (var writer = new MemoryPoolTextWriter(_pool))
            {
                _serializer.Serialize(value, writer);
                writer.Flush();

                var data = writer.Buffer;

                var buffer = new byte[data.Count];

                Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count);

                return new ArraySegment<byte>(buffer);
            }
        }