added destination option for GetDep
[org.ibex.util.git] / src / org / ibex / util / GetDep.java
diff --git a/src/org/ibex/util/GetDep.java b/src/org/ibex/util/GetDep.java
new file mode 100644 (file)
index 0000000..9e0e67a
--- /dev/null
@@ -0,0 +1,63 @@
+// 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.util;
+import java.util.*;
+import java.net.*;
+import java.io.*;
+import java.util.zip.*;
+
+public final class GetDep {
+
+    public static void main(String[] s) throws Exception {
+        if (s.length < 2) {
+            System.out.println("usage: java "+GetDep.class.getName()+" <url> <jarfile>");
+            return;
+        }
+        fetch(s[1], s[0]);
+    }
+
+    public static void fetch(String path, String url) throws Exception {
+        InputStream is = fetch(url);
+        FileOutputStream fos = new FileOutputStream(path);
+        while(true) {
+            byte[] buf = new byte[1024 * 16];
+            int numread = is.read(buf, 0, buf.length);
+            if (numread == -1) break;
+            fos.write(buf, 0, numread);
+        }
+        fos.close();
+    }
+
+    public static InputStream fetch(String url) throws Exception {
+        String scheme = url.substring(0, url.indexOf(':'));
+        if (scheme.equals("zip") || scheme.equals("tgz")) {
+            int bang = url.lastIndexOf('!');
+            String path = url.substring(bang + 1);
+            url = url.substring(url.indexOf(':')+1, bang);
+            InputStream rest = fetch(url);
+            if (scheme.equals("zip")) {
+                ZipInputStream zis = new ZipInputStream(rest);
+                while(true) {
+                    ZipEntry ze = zis.getNextEntry();
+                    if (ze == null) break;
+                    if (ze.getName().equals(path)) return zis;
+                }
+                return null;
+            } else {
+                Tar.TarInputStream tis = new Tar.TarInputStream(new GZIPInputStream(rest));
+                while(true) {
+                    Tar.TarEntry te = tis.getNextEntry();
+                    if (te == null) break;
+                    if (te.getName().equals(path)) return tis;
+                }
+                return null;
+            }
+        } else {
+            URL u = new URL(url);
+            return u.openConnection().getInputStream();
+        }
+    }
+
+}