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); }
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 WriteDefault() { //**************************************** byte[] MyResult; byte[] TempOutput; //**************************************** using (var RawStream = new MemoryStream()) { using (var Writer = new StreamWriter(RawStream)) { Writer.Write(_TestInput); } TempOutput = RawStream.ToArray(); } //**************************************** using (var Buffer = new BufferWriter <byte>()) using (var Writer = new BinaryTextWriter(Buffer)) { Writer.Write(_TestInput); Writer.Flush(); MyResult = Buffer.ToSequence().ToArray(); } //**************************************** CollectionAssert.AreEqual(TempOutput, MyResult); }
public void WriteUnicode([Values(false, true)] bool bigEndian) { //**************************************** byte[] MyResult; byte[] TempOutput; var MyEncoding = new UnicodeEncoding(bigEndian, true); //**************************************** using (var RawStream = new MemoryStream()) { using (var Writer = new StreamWriter(RawStream, MyEncoding)) { Writer.Write(_TestInput); } TempOutput = RawStream.ToArray(); } //**************************************** using (var Buffer = new BufferWriter <byte>()) using (var Writer = new BinaryTextWriter(Buffer, MyEncoding)) { Writer.Write(_TestInput); Writer.Flush(); MyResult = Buffer.ToSequence().ToArray(); } //**************************************** CollectionAssert.AreEqual(TempOutput, MyResult); }
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); }
public void Write([Values(8, 16, 32, 64, 128)] int capacity) { //**************************************** byte[] MyResult; //**************************************** using (var Buffer = new BufferWriter <byte>()) using (var Writer = new BinaryTextWriter(Buffer, Encoding.UTF8, capacity)) { Writer.Write(_TestInput); Writer.Flush(); MyResult = Buffer.ToSequence().ToArray(); } //**************************************** CollectionAssert.AreEqual(_TestOutput, MyResult); }
public void WriteUTF8() { //**************************************** byte[] MyResult; //**************************************** using (var Buffer = new BufferWriter <byte>()) using (var Writer = new BinaryTextWriter(Buffer, Encoding.UTF8)) { Writer.Write(_TestInput); Writer.Flush(); MyResult = Buffer.ToSequence().ToArray(); } //**************************************** CollectionAssert.AreEqual(_TestOutput, MyResult); }
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); } }