[Schmitzm-commits] r1970 - trunk/schmitzm-core/src/main/java/de/schmitzm/io
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Wed Apr 25 19:34:27 CEST 2012
Author: mojays
Date: 2012-04-25 19:34:26 +0200 (Wed, 25 Apr 2012)
New Revision: 1970
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
Log:
IOUtil: possibility to set number of retries for downloadUrlToFile(.)
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-04-25 17:04:55 UTC (rev 1969)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-04-25 17:34:26 UTC (rev 1970)
@@ -571,12 +571,37 @@
/**
* Downloads an {@link URL} to local file.
* @param url remote URL
+ * @param file local file (if {@code null} a temporary file is created, which will be
+ * deleted after JVM exit!)
+ * @param timeout connection timeout in ms
+ * @param maxRetries number of download retries in case of any error
+ */
+ public static File downloadUrlToFile(URL url, File file, int timeout, int maxRetries) throws IOException {
+ return downloadUrlToFile(url, null, file, timeout, maxRetries);
+ }
+
+ /**
+ * Downloads an {@link URL} to local file.
+ * @param url remote URL
* @param proxy proxy server used for url connection (can be {@code null})
* @param file local file (if {@code null} a temporary file is created, which will be
* deleted after JVM exit!)
* @param timeout connection timeout in ms
*/
public static File downloadUrlToFile(URL url, Proxy proxy, File file, int timeout) throws IOException {
+ return downloadUrlToFile(url, proxy, file, timeout, 0);
+ }
+
+ /**
+ * Downloads an {@link URL} to local file.
+ * @param url remote URL
+ * @param proxy proxy server used for url connection (can be {@code null})
+ * @param file local file (if {@code null} a temporary file is created, which will be
+ * deleted after JVM exit!)
+ * @param timeout connection timeout in ms
+ * @param maxRetries number of download retries in case of any error
+ */
+ public static File downloadUrlToFile(URL url, Proxy proxy, File file, int timeout, int maxRetries) throws IOException {
if ( file == null )
file = createTemporaryFile("java-schmitzm-",null,true);
if ( proxy == null )
@@ -584,57 +609,70 @@
LOGGER.debug("Download: "+url+" to "+file);
- // do like FileUtils.copyURLToFile(URL,File,int,int)
- // from Apache Commons IO 2.0
- // JUST HANDLE USING PROXY!!
- // Note: Problem with read timeout exception remains! :-(
- URLConnection connection = url.openConnection(proxy);
- connection.setConnectTimeout(timeout);
- connection.setReadTimeout(timeout);
- InputStream input = connection.getInputStream();
- copyInputStreamToFile(input, file);
+ for (int retry=0;; retry++)
+ try {
+ // do like FileUtils.copyURLToFile(URL,File,int,int)
+ // from Apache Commons IO 2.0
+ // JUST HANDLE USING PROXY!!
+ // Note: Problem with read timeout exception remains! :-(
+ URLConnection connection = url.openConnection(proxy);
+ connection.setConnectTimeout(timeout);
+ connection.setReadTimeout(timeout);
+ InputStream input = connection.getInputStream();
+ copyInputStreamToFile(input, file);
+
+// OutputStream output = null;
+// InputStream input = null;
+// long debugStartTime = System.currentTimeMillis();
+// try {
+// // Prepare output
+// output = new FileOutputStream(file);
+// // Prepare input from URL
+// URLConnection urlconn = url.openConnection(proxy);
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.opernConnection(.)");
+// if ( timeout >= 0 ) {
+// urlconn.setConnectTimeout(timeout);
+// urlconn.setReadTimeout(timeout);
+// }
+// urlconn.setDoOutput(true);
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.setDoOutput(true)");
+// urlconn.connect();
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.connect()");
+// input = urlconn.getInputStream();
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.getInputStream()");
+// // Read/write line-wise
+// byte[] buffer = new byte[204800];
+// int bufferSize = 0;
+// while ( (bufferSize = input.read(buffer)) > 0 ) {
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read(.). BufferSize "+bufferSize);
+// output.write(buffer, 0, bufferSize);
+// }
+// // int i;
+// // while ( (i = input.read()) != -1 ) {
+// //// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read()");
+// // output.write(i);
+// // }
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After read/write loop");
+// output.flush();
+// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After OutputStream.flush()");
+// // urlconn.dis
+// } finally {
+// closeInputStream(input);
+// closeOutputStream(output);
+// }
+
+ break;
+ } catch ( IOException err ) {
+ if ( maxRetries > 0 ) {
+ LOGGER.warn("Error downloading "+url+": "+err.getMessage());
+ if ( retry < maxRetries ) {
+ LOGGER.warn("Starting retry "+(retry+1)+" of "+maxRetries);
+ continue;
+ }
+ }
+ throw err;
+ }
-
-// OutputStream output = null;
-// InputStream input = null;
-// long debugStartTime = System.currentTimeMillis();
-// try {
-// // Prepare output
-// output = new FileOutputStream(file);
-// // Prepare input from URL
-// URLConnection urlconn = url.openConnection(proxy);
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.opernConnection(.)");
-// if ( timeout >= 0 ) {
-// urlconn.setConnectTimeout(timeout);
-// urlconn.setReadTimeout(timeout);
-// }
-// urlconn.setDoOutput(true);
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.setDoOutput(true)");
-// urlconn.connect();
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.connect()");
-// input = urlconn.getInputStream();
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After URLConnection.getInputStream()");
-// // Read/write line-wise
-// byte[] buffer = new byte[204800];
-// int bufferSize = 0;
-// while ( (bufferSize = input.read(buffer)) > 0 ) {
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read(.). BufferSize "+bufferSize);
-// output.write(buffer, 0, bufferSize);
-// }
-//// int i;
-//// while ( (i = input.read()) != -1 ) {
-////// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After InputStream.read()");
-//// output.write(i);
-//// }
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After read/write loop");
-// output.flush();
-// LOGGER.debug(LangUtil.getDelaySeconds(debugStartTime, null)+"s: After OutputStream.flush()");
-//// urlconn.dis
-// } finally {
-// closeInputStream(input);
-// closeOutputStream(output);
-// }
-
return file;
}
More information about the Schmitzm-commits
mailing list