소스 검색

Move downloader to util

Change-Id: I3b95893a0e93f166c4888e3344405cd5b89692d3
Reviewed-on: http://gerrit.dmdirc.com/727
Automatic-Compile: Shane Mc Cormack <shane@dmdirc.com>
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.6.3
Chris Smith 14 년 전
부모
커밋
10a7036086
2개의 변경된 파일272개의 추가작업 그리고 0개의 파일을 삭제
  1. 48
    0
      src/com/dmdirc/util/DownloadListener.java
  2. 224
    0
      src/com/dmdirc/util/Downloader.java

+ 48
- 0
src/com/dmdirc/util/DownloadListener.java 파일 보기

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+/**
26
+ * Defines the method that objects interested in receiving download progress
27
+ * updates should implement.
28
+ * 
29
+ * @author chris
30
+ */
31
+public interface DownloadListener {
32
+
33
+    /**
34
+     * Called when the progress of the download has changed.
35
+     * 
36
+     * @param percent The percentage of the file that has been downloaded
37
+     */
38
+    void downloadProgress(float percent);
39
+    
40
+    /**
41
+     * Called to notify the listener if this download has an indeterminate length.
42
+     * 
43
+     * @param indeterminate true or false
44
+     * 
45
+     * @since 0.6
46
+     */
47
+    void setIndeterminate(final boolean indeterminate);
48
+}

+ 224
- 0
src/com/dmdirc/util/Downloader.java 파일 보기

@@ -0,0 +1,224 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import java.io.BufferedReader;
26
+import java.io.DataOutputStream;
27
+import java.io.File;
28
+import java.io.FileOutputStream;
29
+import java.io.IOException;
30
+import java.io.InputStream;
31
+import java.io.InputStreamReader;
32
+import java.io.OutputStream;
33
+import java.io.UnsupportedEncodingException;
34
+import java.net.MalformedURLException;
35
+import java.net.URL;
36
+import java.net.URLConnection;
37
+import java.net.URLEncoder;
38
+import java.util.ArrayList;
39
+import java.util.List;
40
+import java.util.Map;
41
+
42
+/**
43
+ * Allows easy downloading of files from HTTP sites.
44
+ *
45
+ * @author Chris
46
+ */
47
+public final class Downloader {
48
+
49
+    /** Creates a new instance of Downloader. */
50
+    private Downloader() {
51
+        // Shouldn't be used
52
+    }
53
+
54
+    /**
55
+     * Retrieves the specified page.
56
+     *
57
+     * @param url The URL to retrieve
58
+     * @return A list of lines received from the server
59
+     * @throws java.net.MalformedURLException If the URL is malformed
60
+     * @throws java.io.IOException If there's an I/O error while downloading
61
+     */
62
+    public static List<String> getPage(final String url)
63
+            throws MalformedURLException, IOException {
64
+
65
+        return getPage(url, "");
66
+    }
67
+
68
+    /**
69
+     * Retrieves the specified page, sending the specified post data.
70
+     *
71
+     * @param url The URL to retrieve
72
+     * @param postData The raw POST data to send
73
+     * @return A list of lines received from the server
74
+     * @throws java.net.MalformedURLException If the URL is malformed
75
+     * @throws java.io.IOException If there's an I/O error while downloading
76
+     */
77
+    public static List<String> getPage(final String url, final String postData)
78
+            throws MalformedURLException, IOException {
79
+
80
+        final List<String> res = new ArrayList<String>();
81
+
82
+        final URLConnection urlConn = getConnection(url, postData);
83
+
84
+        BufferedReader in = null;
85
+
86
+        try {
87
+            in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
88
+
89
+            String line;
90
+
91
+            do {
92
+                line = in.readLine();
93
+
94
+                if (line != null) {
95
+                    res.add(line);
96
+                }
97
+            } while (line != null);
98
+        } finally {
99
+            StreamUtil.close(in);
100
+        }
101
+
102
+        return res;
103
+    }
104
+
105
+    /**
106
+     * Retrieves the specified page, sending the specified post data.
107
+     *
108
+     * @param url The URL to retrieve
109
+     * @param postData A map of post data that should be sent
110
+     * @return A list of lines received from the server
111
+     * @throws java.net.MalformedURLException If the URL is malformed
112
+     * @throws java.io.IOException If there's an I/O error while downloading
113
+     */
114
+    public static List<String> getPage(final String url, final Map<String, String> postData)
115
+            throws MalformedURLException, IOException {
116
+        final StringBuilder data = new StringBuilder();
117
+
118
+        for (Map.Entry<String, String> entry : postData.entrySet()) {
119
+            data.append('&');
120
+            data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
121
+            data.append('=');
122
+            data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
123
+        }
124
+
125
+        return getPage(url, data.length() == 0 ? "" : data.substring(1));
126
+    }
127
+
128
+    /**
129
+     * Downloads the specified page to disk.
130
+     *
131
+     * @param url The URL to retrieve
132
+     * @param file The file to save the page to
133
+     * @throws java.io.IOException If there's an I/O error while downloading
134
+     */
135
+    public static void downloadPage(final String url, final String file)
136
+            throws IOException {
137
+        downloadPage(url, file, null);
138
+    }
139
+
140
+    /**
141
+     * Downloads the specified page to disk.
142
+     *
143
+     * @param url The URL to retrieve
144
+     * @param file The file to save the page to
145
+     * @param listener The progress listener for this download
146
+     * @throws java.io.IOException If there's an I/O error while downloading
147
+     */
148
+    public static void downloadPage(final String url, final String file,
149
+            final DownloadListener listener) throws IOException {
150
+
151
+        final URLConnection urlConn = getConnection(url, "");
152
+        final File myFile = new File(file);
153
+
154
+        OutputStream output = null;
155
+        InputStream input = null;
156
+
157
+        try {
158
+            output = new FileOutputStream(myFile);
159
+            input = urlConn.getInputStream();
160
+            final int length = urlConn.getContentLength();
161
+            int current = 0;
162
+
163
+            if (listener != null) {
164
+                listener.setIndeterminate(length == -1);
165
+            }
166
+
167
+            final byte[] buffer = new byte[512];
168
+            int count;
169
+
170
+            do {
171
+                count = input.read(buffer);
172
+
173
+                if (count > 0) {
174
+                    current += count;
175
+                    output.write(buffer, 0, count);
176
+
177
+                    if (listener != null && length != -1) {
178
+                        listener.downloadProgress(100 * (float) current / length);
179
+                    }
180
+                }
181
+            } while (count > 0);
182
+        } finally {
183
+            StreamUtil.close(input);
184
+            StreamUtil.close(output);
185
+        }
186
+    }
187
+
188
+    /**
189
+     * Creates an URL connection for the specified URL and data.
190
+     *
191
+     * @param url The URL to connect to
192
+     * @param postData The POST data to pass to the URL
193
+     * @return An URLConnection for the specified URL/data
194
+     * @throws java.net.MalformedURLException If the specified URL is malformed
195
+     * @throws java.io.IOException If an I/O exception occurs while connecting
196
+     */
197
+    private static URLConnection getConnection(final String url, final String postData)
198
+            throws MalformedURLException, IOException {
199
+        final URL myUrl = new URL(url);
200
+        final URLConnection urlConn = myUrl.openConnection();
201
+
202
+        urlConn.setUseCaches(false);
203
+        urlConn.setDoInput(true);
204
+        urlConn.setDoOutput(postData.length() > 0);
205
+        urlConn.setConnectTimeout(10000);
206
+
207
+        if (postData.length() > 0) {
208
+            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
209
+
210
+            DataOutputStream out = null;
211
+
212
+            try {
213
+                out = new DataOutputStream(urlConn.getOutputStream());
214
+                out.writeBytes(postData);
215
+                out.flush();
216
+            } finally {
217
+                StreamUtil.close(out);
218
+            }
219
+        }
220
+
221
+        return urlConn;
222
+    }
223
+
224
+}

Loading…
취소
저장