X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fio%2FFountain.java;h=d4c1a0d8f67f34deb5b961b91b61b2a043244fd0;hp=8e2561c7938bc4b2c497cb48eaf0234b1796a939;hb=f658e02ec5209c9669351800503f448d38c16bbb;hpb=0e435c7d172d35149811787061b13e89fb912922 diff --git a/src/org/ibex/io/Fountain.java b/src/org/ibex/io/Fountain.java index 8e2561c..d4c1a0d 100644 --- a/src/org/ibex/io/Fountain.java +++ b/src/org/ibex/io/Fountain.java @@ -53,15 +53,138 @@ public interface Fountain { public StringFountain(String s) { super(s); } } + public static class SubFountain implements Fountain { + private final Fountain f; + private final int start; + private final int len; + public SubFountain(Fountain f, int start) { this(f, start, -1); } + public SubFountain(Fountain f, int start, int len) { + this.f = f; + this.start = start; + this.len = len; + } + public Stream getStream() { + Stream s = f.getStream(); + // FIXME: this is really fragile and IMAP needs it + int remain = start; + while(remain > 0) { + long result = s.skip(start); + Log.error("skip", result + " / " + start); + if (result == -1) return s; + remain -= result; + } + if (len != -1) s.setLimit(len); + return s; + } + public long getLength() { + if (len == -1) return f.getLength()-start; + return Math.min(f.getLength()-start, len); + } + public int getNumLines() { return Stream.countLines(getStream()); } + } + public static class Concatenate implements Fountain { - Fountain f1, f2; - public Concatenate(Fountain f1, Fountain f2) { this.f1 = f1; this.f2 = f2; } - public Stream getStream() { return f1.getStream().appendStream(f2.getStream()); } - public int getLength() { return f1.getLength()+f2.getLength(); } - public int getNumLines() { return f1.getNumLines()+f2.getNumLines(); } + private Fountain[] founts; + public Concatenate(Fountain f1, Fountain f2) { this(new Fountain[] { f1, f2 }); } + public Concatenate(Fountain[] f) { this.founts = f; } + public Stream getStream() { + Stream ret = null; + for(int i=founts.length-1; i>=0; i--) + ret = ret==null ? founts[i].getStream() : founts[i].getStream().appendStream(ret); + return ret; + } + public long getLength() { + long ret = 0; + for(int i=0; i