Skip to content

Commit d5d4751

Browse files
committed
Added other helper functions to download data from URLs
1 parent e9625f2 commit d5d4751

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

app/src/processing/app/UpdateCheck.java

+32-13
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,27 @@
2222

2323
package processing.app;
2424

25-
import org.apache.commons.compress.utils.IOUtils;
26-
import processing.app.legacy.PApplet;
25+
import static processing.app.I18n.tr;
2726

28-
import javax.swing.*;
2927
import java.io.BufferedReader;
28+
import java.io.File;
29+
import java.io.FileOutputStream;
3030
import java.io.IOException;
31+
import java.io.InputStream;
3132
import java.io.InputStreamReader;
3233
import java.net.URL;
3334
import java.net.URLEncoder;
35+
import java.nio.file.Files;
36+
import java.nio.file.StandardCopyOption;
37+
import java.util.List;
3438
import java.util.Random;
39+
import java.util.stream.Collectors;
3540

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;
3746

3847

3948
/**
@@ -91,7 +100,7 @@ public void run() {
91100
System.getProperty("os.version") + "\t" +
92101
System.getProperty("os.arch"), "UTF-8");
93102

94-
int latest = readInt("https://www.arduino.cc/latest.txt?" + info);
103+
int latest = readIntFromURL("https://www.arduino.cc/latest.txt?" + info);
95104

96105
String prompt =
97106
tr("A new version of Arduino is available,\n" +
@@ -120,14 +129,24 @@ public void run() {
120129
}
121130

122131

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+
}
131150
}
132151
}
133152
}

0 commit comments

Comments
 (0)