JAL-1859 refactor duplicated code to new HttpUtils class
[jalview.git] / src / jalview / util / HttpUtils.java
1 package jalview.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6
7 public class HttpUtils
8 {
9
10   /**
11    * Returns true if it is possible to open an input stream at the given URL,
12    * else false. The input stream is closed.
13    * 
14    * @param url
15    * @return
16    */
17   public static boolean isValidUrl(String url)
18   {
19     InputStream is = null;
20     try
21     {
22       is = new URL(url).openStream();
23       if (is != null)
24       {
25         return true;
26       }
27     } catch (IOException x)
28     {
29       // MalformedURLException, FileNotFoundException
30       return false;
31     } finally
32     {
33       if (is != null)
34       {
35         try
36         {
37           is.close();
38         } catch (IOException e)
39         {
40           // ignore
41         }
42       }
43     }
44     return false;
45   }
46
47 }