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 6.8KB

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