JAL-3806 fix and test for one-to-many sequence mapping case
[jalview.git] / src / jalview / ws / utils / UrlDownloadClient.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
22 package jalview.ws.utils;
23
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.net.URL;
27 import java.nio.channels.Channels;
28 import java.nio.channels.ReadableByteChannel;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.StandardCopyOption;
33
34 public class UrlDownloadClient
35 {
36   public UrlDownloadClient()
37   {
38
39   }
40
41   /**
42    * Download and save a file from a URL
43    * 
44    * @param urlstring
45    *          url to download from, as string
46    * @param outfile
47    *          the name of file to save the URLs to
48    * @throws IOException
49    */
50   public static void download(String urlstring, String outfile)
51           throws IOException
52   {
53     FileOutputStream fos = null;
54     ReadableByteChannel rbc = null;
55     Path temp = null;
56     try
57     {
58       temp = Files.createTempFile(".jalview_", ".tmp");
59
60       URL url = new URL(urlstring);
61       rbc = Channels.newChannel(url.openStream());
62       fos = new FileOutputStream(temp.toString());
63       fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
64
65       // copy tempfile to outfile once our download completes
66       // incase something goes wrong
67       Files.copy(temp, Paths.get(outfile),
68               StandardCopyOption.REPLACE_EXISTING);
69     } catch (IOException e)
70     {
71       throw e;
72     } finally
73     {
74       try
75       {
76         if (fos != null)
77         {
78           fos.close();
79         }
80       } catch (IOException e)
81       {
82         System.out.println(
83                 "Exception while closing download file output stream: "
84                         + e.getMessage());
85       }
86       try
87       {
88         if (rbc != null)
89         {
90           rbc.close();
91         }
92       } catch (IOException e)
93       {
94         System.out.println("Exception while closing download channel: "
95                 + e.getMessage());
96       }
97       try
98       {
99         if (temp != null)
100         {
101           Files.deleteIfExists(temp);
102         }
103       } catch (IOException e)
104       {
105         System.out.println("Exception while deleting download temp file: "
106                 + e.getMessage());
107       }
108     }
109   }
110 }