2003/10/25 07:50:21
[org.ibex.core.git] / src / org / xwt / util / CachedInputStream.java
index 1c2cffd..d46569e 100644 (file)
@@ -34,14 +34,15 @@ 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);
-            return ret == -1 ? -1 : b[0];
+            return ret == -1 ? -1 : b[0]&0xff;
         }
         public int read(byte[] b, int off, int len) throws IOException {
             synchronized(CachedInputStream.this) {
@@ -49,6 +50,7 @@ public class CachedInputStream {
                 if (eof && pos == size) return -1;
                 int count = Math.min(size - pos, len);
                 System.arraycopy(cache, pos, b, off, count);
+                pos += count;
                 return count;
             }
         }