Skip to content

Commit 864cea6

Browse files
committed
Added other helper functions to download data from URLs
1 parent 6d304c2 commit 864cea6

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

app/src/processing/app/UpdateCheck.java

Lines changed: 32 additions & 13 deletions
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
/**
@@ -89,7 +98,7 @@ public void run() {
8998
System.getProperty("os.version") + "\t" +
9099
System.getProperty("os.arch"), "UTF-8");
91100

92-
int latest = readInt("https://www.arduino.cc/latest.txt?" + info);
101+
int latest = readIntFromURL("https://www.arduino.cc/latest.txt?" + info);
93102

94103
String prompt =
95104
tr("A new version of Arduino is available,\n" +
@@ -118,14 +127,24 @@ public void run() {
118127
}
119128

120129

121-
protected int readInt(String filename) throws IOException {
122-
URL url = new URL(filename);
123-
BufferedReader reader = null;
124-
try {
125-
reader = new BufferedReader(new InputStreamReader(url.openStream()));
126-
return Integer.parseInt(reader.readLine());
127-
} finally {
128-
IOUtils.closeQuietly(reader);
130+
protected int readIntFromURL(String _url) throws Exception {
131+
List<String> lines = readFileFromURL(_url);
132+
return Integer.parseInt(lines.get(0));
133+
}
134+
135+
protected List<String> readFileFromURL(String _url) throws IOException {
136+
URL url = new URL(_url);
137+
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));) {
138+
return in.lines().collect(Collectors.toList());
139+
}
140+
}
141+
142+
protected void downloadFileFromURL(String _url, File dest) throws IOException {
143+
URL url = new URL(_url);
144+
try (InputStream in = url.openStream()) {
145+
try (FileOutputStream out = new FileOutputStream(dest)) {
146+
IOUtils.copy(in, out);
147+
}
129148
}
130149
}
131150
}

0 commit comments

Comments
 (0)