Merge branch 'Jalview-JS/develop' into merge_js_develop
[jalview.git] / src / jalview / util / HttpUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import java.io.BufferedInputStream;
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29
30 public class HttpUtils
31 {
32
33   /**
34    * Returns true if it is possible to open an input stream at the given URL,
35    * else false. The input stream is closed.
36    * 
37    * @param url
38    * @return
39    */
40   public static boolean isValidUrl(String url)
41   {
42     InputStream is = null;
43     try
44     {
45       is = new URL(url).openStream();
46       if (is != null)
47       {
48         return true;
49       }
50     } catch (IOException x)
51     {
52       // MalformedURLException, FileNotFoundException
53       return false;
54     } finally
55     {
56       if (is != null)
57       {
58         try
59         {
60           is.close();
61         } catch (IOException e)
62         {
63           // ignore
64         }
65       }
66     }
67     return false;
68   }
69
70   /**
71    * download from given URL and return a pointer to temporary file
72    */
73   public static File fetchURLToTemp(String url) throws OutOfMemoryError,
74           IOException
75   {
76     long time = System.currentTimeMillis();
77     URL rcall = new URL(url);
78
79     InputStream is = new BufferedInputStream(rcall.openStream());
80     File outFile = null;
81     try
82     {
83       outFile = File.createTempFile("jalview", ".xml");
84       outFile.deleteOnExit();
85       if (outFile.length() == 0)
86       {
87         outFile.delete();
88         return null;
89       }
90     } catch (Exception ex)
91     {
92     }
93
94     if (outFile != null)
95     {
96       FileOutputStream fio = new FileOutputStream(outFile);
97       byte[] bb = new byte[32 * 1024];
98       int l;
99       while ((l = is.read(bb)) > 0)
100       {
101         fio.write(bb, 0, l);
102       }
103       fio.close();
104       is.close();
105       return outFile;
106     }
107     else
108     {
109       return null;
110     }
111   }
112 }