added Duct.java, Fountain.java
authoradam <adam@megacz.com>
Sun, 27 Feb 2005 20:34:28 +0000 (20:34 +0000)
committeradam <adam@megacz.com>
Sun, 27 Feb 2005 20:34:28 +0000 (20:34 +0000)
darcs-hash:20050227203428-5007d-91d016e26a879fcf01a1202b425c40646cb8e248.gz

src/org/ibex/io/Duct.java [new file with mode: 0644]
src/org/ibex/io/Fountain.java [new file with mode: 0644]

diff --git a/src/org/ibex/io/Duct.java b/src/org/ibex/io/Duct.java
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/org/ibex/io/Fountain.java b/src/org/ibex/io/Fountain.java
new file mode 100644 (file)
index 0000000..74fb58e
--- /dev/null
@@ -0,0 +1,74 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.io;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.zip.*;
+import org.ibex.util.*;
+
+/** a source of streams */
+public interface Fountain {
+
+    public Stream getStream();
+    public int getLength();
+    public int getNumLines();
+    /*
+    public static interface Transformer {
+        public Fountain transform(Fountain in);
+
+        public static class Lift implements Fountain.Transformer {
+            private final Stream.Transformer func;
+            public Lift(Stream.Transformer func) { this.func = func; }
+            public Fountain transform(final Fountain in) {
+                return new Fountain() {
+                        public Stream getStream() {
+                            return func.transform(in.getStream()); } };
+            }
+        }
+    }
+    */
+
+    public static class File implements Fountain {
+        private final java.io.File file;
+        public File(java.io.File file) { this.file = file; }
+        public Stream getStream()      { return new Stream(file); }
+        public int getLength()         { return (int)file.length(); }
+        public int getNumLines()       { return Stream.countLines(getStream()); } 
+    }
+
+    // sketchy since the bytes can be modified
+    public static class ByteArray implements Fountain {
+        private final byte[] bytes;
+        private final int off;
+        private final int len;
+        public ByteArray(byte[] bytes)                   { this(bytes, 0, bytes.length); }
+        public ByteArray(byte[] bytes, int off, int len) { this.bytes = bytes; this.off=off; this.len=len; }
+        public Stream getStream()                        { return new Stream(bytes, off, len); }
+        public int getLength()                           { return len; }
+        public int getNumLines()                         { return Stream.countLines(getStream()); } 
+    }
+
+    public static class StringFountain implements Fountain {
+        String s;
+        public StringFountain(String s)                  { this.s = s; }
+        public Stream getStream()                        { return new Stream(s); }
+        public int getLength()                           { return s.length(); }   // FIXME ENCODING ISSUES!!!!!
+        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(); }
+    }
+
+    //public static class LazyCachingStreamFountain implements Fountain {
+    //}
+
+}