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;
}