2525import java .net .ConnectException ;
2626import java .net .MalformedURLException ;
2727import java .net .NoRouteToHostException ;
28- import java .net .URI ;
29- import java .net .URL ;
30- import java .net .URLConnection ;
3128
32- import javafx .application .Platform ;
29+ import org .apache .hc .client5 .http .classic .HttpClient ;
30+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
31+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
32+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
3333
3434import com .tivo .kmttg .main .config ;
3535import com .tivo .kmttg .util .debug ;
@@ -46,7 +46,6 @@ public String download(String dir, String os) {
4646 log .error ("toolDownload - error retrieving base download URL" );
4747 return null ;
4848 }
49- base += "/tools/" ;
5049 debug .print ("dir=" + dir + " os=" + os );
5150 String urlString = null ;
5251 String localFileName = null ;
@@ -80,39 +79,16 @@ public String download(String dir, String os) {
8079 private Boolean downloadUrl (String urlString , String localFileName ) {
8180 BufferedInputStream in = null ;
8281 RandomAccessFile out = null ;
83- Integer size = 0 ;
84- int BLOCK_SIZE = 4096 ;
8582 try {
86- URL url = new URI (urlString ).toURL ();
8783 log .warn ("Downloading file: " + urlString + " ..." );
88- URLConnection con = url .openConnection ();
89- size = con .getContentLength ();
84+ HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ();
85+ HttpClient httpClient = httpClientBuilder .build ();
86+ HttpGet httpget = new HttpGet (urlString );
9087
91- in = new BufferedInputStream (con .getInputStream ());
88+ CloseableHttpResponse response = (CloseableHttpResponse ) httpClient .execute (httpget );
89+ in = new BufferedInputStream (response .getEntity ().getContent ());
9290 out = new RandomAccessFile (localFileName , "rw" );
9391
94- Integer howManyBytes ;
95- Integer readSoFar = 0 ;
96- byte [] bytesIn = new byte [BLOCK_SIZE ];
97- int last_pct = 0 ;
98-
99- while ((howManyBytes = in .read (bytesIn )) >= 0 ) {
100- out .write (bytesIn , 0 , howManyBytes );
101- readSoFar += howManyBytes ;
102- Float f = 100 *readSoFar .floatValue ()/size .floatValue ();
103- final Integer pct = f .intValue ();
104- final String title = String .format ("download: %d%% %s" , pct , config .kmttg );
105- if (pct % 5 == 0 && pct > last_pct ) {
106- last_pct += 5 ;
107- Platform .runLater (new Runnable () {
108- @ Override public void run () {
109- config .gui .progressBar_setValue (pct );
110- config .gui .setTitle (title );
111- }
112- });
113- }
114- }
115-
11692 // Done
11793 in .close ();
11894 out .close ();
0 commit comments