From 0b2973d441cfd3264cf62582c30f65ad0397f863 Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Sun, 12 Nov 2023 00:19:42 -0500 Subject: [PATCH] StreamExtensions --- Common/Extensions/StreamExtensions.cs | 50 ++++++++++++--------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/Common/Extensions/StreamExtensions.cs b/Common/Extensions/StreamExtensions.cs index c1a974b0..8f686361 100644 --- a/Common/Extensions/StreamExtensions.cs +++ b/Common/Extensions/StreamExtensions.cs @@ -1054,43 +1054,37 @@ protected internal override void Dispose(bool disposing) /// The bytes read from the reader public static Common.MemorySegment ReadDelimitedValue(this System.IO.Stream stream, byte delimit = Common.ASCII.LineFeed, bool includeDelimit = false) { - try - { - //Declare a value which will end up in a register on the stack - int register = -1, count = 0; + //Declare a value which will end up in a register on the stack + int register = -1; - //Indicate when to terminate reading. - bool terminate = false; + //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)) + //Use a MemoryStream as to not lock the reader + using (var buffer = new System.IO.MemoryStream()) + { + //While data can be read from the stream + while (false == terminate) { - //While data can be read from the stream - while (false == terminate) - { - //Read a byte from the stream - register = stream.ReadByte(); + //Read a byte from the stream + register = stream.ReadByte(); - //Check for termination - terminate = register == -1 || register == delimit; + //Check for termination + terminate = register == -1 || register == delimit; - //If the byte read is equal to the delimit and the delimit byte is not included then return the array contained in the MemoryStream. - if (terminate && false == includeDelimit) break; + //If the byte read is equal to the delimit and the delimit byte is not included then return the array contained in the MemoryStream. + if (terminate && false == includeDelimit) break; - //Write the value read from the reader to the MemoryStream - buffer.WriteByte((byte)register); + //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(); + //If terminating then return the array contained in the MemoryStream. + buffer.TryGetBuffer(out var bufferSegment); - //Return the bytes read from the stream - return new Common.MemorySegment(result, 0, count); - } + //Return the bytes read from the stream + return new Common.MemorySegment(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count); } - catch { throw; } } ///