From 0236c4835b9add599a4f71b50d4b798050db2876 Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Tue, 20 Mar 2018 09:40:56 -0700 Subject: [PATCH] Bug fix in BufferWriter (#2170) --- .../System/Buffers/Writer/BufferWriter_writable.cs | 5 +++-- .../BasicUnitTests.cs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs index 77c1d501a43..da53dc3b9d7 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs @@ -39,12 +39,13 @@ public void WriteBytes(ReadOnlyMemory bytes) public bool TryWriteBytes(ReadOnlyMemory bytes, TransformationFormat format) { - if (!TryWriteBytes(bytes.Span)) + var span = bytes.Span; + if (!span.TryCopyTo(Free)) { return false; } + int written = span.Length; - int written = bytes.Length; if (format.TryTransform(Free, ref written)) { _written += written; diff --git a/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs b/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs index ca9b47aa3c4..580e5192265 100644 --- a/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs +++ b/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Buffers.Operations; using System.Buffers.Text; using System.Buffers.Writer; using System.Diagnostics; @@ -59,6 +60,17 @@ public void WritePlainText() var result = _sink.ToString(); Assert.Equal(s_response, _sink.ToString()); } + + [Fact] + public void BufferWriterTransform() + { + byte[] buffer = new byte[10]; + var writer = BufferWriter.Create(buffer.AsSpan()); + var transformation = new TransformationFormat(new RemoveTransformation(2)); + ReadOnlyMemory value = new byte[] { 1, 2, 3 }; + writer.WriteBytes(value, transformation); + Assert.Equal(-1, buffer.AsSpan().IndexOf((byte)2)); + } } class Sink : IBufferWriter