How to string multiple TextReaders together?

谁说胖子不能爱 提交于 2019-11-29 14:08:38

I just threw this together, so it's not super-robust (no error handling, etc) but the basic test case works.

It works by creating an extension method for TextReader's which take a second, and returns a new TextReader class which internally calls Read() on the first until it runs out, and then starts calling Read()on the second. You can chain this indefinitely.

To provide a complete implementation of TextReader you only need to implement Read(), Peek(), Close() and Dispose(). All the other methods rely on specific implementation Read() to work. So creating your own TextReader really isn't so bad, as you can see below.

This also alleviates any performance concerns since we are simply wrapping the existing TextReaders and not actually invoking them to perform the concatenation.

class Program
{
    static void Main(string[] args)
    {
        StringReader first = new StringReader("hello ");
        StringReader second = new StringReader("world");
        StringReader third = new StringReader("!");

        using (var allOfThem = first.Concat(second).Concat(third))
        {
            //writes "hello world!"
            Console.WriteLine(allOfThem.ReadToEnd());
        }
        Console.Read();
    }
}

public static class Extensions
{
    public static TextReader Concat(this TextReader first, TextReader second)
    {
        return new ChainedTextReader(first, second);
    }

    private class ChainedTextReader : TextReader
    {
        private TextReader first;
        private TextReader second;
        private bool readFirst = true;

        public ChainedTextReader(TextReader first, TextReader second)
        {
            this.first = first;
            this.second = second;
        }

        public override int Peek()
        {
            if (readFirst)
            {
                return first.Peek();
            }
            else
            {
                return second.Peek();
            }
        }

        public override int Read()
        {
            if (readFirst)
            {
                int value = first.Read();
                if (value == -1)
                {
                    readFirst = false;
                }
                else
                {
                    return value;
                }
            }
            return second.Read();
        }

        public override void Close()
        {
            first.Close();
            second.Close();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                first.Dispose();
                second.Dispose();
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!