added Duct.java, Fountain.java
[org.ibex.io.git] / src / org / ibex / io / Fountain.java
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.
4
5 package org.ibex.io;
6
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import java.util.zip.*;
11 import org.ibex.util.*;
12
13 /** a source of streams */
14 public interface Fountain {
15
16     public Stream getStream();
17     public int getLength();
18     public int getNumLines();
19     /*
20     public static interface Transformer {
21         public Fountain transform(Fountain in);
22
23         public static class Lift implements Fountain.Transformer {
24             private final Stream.Transformer func;
25             public Lift(Stream.Transformer func) { this.func = func; }
26             public Fountain transform(final Fountain in) {
27                 return new Fountain() {
28                         public Stream getStream() {
29                             return func.transform(in.getStream()); } };
30             }
31         }
32     }
33     */
34
35     public static class File implements Fountain {
36         private final java.io.File file;
37         public File(java.io.File file) { this.file = file; }
38         public Stream getStream()      { return new Stream(file); }
39         public int getLength()         { return (int)file.length(); }
40         public int getNumLines()       { return Stream.countLines(getStream()); } 
41     }
42
43     // sketchy since the bytes can be modified
44     public static class ByteArray implements Fountain {
45         private final byte[] bytes;
46         private final int off;
47         private final int len;
48         public ByteArray(byte[] bytes)                   { this(bytes, 0, bytes.length); }
49         public ByteArray(byte[] bytes, int off, int len) { this.bytes = bytes; this.off=off; this.len=len; }
50         public Stream getStream()                        { return new Stream(bytes, off, len); }
51         public int getLength()                           { return len; }
52         public int getNumLines()                         { return Stream.countLines(getStream()); } 
53     }
54
55     public static class StringFountain implements Fountain {
56         String s;
57         public StringFountain(String s)                  { this.s = s; }
58         public Stream getStream()                        { return new Stream(s); }
59         public int getLength()                           { return s.length(); }   // FIXME ENCODING ISSUES!!!!!
60         public int getNumLines()                         { return Stream.countLines(getStream()); } 
61     }
62
63     public static class Concatenate implements Fountain {
64         Fountain f1, f2;
65         public Concatenate(Fountain f1, Fountain f2)     { this.f1 = f1; this.f2 = f2; }
66         public Stream getStream()                        { return f1.getStream().appendStream(f2.getStream()); }
67         public int getLength()                           { return f1.getLength()+f2.getLength(); }
68         public int getNumLines()                         { return f1.getNumLines()+f2.getNumLines(); }
69     }
70
71     //public static class LazyCachingStreamFountain implements Fountain {
72     //}
73
74 }