1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
10 import java.util.zip.*;
11 import org.ibex.util.*;
14 /** a source of streams */
15 public interface Fountain {
17 public Stream getStream();
18 public long getLength();
19 public int getNumLines();
21 public static class File implements Fountain {
22 private final java.io.File file;
23 public File(java.io.File file) { this.file = file; }
24 public Stream getStream() { return new Stream(file); }
25 public long getLength() { return (int)file.length(); }
26 public int getNumLines() { return Stream.countLines(getStream()); }
29 // sketchy since the bytes can be modified
30 public static class ByteArray implements Fountain {
31 private final byte[] bytes;
32 private final int off;
33 private final int len;
34 public ByteArray(byte[] bytes) { this(bytes, 0, bytes.length); }
35 public ByteArray(byte[] bytes, int off, int len) { this.bytes = bytes; this.off=off; this.len=len; }
36 public Stream getStream() { return new Stream(bytes, off, len); }
37 public long getLength() { return len; }
38 public int getNumLines() { return Stream.countLines(getStream()); }
41 public static class StringFountain implements Fountain {
43 public StringFountain(String s) { this.s = s; }
44 public Stream getStream() { return new Stream(s); }
45 public int getLength() { return s.length(); } // FIXME ENCODING ISSUES!!!!!
46 public int getNumLines() { return Stream.countLines(getStream()); }
49 public static class Concatenate implements Fountain {
51 public Concatenate(Fountain f1, Fountain f2) { this.f1 = f1; this.f2 = f2; }
52 public Stream getStream() { return f1.getStream().appendStream(f2.getStream()); }
53 public int getLength() { return f1.getLength()+f2.getLength(); }
54 public int getNumLines() { return f1.getNumLines()+f2.getNumLines(); }
57 //public static class LazyCachingStreamFountain implements Fountain {