1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.codehaus.mojo.license;
23
24 import java.io.*;
25 import java.net.URL;
26 import java.net.URLConnection;
27
28
29
30
31
32
33
34 public class LicenseDownloader
35 {
36
37
38
39
40 public static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
41
42 public static void downloadLicense( String licenseUrlString, File outputFile )
43 throws IOException
44 {
45 InputStream licenseInputStream = null;
46 FileOutputStream fos = null;
47
48 try
49 {
50 URL licenseUrl = new URL( licenseUrlString );
51 URLConnection connection = licenseUrl.openConnection();
52 connection.setConnectTimeout( DEFAULT_CONNECTION_TIMEOUT );
53 connection.setReadTimeout( DEFAULT_CONNECTION_TIMEOUT );
54 licenseInputStream = connection.getInputStream();
55 fos = new FileOutputStream( outputFile );
56 copyStream( licenseInputStream, fos );
57 licenseInputStream.close();
58 fos.close();
59 }
60 finally
61 {
62 FileUtil.tryClose( licenseInputStream );
63 FileUtil.tryClose( fos );
64 }
65
66 }
67
68
69
70
71
72
73
74
75 private static void copyStream( InputStream inStream, OutputStream outStream )
76 throws IOException
77 {
78 byte[] buf = new byte[1024];
79 int len;
80 while ( ( len = inStream.read( buf ) ) > 0 )
81 {
82 outStream.write( buf, 0, len );
83 }
84 }
85
86 }