Browse Source

Tidy up downloader.

pull/22/head
Greg Holmes 9 years ago
parent
commit
c7d9420b9a
2 changed files with 12 additions and 65 deletions
  1. 12
    62
      src/com/dmdirc/util/io/Downloader.java
  2. 0
    3
      test/com/dmdirc/util/io/DownloaderTest.java

+ 12
- 62
src/com/dmdirc/util/io/Downloader.java View File

25
 import java.io.BufferedReader;
25
 import java.io.BufferedReader;
26
 import java.io.DataOutputStream;
26
 import java.io.DataOutputStream;
27
 import java.io.IOException;
27
 import java.io.IOException;
28
-import java.io.InputStream;
29
 import java.io.InputStreamReader;
28
 import java.io.InputStreamReader;
30
-import java.io.OutputStream;
31
 import java.net.URL;
29
 import java.net.URL;
32
 import java.net.URLConnection;
30
 import java.net.URLConnection;
33
-import java.net.URLEncoder;
34
 import java.nio.file.Files;
31
 import java.nio.file.Files;
35
 import java.nio.file.Path;
32
 import java.nio.file.Path;
36
-import java.util.ArrayList;
37
 import java.util.List;
33
 import java.util.List;
38
 import java.util.Map;
34
 import java.util.Map;
35
+import java.util.stream.Collectors;
39
 
36
 
40
 /**
37
 /**
41
  * Allows easy downloading of files from HTTP sites.
38
  * Allows easy downloading of files from HTTP sites.
50
      * @throws IOException If there's an I/O error while downloading
47
      * @throws IOException If there's an I/O error while downloading
51
      */
48
      */
52
     public List<String> getPage(final String url) throws IOException {
49
     public List<String> getPage(final String url) throws IOException {
53
-
54
         return getPage(url, "");
50
         return getPage(url, "");
55
     }
51
     }
56
 
52
 
64
      */
60
      */
65
     public List<String> getPage(final String url, final String postData)
61
     public List<String> getPage(final String url, final String postData)
66
             throws IOException {
62
             throws IOException {
67
-
68
-        final List<String> res = new ArrayList<>();
69
-
70
         final URLConnection urlConn = getConnection(url, postData);
63
         final URLConnection urlConn = getConnection(url, postData);
71
-
64
+        final List<String> res;
72
         try (BufferedReader in = new BufferedReader(new InputStreamReader(
65
         try (BufferedReader in = new BufferedReader(new InputStreamReader(
73
                 urlConn.getInputStream()))) {
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
         return res;
69
         return res;
85
     }
70
     }
86
 
71
 
94
      */
79
      */
95
     public List<String> getPage(final String url,
80
     public List<String> getPage(final String url,
96
             final Map<String, String> postData) throws IOException {
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
      * @param listener The progress listener for this download
103
      * @param listener The progress listener for this download
127
      * @throws IOException If there's an I/O error while downloading
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
      * @return An URLConnection for the specified URL/data
119
      * @return An URLConnection for the specified URL/data
168
      * @throws IOException If an I/O exception occurs while connecting
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
             throws IOException {
123
             throws IOException {
173
         final URLConnection urlConn = getURLConnection(url);
124
         final URLConnection urlConn = getURLConnection(url);
174
 
125
 
178
         urlConn.setConnectTimeout(10000);
129
         urlConn.setConnectTimeout(10000);
179
 
130
 
180
         if (!postData.isEmpty()) {
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
             try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
134
             try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
185
                 out.writeBytes(postData);
135
                 out.writeBytes(postData);

+ 0
- 3
test/com/dmdirc/util/io/DownloaderTest.java View File

47
 import static org.junit.Assert.assertFalse;
47
 import static org.junit.Assert.assertFalse;
48
 import static org.junit.Assert.assertTrue;
48
 import static org.junit.Assert.assertTrue;
49
 import static org.mockito.Matchers.anyBoolean;
49
 import static org.mockito.Matchers.anyBoolean;
50
-import static org.mockito.Matchers.anyInt;
51
-import static org.mockito.Mockito.atLeastOnce;
52
 import static org.mockito.Mockito.never;
50
 import static org.mockito.Mockito.never;
53
 import static org.mockito.Mockito.verify;
51
 import static org.mockito.Mockito.verify;
54
 import static org.mockito.Mockito.when;
52
 import static org.mockito.Mockito.when;
123
         assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
121
         assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
124
                 Charset.forName("UTF-8")));
122
                 Charset.forName("UTF-8")));
125
         verify(listener).setIndeterminate(anyBoolean());
123
         verify(listener).setIndeterminate(anyBoolean());
126
-        verify(listener, atLeastOnce()).downloadProgress(anyInt());
127
     }
124
     }
128
 
125
 
129
     private class TestableDownloader extends Downloader {
126
     private class TestableDownloader extends Downloader {

Loading…
Cancel
Save