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); } }
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); } }
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); }
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())); } } }
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()); } } }
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); }