22
33import com .google .gson .Gson ;
44import com .google .gson .reflect .TypeToken ;
5- import org . apache . commons . io . FileUtils ;
5+ import i18nupdatemod . I18nUpdateMod ;
66import org .apache .commons .io .IOUtils ;
77import org .jetbrains .annotations .NotNull ;
88
99import java .io .IOException ;
10+ import java .io .InputStream ;
1011import java .io .InputStreamReader ;
12+ import java .io .InterruptedIOException ;
13+ import java .io .OutputStream ;
1114import java .lang .reflect .Type ;
1215import java .net .HttpURLConnection ;
1316import java .net .URI ;
1417import java .net .URISyntaxException ;
1518import java .net .URL ;
19+ import java .net .SocketTimeoutException ;
1620import java .nio .charset .StandardCharsets ;
21+ import java .nio .file .Files ;
1722import java .nio .file .Path ;
1823import java .util .ArrayList ;
1924import java .util .HashMap ;
@@ -35,8 +40,36 @@ public class AssetUtil {
3540
3641 public static void download (String url , Path localFile ) throws IOException , URISyntaxException {
3742 Log .info ("Downloading: %s -> %s" , url , localFile );
38- FileUtils .copyURLToFile (new URI (url ).toURL (), localFile .toFile (),
39- (int ) TimeUnit .SECONDS .toMillis (3 ), (int ) TimeUnit .SECONDS .toMillis (33 ));
43+
44+ HttpURLConnection connection = (HttpURLConnection ) new URI (url ).toURL ().openConnection ();
45+ connection .setRequestMethod ("GET" );
46+ connection .setConnectTimeout ((int ) TimeUnit .SECONDS .toMillis (3 ));
47+ connection .setReadTimeout ((int ) TimeUnit .SECONDS .toMillis (1 ));
48+
49+ try (InputStream input = connection .getInputStream ();
50+ OutputStream output = Files .newOutputStream (localFile )) {
51+ byte [] buffer = new byte [8192 ];
52+ while (true ) {
53+ if (I18nUpdateMod .shouldShutdown ) {
54+ throw new InterruptedIOException ("Download cancelled by user" );
55+ }
56+
57+ int read ;
58+ try {
59+ read = input .read (buffer );
60+ } catch (SocketTimeoutException e ) {
61+ continue ;
62+ }
63+
64+ if (read < 0 ) {
65+ break ;
66+ }
67+ output .write (buffer , 0 , read );
68+ }
69+ } finally {
70+ connection .disconnect ();
71+ }
72+
4073 Log .debug ("Downloaded: %s -> %s" , url , localFile );
4174 }
4275
0 commit comments