2003/10/25 07:50:21
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:03 +0000 (07:40 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:03 +0000 (07:40 +0000)
darcs-hash:20040130074003-2ba56-37a9b9db3acfb3e836e0267d878cd8ff7cf51293.gz

src/org/xwt/util/CachedInputStream.java
src/org/xwt/util/KnownLength.java [new file with mode: 0644]

index fdf8a78..d46569e 100644 (file)
@@ -34,10 +34,11 @@ public class CachedInputStream {
         notifyAll();
     }
 
-    private class SubStream extends InputStream {
+    private class SubStream extends InputStream implements KnownLength {
         int pos = 0;
         public int available() { return Math.max(0, size - pos); }
         public long skip(long n) throws IOException { pos += (int)n; return n; }     // FEATURE: don't skip past EOF
+        public int getLength() { return eof ? size : is instanceof KnownLength ? ((KnownLength)is).getLength() : 0; }
         public int read() throws IOException {                                       // FEATURE: be smarter here
             byte[] b = new byte[1];
             int ret = read(b, 0, 1);
diff --git a/src/org/xwt/util/KnownLength.java b/src/org/xwt/util/KnownLength.java
new file mode 100644 (file)
index 0000000..181607e
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.xwt.util;
+import java.io.*;
+
+/** a generic interface for things that "know" their length */
+public interface KnownLength {
+
+    public abstract int getLength();
+
+    public static class KnownLengthInputStream extends FilterInputStream implements KnownLength {
+        int length;
+        public int getLength() { return length; }
+        public KnownLengthInputStream(java.io.InputStream parent, int length) {
+            super(parent);
+            this.length = length;
+        }
+    }
+
+}