コード例 #1
0
        public void WritesInChunks()
        {
            int bufferSize = 500;
            int size = 3000;

            var buffers = new List<ArraySegment<byte>>();
            var writer = new BinaryTextWriter((buffer, state) =>
            {
                buffers.Add(buffer);
            },
            null, reuseBuffers: true, bufferSize: bufferSize);

            writer.Write(new string('C', size));
            writer.Flush();

            var expected = GetChunks(size, bufferSize).ToArray();

            Assert.NotEmpty(buffers);
            Assert.Equal(expected.Length, buffers.Count);

            for (int i = 0; i < buffers.Count; i++)
            {
                Assert.Equal(expected[i], buffers[i].Count);
            }
        }
コード例 #2
0
        public void WriteSameBufferIsUsedForWritingChunksIfReuseBuffersTrue()
        {
            var buffers = new List<ArraySegment<byte>>();
            var writer = new BinaryTextWriter((buffer, state) =>
            {
                buffers.Add(buffer);
            },
            null, reuseBuffers: true, bufferSize: 128);

            writer.Write(new string('C', 10000));
            writer.Flush();

            Assert.True(buffers.Count > 1);
            var underlyingBuffer = buffers[0].Array;
            for (int i = 1; i < buffers.Count; i++)
            {
                Assert.Same(underlyingBuffer, buffers[i].Array);
            }
        }
コード例 #3
0
        public void CanEncodingSurrogatePairsCorrectly()
        {
            var bytes = new List<byte>();
            var writer = new BinaryTextWriter((buffer, state) =>
            {
                for (int i = buffer.Offset; i < buffer.Count; i++)
                {
                    bytes.Add(buffer.Array[i]);
                }
            }, null, reuseBuffers: true, bufferSize: 128);

            writer.Write("\U00024B62"[0]);
            writer.Write("\U00024B62"[1]);
            writer.Flush();

            var expected = new byte[] { 0xF0, 0xA4, 0xAD, 0xA2 };

            Assert.Equal(expected, bytes);
        }
コード例 #4
0
ファイル: Connection.cs プロジェクト: xushuai02555/WebPrinter
        private ArraySegment <byte> GetMessageBuffer(object value)
        {
            using (var stream = new MemoryStream(128))
            {
                var bufferWriter = new BinaryTextWriter((buffer, state) =>
                {
                    ((MemoryStream)state).Write(buffer.Array, buffer.Offset, buffer.Count);
                },
                                                        stream,
                                                        reuseBuffers: true,
                                                        bufferSize: 1024);

                using (bufferWriter)
                {
                    _serializer.Serialize(value, bufferWriter);
                    bufferWriter.Flush();

                    return(new ArraySegment <byte>(stream.ToArray()));
                }
            }
        }
コード例 #5
0
ファイル: Connection.cs プロジェクト: Djohnnie/Sonarr
        private ArraySegment<byte> GetMessageBuffer(object value)
        {
            using (var stream = new MemoryStream(128))
            {
                var bufferWriter = new BinaryTextWriter((buffer, state) =>
                {
                    ((MemoryStream)state).Write(buffer.Array, buffer.Offset, buffer.Count);
                },
                stream,
                reuseBuffers: true,
                bufferSize: 1024);

                using (bufferWriter)
                {
                    _serializer.Serialize(value, bufferWriter);
                    bufferWriter.Flush();

                    return new ArraySegment<byte>(stream.ToArray());
                }
            }
        }
コード例 #6
0
        public void CanInterleaveStringsAndRawBinary()
        {
            var buffers = new List<ArraySegment<byte>>();
            var writer = new BinaryTextWriter((buffer, state) =>
            {
                buffers.Add(buffer);
            },
            null, reuseBuffers: true, bufferSize: 128);

            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(2, buffers.Count);
            var s = "";
            foreach (var buffer in buffers)
            {
                s += encoding.GetString(buffer.Array, buffer.Offset, buffer.Count);
            }
            Assert.Equal("Hello World", s);
        }