|
22 | 22 |
|
23 | 23 | package processing.app;
|
24 | 24 |
|
25 |
| -import org.apache.commons.compress.utils.IOUtils; |
26 |
| -import processing.app.legacy.PApplet; |
| 25 | +import static processing.app.I18n.tr; |
27 | 26 |
|
28 |
| -import javax.swing.*; |
29 | 27 | import java.io.BufferedReader;
|
| 28 | +import java.io.File; |
| 29 | +import java.io.FileOutputStream; |
30 | 30 | import java.io.IOException;
|
| 31 | +import java.io.InputStream; |
31 | 32 | import java.io.InputStreamReader;
|
32 | 33 | import java.net.URL;
|
33 | 34 | import java.net.URLEncoder;
|
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.StandardCopyOption; |
| 37 | +import java.util.List; |
34 | 38 | import java.util.Random;
|
| 39 | +import java.util.stream.Collectors; |
35 | 40 |
|
36 |
| -import static processing.app.I18n.tr; |
| 41 | +import javax.swing.JOptionPane; |
| 42 | + |
| 43 | +import org.apache.commons.compress.utils.IOUtils; |
| 44 | + |
| 45 | +import processing.app.legacy.PApplet; |
37 | 46 |
|
38 | 47 |
|
39 | 48 | /**
|
@@ -91,7 +100,7 @@ public void run() {
|
91 | 100 | System.getProperty("os.version") + "\t" +
|
92 | 101 | System.getProperty("os.arch"), "UTF-8");
|
93 | 102 |
|
94 |
| - int latest = readInt("https://www.arduino.cc/latest.txt?" + info); |
| 103 | + int latest = readIntFromURL("https://www.arduino.cc/latest.txt?" + info); |
95 | 104 |
|
96 | 105 | String prompt =
|
97 | 106 | tr("A new version of Arduino is available,\n" +
|
@@ -120,14 +129,24 @@ public void run() {
|
120 | 129 | }
|
121 | 130 |
|
122 | 131 |
|
123 |
| - protected int readInt(String filename) throws IOException { |
124 |
| - URL url = new URL(filename); |
125 |
| - BufferedReader reader = null; |
126 |
| - try { |
127 |
| - reader = new BufferedReader(new InputStreamReader(url.openStream())); |
128 |
| - return Integer.parseInt(reader.readLine()); |
129 |
| - } finally { |
130 |
| - IOUtils.closeQuietly(reader); |
| 132 | + protected int readIntFromURL(String _url) throws Exception { |
| 133 | + List<String> lines = readFileFromURL(_url); |
| 134 | + return Integer.parseInt(lines.get(0)); |
| 135 | + } |
| 136 | + |
| 137 | + protected List<String> readFileFromURL(String _url) throws IOException { |
| 138 | + URL url = new URL(_url); |
| 139 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));) { |
| 140 | + return in.lines().collect(Collectors.toList()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + protected void downloadFileFromURL(String _url, File dest) throws IOException { |
| 145 | + URL url = new URL(_url); |
| 146 | + try (InputStream in = url.openStream()) { |
| 147 | + try (FileOutputStream out = new FileOutputStream(dest)) { |
| 148 | + IOUtils.copy(in, out); |
| 149 | + } |
131 | 150 | }
|
132 | 151 | }
|
133 | 152 | }
|
0 commit comments