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

ReadFiltered() protected method

Fills the readBuffers with more data from the underlying stream passed through all the stream filters.
protected ReadFiltered ( int chunkSize ) : TextElement
chunkSize int Maximum number of bytes to be read from the stream.
return TextElement
        protected TextElement ReadFiltered(int chunkSize)
        {
            byte[] chunk = new byte[chunkSize];
            var filtered = TextElement.Null;

            while (filtered.IsNull)
            {
                // Read data until there is an output or error or EOF.
                if (RawEof) return TextElement.Null;
                int read = RawRead(chunk, 0, chunkSize);
                if (read <= 0)
                {
                    // Error or EOF.
                    return TextElement.Null;
                }

                if (read < chunkSize)
                {
                    byte[] sub = new byte[read];
                    Array.Copy(chunk, 0, sub, 0, read);
                    chunk = sub;
                }
                filtered = new TextElement(chunk);

                bool closing = RawEof;

                if (textReadFilter != null)
                {
                    // First use the text-input filter if any.
                    filtered = textReadFilter.Filter(_ctx, filtered, closing);
                }

                if (readFilters != null)
                {
                    // After that apply the user-filters.
                    foreach (IFilter f in readFilters)
                    {
                        if (filtered.IsNull)
                        {
                            // This is the last chance to output something. Give chance to all filters.
                            if (closing) filtered = TextElement.Empty;
                            else break; // Continue with next RawRead()
                        }
                        filtered = f.Filter(_ctx, filtered, closing);
                    } // foreach
                } // if
            } // while 

            return filtered;
        }