comparison mxtool/URLConnectionDownload.java @ 3723:6c5f528c7aac

Added a copy of the mxtool to repo.
author Doug Simon <doug.simon@oracle.com>
date Fri, 16 Dec 2011 16:46:33 +0100
parents
children 148fa38782e8
comparison
equal deleted inserted replaced
3722:7c5524a4e86e 3723:6c5f528c7aac
1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26 import java.util.regex.*;
27
28 /**
29 * Downloads content from a given URL to a given file.
30 *
31 * @param path where to write the content
32 * @param urls the URLs to try, stopping after the first successful one
33 */
34 public class URLConnectionDownload {
35
36 /**
37 * Downloads content from a given URL to a given file.
38 *
39 * @param args
40 * arg[0] is the path where to write the content. The remainder
41 * of args are the URLs to try, stopping after the first
42 * successful one
43 */
44 public static void main(String[] args) {
45 File path = new File(args[0]);
46 String[] urls = new String[args.length - 1];
47 System.arraycopy(args, 1, urls, 0, urls.length);
48
49 File parent = path.getParentFile();
50 makeDirectory(parent);
51
52 // Enable use of system proxies
53 System.setProperty("java.net.useSystemProxies", "true");
54
55 String proxy = System.getenv("HTTP_PROXY");
56 String proxyMsg = "";
57 if (proxy != null) {
58 Pattern p = Pattern.compile("(?:http://)?([^:]+)(:\\d+)?");
59 Matcher m = p.matcher(proxy);
60 if (m.matches()) {
61 String host = m.group(1);
62 String port = m.group(2);
63 System.setProperty("http.proxyHost", host);
64 if (port != null) {
65 port = port.substring(1); // strip ':'
66 System.setProperty("http.proxyPort", port);
67 }
68 proxyMsg = " via proxy " + proxy;
69 } else {
70 System.err.println("Value of HTTP_PROXY is not valid: " + proxy);
71 }
72 } else {
73 System.err.println("** If behind a firewall without direct internet access, use the HTTP_PROXY environment variable (e.g. 'env HTTP_PROXY=proxy.company.com:80 max ...') or download manually with a web browser.");
74 }
75
76 for (String s : urls) {
77 try {
78 System.err.println("Downloading " + s + " to " + path + proxyMsg);
79 URL url = new URL(s);
80 URLConnection conn = url.openConnection();
81 // 10 second timeout to establish connection
82 conn.setConnectTimeout(10000);
83 InputStream in = conn.getInputStream();
84 int size = conn.getContentLength();
85 FileOutputStream out = new FileOutputStream(path);
86 int read = 0;
87 byte[] buf = new byte[8192];
88 int n = 0;
89 while ((read = in.read(buf)) != -1) {
90 n += read;
91 System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " ( " + (n * 100 / size) + "%)"));
92 out.write(buf, 0, read);
93 }
94 System.err.println();
95 out.close();
96 in.close();
97 return;
98 } catch (MalformedURLException e) {
99 throw new Error("Error in URL " + s, e);
100 } catch (IOException e) {
101 System.err.println("Error reading from " + s + ": " + e);
102 path.delete();
103 }
104 }
105 throw new Error("Could not download content to " + path + " from " + Arrays.toString(urls));
106 }
107
108 private static void makeDirectory(File directory) {
109 if (!directory.exists() && !directory.mkdirs()) {
110 throw new Error("Could not make directory " + directory);
111 }
112 }
113 }
114