JAL-1859 refactor duplicated code to new HttpUtils class
[jalview.git] / src / jalview / util / HttpUtils.java
diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java
new file mode 100644 (file)
index 0000000..88df587
--- /dev/null
@@ -0,0 +1,47 @@
+package jalview.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+public class HttpUtils
+{
+
+  /**
+   * Returns true if it is possible to open an input stream at the given URL,
+   * else false. The input stream is closed.
+   * 
+   * @param url
+   * @return
+   */
+  public static boolean isValidUrl(String url)
+  {
+    InputStream is = null;
+    try
+    {
+      is = new URL(url).openStream();
+      if (is != null)
+      {
+        return true;
+      }
+    } catch (IOException x)
+    {
+      // MalformedURLException, FileNotFoundException
+      return false;
+    } finally
+    {
+      if (is != null)
+      {
+        try
+        {
+          is.close();
+        } catch (IOException e)
+        {
+          // ignore
+        }
+      }
+    }
+    return false;
+  }
+
+}