1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 10 |
Complexity Density: 0.4 |
|
38 |
|
public class UrlDownloadClient |
39 |
|
{ |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@param |
44 |
|
|
45 |
|
@param |
46 |
|
|
47 |
|
@throws |
48 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 8 |
Complexity Density: 0.35 |
|
49 |
0 |
public static void download(String urlstring, String outfile)... |
50 |
|
throws IOException |
51 |
|
{ |
52 |
|
|
53 |
0 |
FileOutputStream fos = null; |
54 |
0 |
ReadableByteChannel rbc = null; |
55 |
0 |
Path temp = null; |
56 |
0 |
try |
57 |
|
{ |
58 |
0 |
temp = Files.createTempFile(".jalview_", ".tmp"); |
59 |
|
|
60 |
0 |
URL url = new URL(urlstring); |
61 |
0 |
rbc = Channels.newChannel(HttpUtils.openStream(url)); |
62 |
0 |
fos = new FileOutputStream(temp.toString()); |
63 |
0 |
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
0 |
Files.copy(temp, Paths.get(outfile), |
68 |
|
StandardCopyOption.REPLACE_EXISTING); |
69 |
|
} catch (IOException e) |
70 |
|
{ |
71 |
0 |
throw e; |
72 |
|
} finally |
73 |
|
{ |
74 |
0 |
try |
75 |
|
{ |
76 |
0 |
if (fos != null) |
77 |
|
{ |
78 |
0 |
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 |
0 |
try |
87 |
|
{ |
88 |
0 |
if (rbc != null) |
89 |
|
{ |
90 |
0 |
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 |
0 |
try |
99 |
|
{ |
100 |
0 |
if (temp != null) |
101 |
|
{ |
102 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
114 |
0 |
public static void download(String urlstring, File tempFile)... |
115 |
|
throws IOException |
116 |
|
{ |
117 |
0 |
if (!Platform.setFileBytes(tempFile, urlstring)) |
118 |
|
{ |
119 |
0 |
download(urlstring, tempFile.toString()); |
120 |
|
} |
121 |
|
} |
122 |
|
} |