package org.cobbzilla.util.io;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class MultiReader extends Reader {

    private final List<Reader> readers = new ArrayList<>();
    private Reader currentReader;
    private int readerIndex = 0;
    private boolean endOfReaders = false;

    public MultiReader (Reader r) {
        addReader(r);
        currentReader = r;
    }

    public void addReader (Reader r) { readers.add(r); }

    public void addLastReader (Reader r) {
        addReader(r);
        endOfReaders = true;
    }

    @Override public int read(char[] buf, int off, int len) throws IOException {
        int count = currentReader.read(buf, off, len);
        if (count == -1) {
            if (readerIndex == readers.size()-1) {
                return endOfReaders ? -1 : 0;
            }
            currentReader.close();
            readerIndex++;
            currentReader = readers.get(readerIndex);
            return read(buf, off, len);
        }
        return count;
    }

    @Override public void close() throws IOException {
        if (currentReader != null) currentReader.close();
    }
}

@@ -53,16 +53,78 @@
 eption;%0A
+import java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 import j
@@ -130,32 +130,32 @@
 java.io.Reader;%0A
-
 import java.util
@@ -491,24 +491,114 @@
 = r;%0A    %7D%0A%0A
+    public MultiReader (InputStream in) %7B%0A        this(new InputStreamReader(in));%0A    %7D%0A%0A
     public v
@@ -737,23 +737,203 @@
 = true;%0A
-
     %7D%0A%0A
+    public void addStream (InputStream in) %7B addReader(new InputStreamReader(in)); %7D%0A%0A    public void addLastStream (InputStream in) %7B addLastReader(new InputStreamReader(in)); %7D%0A%0A
     @Ove

add stream methods