File Source: Downloader.java
/*
P/P * Method: com.dmdirc.util.Downloader__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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 com.dmdirc.logger.ErrorLevel;
26 import com.dmdirc.logger.Logger;
27
28 import java.io.BufferedReader;
29 import java.io.DataOutputStream;
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.UnsupportedEncodingException;
36 import java.net.MalformedURLException;
37 import java.net.URL;
38 import java.net.URLConnection;
39 import java.net.URLEncoder;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Map;
43
44 /**
45 * Allows easy downloading of files from HTTP sites.
46 *
47 * @author Chris
48 */
49 public final class Downloader {
50
51 /** Creates a new instance of Downloader. */
/*
P/P * Method: void com.dmdirc.util.Downloader()
*/
52 private Downloader() {
53 // Shouldn't be used
54 }
55
56 /**
57 * Retrieves the specified page.
58 *
59 * @param url The URL to retrieve
60 * @return A list of lines received from the server
61 * @throws java.net.MalformedURLException If the URL is malformed
62 * @throws java.io.IOException If there's an I/O error while downloading
63 */
64 public static List<String> getPage(final String url)
65 throws MalformedURLException, IOException {
66
/*
P/P * Method: List getPage(String)
*
* Postconditions:
* return_value == &new ArrayList(getPage#1*)
* new ArrayList(getPage#1*) num objects == 1
*/
67 return getPage(url, "");
68 }
69
70 /**
71 * Retrieves the specified page, sending the specified post data.
72 *
73 * @param url The URL to retrieve
74 * @param postData The raw POST data to send
75 * @return A list of lines received from the server
76 * @throws java.net.MalformedURLException If the URL is malformed
77 * @throws java.io.IOException If there's an I/O error while downloading
78 */
79 public static List<String> getPage(final String url, final String postData)
80 throws MalformedURLException, IOException {
81
/*
P/P * Method: List getPage(String, String)
*
* Preconditions:
* postData != null
*
* Postconditions:
* return_value == &new ArrayList(getPage#1)
* new ArrayList(getPage#1) num objects == 1
*
* Test Vectors:
* java.io.BufferedReader:readLine(...)@92: Addr_Set{null}, Inverse{null}
*/
82 final List<String> res = new ArrayList<String>();
83
84 final URLConnection urlConn = getConnection(url, postData);
85
86 final BufferedReader in = new BufferedReader(
87 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
99 in.close();
100
101 return res;
102 }
103
104 /**
105 * Retrieves the specified page, sending the specified post data.
106 *
107 * @param url The URL to retrieve
108 * @param postData A map of post data that should be sent
109 * @return A list of lines received from the server
110 * @throws java.net.MalformedURLException If the URL is malformed
111 * @throws java.io.IOException If there's an I/O error while downloading
112 */
113 public static List<String> getPage(final String url, final Map<String, String> postData)
114 throws MalformedURLException, IOException {
115
/*
P/P * Method: List getPage(String, Map)
*
* Preconditions:
* (soft) postData != null
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* java.util.Iterator:next(...)@119 != null
* java.util.Map:entrySet(...)@119 != null
*
* Postconditions:
* return_value == &new ArrayList(getPage#1*)
* new ArrayList(getPage#1*) num objects == 1
*/
116 final StringBuilder data = new StringBuilder();
117
118 try {
119 for (Map.Entry<String, String> entry : postData.entrySet()) {
120 data.append('&');
121 data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
122 data.append('=');
123 data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
124 }
125 } catch (UnsupportedEncodingException ex) {
126 Logger.appError(ErrorLevel.MEDIUM, "URLEncoder doesn't support UTF-8", ex);
127 }
128
129 return getPage(url, data.length() == 0 ? "" : data.substring(1));
130 }
131
132 /**
133 * Downloads the specified page to disk.
134 *
135 * @param url The URL to retrieve
136 * @param file The file to save the page to
137 * @throws java.io.IOException If there's an I/O error while downloading
138 */
139 public static void downloadPage(final String url, final String file)
140 throws IOException {
/*
P/P * Method: void downloadPage(String, String)
*/
141 downloadPage(url, file, null);
142 }
143
144 /**
145 * Downloads the specified page to disk.
146 *
147 * @param url The URL to retrieve
148 * @param file The file to save the page to
149 * @param listener The progress listener for this download
150 * @throws java.io.IOException If there's an I/O error while downloading
151 */
152 public static void downloadPage(final String url, final String file,
153 final DownloadListener listener) throws IOException {
154
/*
P/P * Method: void downloadPage(String, String, DownloadListener)
*
* Preconditions:
* (soft) listener.listeners != null
*
* Presumptions:
* (float) (java.net.URLConnection:getContentLength(...)@160) != +0
* java.net.URLConnection:getInputStream(...)@159 != null
*
* Postconditions:
* possibly_updated(listener.progress)
*
* Test Vectors:
* listener: Addr_Set{null}, Inverse{null}
* java.io.InputStream:read(...)@171: {-231..0}, {1..232-1}
* java.net.URLConnection:getContentLength(...)@160: {-1}, {-231..-2, 0..232-1}
*/
155 final URLConnection urlConn = getConnection(url, "");
156 final File myFile = new File(file);
157
158 final FileOutputStream output = new FileOutputStream(myFile);
159 final InputStream 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
183 input.close();
184 output.close();
185 }
186
187 /**
188 * Creates an URL connection for the specified URL and data.
189 *
190 * @param url The URL to connect to
191 * @param postData The POST data to pass to the URL
192 * @return An URLConnection for the specified URL/data
193 * @throws java.net.MalformedURLException If the specified URL is malformed
194 * @throws java.io.IOException If an I/O exception occurs while connecting
195 */
196 private static URLConnection getConnection(final String url, final String postData)
197 throws MalformedURLException, IOException {
/*
P/P * Method: URLConnection getConnection(String, String)
*
* Preconditions:
* postData != null
*
* Presumptions:
* java.net.URL:openConnection(...)@199 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* java.lang.String:length(...)@206: {0}, {1..232-1}
*/
198 final URL myUrl = new URL(url);
199 final URLConnection urlConn = myUrl.openConnection();
200
201 urlConn.setUseCaches(false);
202 urlConn.setDoInput(true);
203 urlConn.setDoOutput(postData.length() > 0);
204 urlConn.setConnectTimeout(10000);
205
206 if (postData.length() > 0) {
207 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
208
209 final DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
210 out.writeBytes(postData);
211 out.flush();
212 out.close();
213 }
214
215 return urlConn;
216 }
217
218 }
SofCheck Inspector Build Version : 2.17854
| Downloader.java |
2009-Jun-25 01:54:24 |
| Downloader.class |
2009-Sep-02 17:04:17 |