From eb7092627133a80fefcd8c8082372c9489c15ad3 Mon Sep 17 00:00:00 2001 From: Mark Junker Date: Fri, 10 Jul 2015 12:50:35 +0200 Subject: [PATCH] Fixed the MarkableStreamReader to copy previously buffered data into the new buffer when there are still unread characters in the old buffer. --- BeanIO.Test/Stream/FixedLengthReaderTest.cs | 2 +- BeanIO/Internal/Util/MarkableTextReader.cs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/BeanIO.Test/Stream/FixedLengthReaderTest.cs b/BeanIO.Test/Stream/FixedLengthReaderTest.cs index 3ce8257..ff467c1 100644 --- a/BeanIO.Test/Stream/FixedLengthReaderTest.cs +++ b/BeanIO.Test/Stream/FixedLengthReaderTest.cs @@ -124,7 +124,7 @@ public void TestComments() "/+" + "+" + "// ignored+" + - "//++"); + "//"); var reader = new FixedLengthReader(input, config); Assert.Equal("one", reader.Read()); diff --git a/BeanIO/Internal/Util/MarkableTextReader.cs b/BeanIO/Internal/Util/MarkableTextReader.cs index 634a50e..93d3e8a 100644 --- a/BeanIO/Internal/Util/MarkableTextReader.cs +++ b/BeanIO/Internal/Util/MarkableTextReader.cs @@ -62,8 +62,20 @@ public override int Read() public virtual void Mark(int readAheadLimit) { - _markBuffer = new int[readAheadLimit]; - _markBufferSize = _markBufferPosition = 0; + var oldBuffer = _markBuffer; + if (oldBuffer != null && _markBufferPosition != _markBufferSize) + { + var remainingSize = _markBufferSize - _markBufferPosition; + _markBuffer = new int[Math.Max(readAheadLimit, remainingSize)]; + Array.Copy(oldBuffer, _markBufferPosition, _markBuffer, 0, remainingSize); + _markBufferSize = remainingSize; + } + else + { + _markBuffer = new int[readAheadLimit]; + _markBufferSize = 0; + } + _markBufferPosition = 0; } public virtual void Reset()