Clover icon

Coverage Report

  1. Project Clover database Wed Nov 13 2024 18:27:33 GMT
  2. Package jalview.ws.utils

File UrlDownloadClient.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

8
25
2
1
122
82
10
0.4
12.5
2
5

Classes

Class Line # Actions
UrlDownloadClient 38 25 10
0.771428677.1%
 

Contributing tests

This file is covered by 56 tests. .

Source view

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