diff --git a/Common/Extensions/StreamExtensions.cs b/Common/Extensions/StreamExtensions.cs index b41cb5b3..8f686361 100644 --- a/Common/Extensions/StreamExtensions.cs +++ b/Common/Extensions/StreamExtensions.cs @@ -1055,13 +1055,13 @@ protected internal override void Dispose(bool disposing) public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stream, byte delimit = Common.ASCII.LineFeed, bool includeDelimit = false) { //Declare a value which will end up in a register on the stack - int register = -1, count = 0; + int register = -1; //Indicate when to terminate reading. bool terminate = false; //Use a MemoryStream as to not lock the reader - using (var buffer = new System.IO.MemoryStream(128)) + using (var buffer = new System.IO.MemoryStream()) { //While data can be read from the stream while (false == terminate) @@ -1077,15 +1077,13 @@ public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stre //Write the value read from the reader to the MemoryStream buffer.WriteByte((byte)register); - - //Store count - ++count; } + //If terminating then return the array contained in the MemoryStream. - var result = buffer.ToArray(); + buffer.TryGetBuffer(out var bufferSegment); //Return the bytes read from the stream - return new Common.MemorySegment(result, 0, count); + return new Common.MemorySegment(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count); } }