comparison mxtool/URLConnectionDownload.java @ 15922:2022366b513c

mx: add verbose mode to download helper
author Bernhard Urban <bernhard.urban@jku.at>
date Tue, 27 May 2014 12:14:54 +0200
parents db072eec897e
children 3152f72f5cda
comparison
equal deleted inserted replaced
15921:6d8c901814eb 15922:2022366b513c
40 * arg[0] is the path where to write the content. The remainder 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 41 * of args are the URLs to try, stopping after the first
42 * successful one 42 * successful one
43 */ 43 */
44 public static void main(String[] args) { 44 public static void main(String[] args) {
45 File path = new File(args[0]); 45 File path = new File(args[0]);
46 String[] urls = new String[args.length - 1]; 46 boolean verbose = args[1].equals("-v");
47 System.arraycopy(args, 1, urls, 0, urls.length); 47 int offset = verbose ? 2 : 1;
48 String[] urls = new String[args.length - offset];
49 System.arraycopy(args, offset, urls, 0, urls.length);
48 50
49 File parent = path.getParentFile(); 51 File parent = path.getParentFile();
50 makeDirectory(parent); 52 makeDirectory(parent);
51 53
52 // Enable use of system proxies 54 // Enable use of system proxies
90 int read = 0; 92 int read = 0;
91 byte[] buf = new byte[8192]; 93 byte[] buf = new byte[8192];
92 int n = 0; 94 int n = 0;
93 while ((read = in.read(buf)) != -1) { 95 while ((read = in.read(buf)) != -1) {
94 n += read; 96 n += read;
95 long percent = ((long) n * 100 / size); 97 if (verbose) {
96 System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)")); 98 long percent = ((long) n * 100 / size);
99 System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)"));
100 }
97 out.write(buf, 0, read); 101 out.write(buf, 0, read);
98 } 102 }
99 System.err.println(); 103 System.err.println();
100 out.close(); 104 out.close();
101 in.close(); 105 in.close();