[Schmitzm-commits] r1484 - trunk/schmitzm-core/src/main/java/de/schmitzm/io

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 3 02:24:32 CET 2011


Author: alfonx
Date: 2011-02-03 02:24:30 +0100 (Thu, 03 Feb 2011)
New Revision: 1484

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
Log:
Imporved the urlExists mehtod not to hang without a timeout if it reaches an open port without a service running. Not timesout after 3 sec

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-02-02 22:46:05 UTC (rev 1483)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2011-02-03 01:24:30 UTC (rev 1484)
@@ -49,6 +49,7 @@
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.net.URLDecoder;
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
@@ -875,11 +876,14 @@
 	}
 
 	/**
-	 * Test whether it is possible to access the given URL.
+	 * Test whether it is possible to access the given URL. Times-out after 3s
 	 */
 	public static boolean urlExists(final URL url) {
 		try {
-			url.openStream().close();
+			URLConnection conn = url.openConnection();
+			conn.setConnectTimeout(3000);
+			conn.setReadTimeout(3000);
+			conn.getInputStream().close();
 		} catch (final IOException e) {
 			return false;
 		}
@@ -887,6 +891,18 @@
 	}
 
 	/**
+	 * Test whether it is possible to access the given URL. Times-out after 3s.
+	 * Returns <code>false</code> if the String is no valid URL.
+	 */
+	public static boolean urlExists(final String url) {
+		try {
+			return urlExists(new URL(url));
+		} catch (final Exception e) {
+			return false;
+		}
+	}
+
+	/**
 	 * Reads the contents of a File into one String. Watch the size!
 	 * 
 	 * @param file



More information about the Schmitzm-commits mailing list