|
@@ -25,17 +25,14 @@ package com.dmdirc.util.io;
|
25
|
25
|
import java.io.BufferedReader;
|
26
|
26
|
import java.io.DataOutputStream;
|
27
|
27
|
import java.io.IOException;
|
28
|
|
-import java.io.InputStream;
|
29
|
28
|
import java.io.InputStreamReader;
|
30
|
|
-import java.io.OutputStream;
|
31
|
29
|
import java.net.URL;
|
32
|
30
|
import java.net.URLConnection;
|
33
|
|
-import java.net.URLEncoder;
|
34
|
31
|
import java.nio.file.Files;
|
35
|
32
|
import java.nio.file.Path;
|
36
|
|
-import java.util.ArrayList;
|
37
|
33
|
import java.util.List;
|
38
|
34
|
import java.util.Map;
|
|
35
|
+import java.util.stream.Collectors;
|
39
|
36
|
|
40
|
37
|
/**
|
41
|
38
|
* Allows easy downloading of files from HTTP sites.
|
|
@@ -50,7 +47,6 @@ public class Downloader {
|
50
|
47
|
* @throws IOException If there's an I/O error while downloading
|
51
|
48
|
*/
|
52
|
49
|
public List<String> getPage(final String url) throws IOException {
|
53
|
|
-
|
54
|
50
|
return getPage(url, "");
|
55
|
51
|
}
|
56
|
52
|
|
|
@@ -64,23 +60,12 @@ public class Downloader {
|
64
|
60
|
*/
|
65
|
61
|
public List<String> getPage(final String url, final String postData)
|
66
|
62
|
throws IOException {
|
67
|
|
-
|
68
|
|
- final List<String> res = new ArrayList<>();
|
69
|
|
-
|
70
|
63
|
final URLConnection urlConn = getConnection(url, postData);
|
71
|
|
-
|
|
64
|
+ final List<String> res;
|
72
|
65
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(
|
73
|
66
|
urlConn.getInputStream()))) {
|
74
|
|
- String line;
|
75
|
|
- do {
|
76
|
|
- line = in.readLine();
|
77
|
|
-
|
78
|
|
- if (line != null) {
|
79
|
|
- res.add(line);
|
80
|
|
- }
|
81
|
|
- } while (line != null);
|
|
67
|
+ res = in.lines().collect(Collectors.toList());
|
82
|
68
|
}
|
83
|
|
-
|
84
|
69
|
return res;
|
85
|
70
|
}
|
86
|
71
|
|
|
@@ -94,16 +79,8 @@ public class Downloader {
|
94
|
79
|
*/
|
95
|
80
|
public List<String> getPage(final String url,
|
96
|
81
|
final Map<String, String> postData) throws IOException {
|
97
|
|
- final StringBuilder data = new StringBuilder();
|
98
|
|
-
|
99
|
|
- for (Map.Entry<String, String> entry : postData.entrySet()) {
|
100
|
|
- data.append('&');
|
101
|
|
- data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
|
102
|
|
- data.append('=');
|
103
|
|
- data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
|
104
|
|
- }
|
105
|
|
-
|
106
|
|
- return getPage(url, data.length() == 0 ? "" : data.substring(1));
|
|
82
|
+ return getPage(url, postData.entrySet().stream().map(Object::toString)
|
|
83
|
+ .collect(Collectors.joining("&")));
|
107
|
84
|
}
|
108
|
85
|
|
109
|
86
|
/**
|
|
@@ -126,37 +103,12 @@ public class Downloader {
|
126
|
103
|
* @param listener The progress listener for this download
|
127
|
104
|
* @throws IOException If there's an I/O error while downloading
|
128
|
105
|
*/
|
129
|
|
- public void downloadPage(final String url, final Path file,
|
130
|
|
- final DownloadListener listener) throws IOException {
|
131
|
|
-
|
132
|
|
- final URLConnection urlConn = getConnection(url, "");
|
133
|
|
-
|
134
|
|
- try (OutputStream output = Files.newOutputStream(file);
|
135
|
|
- InputStream input = urlConn.getInputStream()) {
|
136
|
|
- final int length = urlConn.getContentLength();
|
137
|
|
-
|
138
|
|
- if (listener != null) {
|
139
|
|
- listener.setIndeterminate(length == -1);
|
140
|
|
- }
|
141
|
|
-
|
142
|
|
- final byte[] buffer = new byte[512];
|
143
|
|
- int count;
|
144
|
|
-
|
145
|
|
- int current = 0;
|
146
|
|
- do {
|
147
|
|
- count = input.read(buffer);
|
148
|
|
-
|
149
|
|
- if (count > 0) {
|
150
|
|
- current += count;
|
151
|
|
- output.write(buffer, 0, count);
|
152
|
|
-
|
153
|
|
- if (listener != null && length != -1) {
|
154
|
|
- listener.downloadProgress(100 * (float) current
|
155
|
|
- / length);
|
156
|
|
- }
|
157
|
|
- }
|
158
|
|
- } while (count > 0);
|
|
106
|
+ public void downloadPage(final String url, final Path file, final DownloadListener listener)
|
|
107
|
+ throws IOException {
|
|
108
|
+ if (listener != null) {
|
|
109
|
+ listener.setIndeterminate(true);
|
159
|
110
|
}
|
|
111
|
+ Files.copy(getConnection(url, "").getInputStream(), file);
|
160
|
112
|
}
|
161
|
113
|
|
162
|
114
|
/**
|
|
@@ -167,8 +119,7 @@ public class Downloader {
|
167
|
119
|
* @return An URLConnection for the specified URL/data
|
168
|
120
|
* @throws IOException If an I/O exception occurs while connecting
|
169
|
121
|
*/
|
170
|
|
- private URLConnection getConnection(final String url,
|
171
|
|
- final String postData)
|
|
122
|
+ private URLConnection getConnection(final String url, final String postData)
|
172
|
123
|
throws IOException {
|
173
|
124
|
final URLConnection urlConn = getURLConnection(url);
|
174
|
125
|
|
|
@@ -178,8 +129,7 @@ public class Downloader {
|
178
|
129
|
urlConn.setConnectTimeout(10000);
|
179
|
130
|
|
180
|
131
|
if (!postData.isEmpty()) {
|
181
|
|
- urlConn.setRequestProperty("Content-Type",
|
182
|
|
- "application/x-www-form-urlencoded");
|
|
132
|
+ urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
183
|
133
|
|
184
|
134
|
try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
|
185
|
135
|
out.writeBytes(postData);
|