dd216589b6d38db1abd98e13e5e223a1c0c9e443
[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 import java.sql.*;
13
14 /** a source of streams */
15 public interface Fountain {
16
17     public Stream getStream();
18     public long   getLength();
19     public int    getNumLines();
20
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()); } 
27     }
28
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         protected ByteArray(String s) {
35             try {
36                 byte[] bytes = s.getBytes("UTF-8");
37                 this.bytes = bytes;
38                 this.off = 0;
39                 this.len = bytes.length;
40             } catch (UnsupportedEncodingException e) {
41                 throw new RuntimeException(e);
42             }
43         }
44         public ByteArray(byte[] bytes)                   { this(bytes, 0, bytes.length); }
45         public ByteArray(byte[] bytes, int off, int len) { this.bytes = bytes; this.off=off; this.len=len; }
46         public Stream getStream()                        { return new Stream(bytes, off, len); }
47         public long getLength()                          { return len; }
48         public int getNumLines()                         { return Stream.countLines(getStream()); } 
49     }
50
51
52     public static class StringFountain extends ByteArray {
53         public StringFountain(String s)                  { super(s); }
54     }
55
56     public static class Concatenate implements Fountain {
57         private Fountain[] founts;
58         public Concatenate(Fountain f1, Fountain f2)     { this(new Fountain[] { f1, f2 }); }
59         public Concatenate(Fountain[] f)                 { this.founts = f; }
60         public Stream getStream() {
61             Stream ret = null;
62             for(int i=founts.length-1; i>=0; i--)
63                 ret = ret==null ? founts[i].getStream() : founts[i].getStream().appendStream(ret);
64             return ret;
65         }
66         public long getLength() {
67             long ret = 0;
68             for(int i=0; i<founts.length; i++)
69                 ret += founts[i].getLength();
70             return ret;
71         }
72         public int getNumLines() {
73             int ret = 0;
74             for(int i=0; i<founts.length; i++)
75                 ret += founts[i].getNumLines();
76             return ret;
77         }
78     }
79
80     //public static class LazyCachingStreamFountain implements Fountain {
81     //}
82
83     /*
84     public static interface Transformer {
85         public Fountain transform(Fountain in);
86
87         public static class Lift implements Fountain.Transformer {
88             private final Stream.Transformer func;
89             public Lift(Stream.Transformer func) { this.func = func; }
90             public Fountain transform(final Fountain in) {
91                 return new Fountain() {
92                         public Stream getStream() {
93                             return func.transform(in.getStream()); } };
94             }
95         }
96     }
97     */
98
99     public static class Util {
100         public static Fountain create(String s) { return new StringFountain(s); }
101         public static Fountain concat(Fountain[] f) { return new Concatenate(f); }
102         public static Fountain concat(Fountain f1, Fountain f2) {
103             return new Concatenate(f1, f2);
104         }
105         public static Fountain concat(Fountain f1, Fountain f2, Fountain f3) {
106             return new Concatenate(f1, new Concatenate(f2, f3));
107         }
108     }
109 }