2003/10/20 03:37:12
[org.ibex.core.git] / src / org / xwt / Res.java
index 896220b..60a9437 100644 (file)
@@ -21,8 +21,30 @@ public abstract class Res extends JS {
 
     public Res getParent() { return null; }
 
+    /** an InputStream that makes sure it is not in the MessageQueue when blocked on a read */
+    private static class BackgroundInputStream extends FilterInputStream {
+        BackgroundInputStream(InputStream i) { super(i); }
+        private void suspend() throws IOException {
+            if (!ThreadMessage.suspendThread())
+                throw new IOException("attempt to perform background-only operation in a foreground thread");
+        }
+        private void resume() {
+            ThreadMessage.resumeThread();
+        }
+        public int read() throws IOException {
+            suspend();
+            try { return super.read(); }
+            finally { resume(); }
+        }
+        public int read(byte[] b, int off, int len) throws IOException {
+            suspend();
+            try { return super.read(b, off, len); }
+            finally { resume(); }
+        }
+    }
+
     /** returns an InputStream containing the Resource's contents */
-    public InputStream getInputStream() throws IOException { return getInputStream(""); }
+    public final InputStream getInputStream() throws IOException { return new BackgroundInputStream(getInputStream("")); }
     public abstract InputStream getInputStream(String path) throws IOException;
 
     /** graft newResource in place of this resource on its parent */
@@ -46,14 +68,12 @@ public abstract class Res extends JS {
         return ret;
     }
 
-    public static Res stringToRes(String url) { return stringToRes(url, false); }
-    public static Res stringToRes(String url, boolean permitLocalFilesystem) {
+    public static Res stringToRes(String url) {
         if (url.indexOf('!') != -1)
             return (Res)(new Zip(stringToRes(url.substring(0, url.lastIndexOf('!')))).
                          get(url.substring(url.lastIndexOf('!') + 1)));
         if (url.startsWith("http://")) return new HTTP(url);
         if (url.startsWith("https://")) return new HTTP(url);
-        if (url.startsWith("file:") && permitLocalFilesystem) return new File(url.substring(5));
         if (url.startsWith("cab:")) return new CAB(stringToRes(url.substring(4)));
         if (url.startsWith("data:")) return new ByteArray(Base64.decode(url.substring(5)));
         if (url.startsWith("utf8:")) return new ByteArray(url.substring(5).getBytes());
@@ -100,16 +120,6 @@ public abstract class Res extends JS {
             return new FileInputStream((path + rest).replace('/', java.io.File.separatorChar)); }
     }
 
-    /** wrap a Res around a preexisting InputStream */
-    public static class IS extends Res {
-        InputStream parent;
-        IS(InputStream parent) { this.parent = parent; }
-        public InputStream getInputStream(String path) {
-            if (!"".equals(path)) throw new JS.Exn("can't access subresources of IS");
-            return parent;
-        }
-    }
-
     /** "unwrap" a Zip archive */
     public static class Zip extends Res {
         private Res parent;
@@ -119,7 +129,7 @@ public abstract class Res extends JS {
             ZipInputStream zis = new ZipInputStream(parent.getInputStream());
             ZipEntry ze = zis.getNextEntry();
             while(ze != null && !ze.getName().equals(path)) ze = zis.getNextEntry();
-            if (ze == null) throw new JS.Exn("requested file not found in archive");
+            if (ze == null) throw new JS.Exn("requested file (" + path + ") not found in archive");
             return zis;
         }
     }