correct finding of system classes
authorcrawshaw <crawshaw@ibex.org>
Fri, 31 Dec 2004 10:48:53 +0000 (10:48 +0000)
committercrawshaw <crawshaw@ibex.org>
Fri, 31 Dec 2004 10:48:53 +0000 (10:48 +0000)
darcs-hash:20041231104853-2eb37-54d57e58d2adc3933ae90605e8a599e0c3491a38.gz

src/org/ibex/tool/Compiler.java

index 7c771a6..bf1ad56 100644 (file)
@@ -247,33 +247,7 @@ public class Compiler {
     private final FileFilter filterDirs = new FileFilter() {
         public boolean accept(File path) { return path.isDirectory(); }
     };
-    private byte[] bytes(File bfile) {
-        byte[] bytes = new byte[2 * 1024];
-        int pos = 0, in = 0;
-        try {
-            InputStream bin = new FileInputStream(bfile);
-            while ((in = bin.read(bytes, pos, bytes.length - pos)) != -1) {
-                pos += in;
-                if (pos == bytes.length) {
-                    byte[] newbytes= new byte[pos * 2];
-                    System.arraycopy(bytes, 0, newbytes, 0, pos);
-                    bytes = newbytes;
-                }
-            }
-            bin.close();
-        } catch (IOException e) {
-            System.out.println("Error reading class file"); // FIXME
-            e.printStackTrace(); return null;
-        }
 
-        if (pos != bytes.length) {
-            byte[] newbytes= new byte[pos * 2];
-            System.arraycopy(bytes, 0, newbytes, 0, pos);
-            bytes = newbytes;
-        }
-
-        return bytes;
-    }
     private void filterSources(List s, File dir, char[][] pack, String srcdir) {
         List bclasses = new ArrayList();
         File bdir = new File(builddir, str(pack, File.separatorChar));
@@ -402,17 +376,13 @@ public class Compiler {
                 if (veryverbose) System.out.println("  found in classloader: "+ c);
                 IBinaryType b = (IBinaryType)loaded.get(c);
                 if (b == null) loaded.put(c, b = new ClassFileReader(
-                    read(c.getResourceAsStream(classname)),
-                    (str(p, '/') + '/' + classname).toCharArray()));
+                    bytes(c), (str(p, '/') + '/' + classname).toCharArray()));
                 return new NameEnvironmentAnswer(b, access);
             } catch (ClassNotFoundException e) {
             } catch (ClassFormatException e) {
                 e.printStackTrace();
                 throw new Error("ClassFormatException reading system class: " +
                     str(p, '.')+new String(n));
-            } catch (IOException e) {
-                e.printStackTrace();
-                throw new Error("IOException reading system class: "+str(p, '.')+new String(n));
             }
 
             // cut out searches for java.* packages in sources list
@@ -652,23 +622,37 @@ public class Compiler {
         return sb.toString().toCharArray();
     }
 
-    private static byte[] read(InputStream in) throws IOException {
-        byte[] bytes = new byte[16];
-        int off = 0, ret;
-        while ((ret = in.read(bytes, off, bytes.length - off)) != -1) {
-            off += ret;
-            if (off == bytes.length) {
-                byte[] nb = new byte[bytes.length * 2];
-                System.arraycopy(bytes, 0, nb, 0, bytes.length);
-                bytes = nb;
+    private static byte[] bytes(Class c) {
+        return bytes(c.getResourceAsStream('/' + c.getName().replace('.', '/') + ".class"));
+    }
+    private static byte[] bytes(File bfile) {
+        try { return bytes(new FileInputStream(bfile)); }
+        catch (FileNotFoundException e) { throw new Error("FileNotFoundException reading bytes: "+e); }
+    }
+    private static byte[] bytes(InputStream bin) {
+        byte[] bytes = new byte[2 * 1024];
+        int pos = 0, in = 0;
+        try {
+            while ((in = bin.read(bytes, pos, bytes.length - pos)) != -1) {
+                pos += in;
+                if (pos == bytes.length) {
+                    byte[] newbytes= new byte[pos * 2];
+                    System.arraycopy(bytes, 0, newbytes, 0, pos);
+                    bytes = newbytes;
+                }
             }
+            bin.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new Error("IOException reading class file: "+e);
         }
-        if (off != bytes.length) {
-            byte[] nb = new byte[off];
-            System.arraycopy(bytes, 0, nb, 0, off);
-            bytes = nb;
+
+        if (pos != bytes.length) {
+            byte[] newbytes= new byte[pos * 2];
+            System.arraycopy(bytes, 0, newbytes, 0, pos);
+            bytes = newbytes;
         }
+
         return bytes;
     }
-
 }