Pchp.Library.Streams.PhpStream.ReadLine C# (CSharp) Method

ReadLine() public method

Reads one line (text ending with the ending delimiter) from the stream up to length characters long.
public ReadLine ( int length, string ending ) : string
length int Maximum length of the returned or -1 for unlimited reslut.
ending string Delimiter of the returned line or null to use the system default.
return string
        public string ReadLine(int length, string ending)
        {
            // A length has to be specified if we want to use the delimiter.
            Debug.Assert((length > 0) || (ending == null));

            var str = ReadData(length, ending == null) // null ending => use \n
                .AsText(_ctx.StringEncoding);

            if (ending != null)
            {
                int pos = (ending.Length == 1) ? str.IndexOf(ending[0]) : str.IndexOf(ending);
                if (pos >= 0)
                {
                    string left, right;
                    SplitData(str, pos + ending.Length - 1, out left, out right);
                    Debug.Assert(right != null);
                    int returnedLength = right.Length;
                    TextElement rightElement = (this.IsBinary)
                        ? new TextElement(_ctx.StringEncoding.GetBytes(right))
                        : new TextElement(right);
                    
                    if (readBuffers.Count != 0)
                    {
                        // EX: Damn. Have to put the data to the front of the queue :((
                        // Better first look into the buffers for the ending..
                        var newBuffers = new Queue<TextElement>(readBuffers.Count + 2);
                        newBuffers.Enqueue(rightElement);
                        foreach (var o in readBuffers)
                        {
                            newBuffers.Enqueue(o);
                        }
                        readBuffers = newBuffers;
                    }
                    else
                    {
                        readBuffers.Enqueue(rightElement);
                    }
                    // Update the offset as the data gets back.
                    readOffset -= returnedLength;
                    return left;
                }
            }
            // ReadLine now works on binary files too but only for the \n ending.
            return str;
        }