You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Downloader.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  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. package com.dmdirc.util.io;
  23. import java.io.BufferedReader;
  24. import java.io.DataOutputStream;
  25. import java.io.File;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.InputStreamReader;
  30. import java.io.OutputStream;
  31. import java.net.MalformedURLException;
  32. import java.net.URL;
  33. import java.net.URLConnection;
  34. import java.net.URLEncoder;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import java.util.Map;
  38. /**
  39. * Allows easy downloading of files from HTTP sites.
  40. */
  41. public final class Downloader {
  42. /** Creates a new instance of Downloader. */
  43. private Downloader() {
  44. // Shouldn't be used
  45. }
  46. /**
  47. * Retrieves the specified page.
  48. *
  49. * @param url The URL to retrieve
  50. * @return A list of lines received from the server
  51. * @throws java.io.IOException If there's an I/O error while downloading
  52. */
  53. public static List<String> getPage(final String url) throws IOException {
  54. return getPage(url, "");
  55. }
  56. /**
  57. * Retrieves the specified page, sending the specified post data.
  58. *
  59. * @param url The URL to retrieve
  60. * @param postData The raw POST data to send
  61. * @return A list of lines received from the server
  62. * @throws java.io.IOException If there's an I/O error while downloading
  63. */
  64. public static List<String> getPage(final String url, final String postData)
  65. throws IOException {
  66. final List<String> res = new ArrayList<String>();
  67. final URLConnection urlConn = getConnection(url, postData);
  68. BufferedReader in = null;
  69. try {
  70. in = new BufferedReader(new InputStreamReader(
  71. urlConn.getInputStream()));
  72. String line;
  73. do {
  74. line = in.readLine();
  75. if (line != null) {
  76. res.add(line);
  77. }
  78. } while (line != null);
  79. } finally {
  80. StreamUtils.close(in);
  81. }
  82. return res;
  83. }
  84. /**
  85. * Retrieves the specified page, sending the specified post data.
  86. *
  87. * @param url The URL to retrieve
  88. * @param postData A map of post data that should be sent
  89. * @return A list of lines received from the server
  90. * @throws java.io.IOException If there's an I/O error while downloading
  91. */
  92. public static List<String> getPage(final String url,
  93. final Map<String, String> postData) throws IOException {
  94. final StringBuilder data = new StringBuilder();
  95. for (Map.Entry<String, String> entry : postData.entrySet()) {
  96. data.append('&');
  97. data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  98. data.append('=');
  99. data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  100. }
  101. return getPage(url, data.length() == 0 ? "" : data.substring(1));
  102. }
  103. /**
  104. * Downloads the specified page to disk.
  105. *
  106. * @param url The URL to retrieve
  107. * @param file The file to save the page to
  108. * @throws java.io.IOException If there's an I/O error while downloading
  109. */
  110. public static void downloadPage(final String url, final String file)
  111. throws IOException {
  112. downloadPage(url, file, null);
  113. }
  114. /**
  115. * Downloads the specified page to disk.
  116. *
  117. * @param url The URL to retrieve
  118. * @param file The file to save the page to
  119. * @param listener The progress listener for this download
  120. * @throws java.io.IOException If there's an I/O error while downloading
  121. */
  122. public static void downloadPage(final String url, final String file,
  123. final DownloadListener listener) throws IOException {
  124. final URLConnection urlConn = getConnection(url, "");
  125. final File myFile = new File(file);
  126. OutputStream output = null;
  127. InputStream input = null;
  128. try {
  129. output = new FileOutputStream(myFile);
  130. input = urlConn.getInputStream();
  131. final int length = urlConn.getContentLength();
  132. int current = 0;
  133. if (listener != null) {
  134. listener.setIndeterminate(length == -1);
  135. }
  136. final byte[] buffer = new byte[512];
  137. int count;
  138. do {
  139. count = input.read(buffer);
  140. if (count > 0) {
  141. current += count;
  142. output.write(buffer, 0, count);
  143. if (listener != null && length != -1) {
  144. listener.downloadProgress(100 * (float) current
  145. / length);
  146. }
  147. }
  148. } while (count > 0);
  149. } finally {
  150. StreamUtils.close(input);
  151. StreamUtils.close(output);
  152. }
  153. }
  154. /**
  155. * Creates an URL connection for the specified URL and data.
  156. *
  157. * @param url The URL to connect to
  158. * @param postData The POST data to pass to the URL
  159. * @return An URLConnection for the specified URL/data
  160. * @throws java.net.MalformedURLException If the specified URL is malformed
  161. * @throws java.io.IOException If an I/O exception occurs while connecting
  162. */
  163. private static URLConnection getConnection(final String url,
  164. final String postData)
  165. throws MalformedURLException, IOException {
  166. final URL myUrl = new URL(url);
  167. final URLConnection urlConn = myUrl.openConnection();
  168. urlConn.setUseCaches(false);
  169. urlConn.setDoInput(true);
  170. urlConn.setDoOutput(postData.length() > 0);
  171. urlConn.setConnectTimeout(10000);
  172. if (postData.length() > 0) {
  173. urlConn.setRequestProperty("Content-Type",
  174. "application/x-www-form-urlencoded");
  175. DataOutputStream out = null;
  176. try {
  177. out = new DataOutputStream(urlConn.getOutputStream());
  178. out.writeBytes(postData);
  179. out.flush();
  180. } finally {
  181. StreamUtils.close(out);
  182. }
  183. }
  184. return urlConn;
  185. }
  186. }