DownloadUtils.java: Change some in throwing exception

This commit is contained in:
khanhduytran0 2020-11-15 16:27:50 +07:00
parent 8f5608975e
commit 84d8fbacd0

View File

@ -11,15 +11,11 @@ public class DownloadUtils {
public static final String USER_AGENT = Tools.APP_NAME; public static final String USER_AGENT = Tools.APP_NAME;
public static final Charset utf8 = Charset.forName("UTF-8"); public static final Charset utf8 = Charset.forName("UTF-8");
public static void download(String url, OutputStream os) { public static void download(String url, OutputStream os) throws IOException {
try { download(new URL(url), os);
download(new URL(url), os);
} catch (Throwable malformed) {
throw new RuntimeException(malformed);
}
} }
public static void download(URL url, OutputStream os) throws Throwable { public static void download(URL url, OutputStream os) throws IOException {
InputStream is = null; InputStream is = null;
try { try {
// System.out.println("Connecting: " + url.toString()); // System.out.println("Connecting: " + url.toString());
@ -29,11 +25,13 @@ public class DownloadUtils {
conn.setDoInput(true); conn.setDoInput(true);
conn.connect(); conn.connect();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Server returned HTTP " + conn.getResponseCode() throw new IOException("Server returned HTTP " + conn.getResponseCode()
+ ": " + conn.getResponseMessage()); + ": " + conn.getResponseMessage());
} }
is = conn.getInputStream(); is = conn.getInputStream();
IOUtils.copy(is, os); IOUtils.copy(is, os);
} catch (IOException e) {
throw new IOException("Unable to download from " + url.toString(), e);
} finally { } finally {
if (is != null) { if (is != null) {
try { try {
@ -51,7 +49,7 @@ public class DownloadUtils {
return new String(bos.toByteArray(), utf8); return new String(bos.toByteArray(), utf8);
} }
public static void downloadFile(String url, File out) throws Throwable { public static void downloadFile(String url, File out) throws IOException {
out.getParentFile().mkdirs(); out.getParentFile().mkdirs();
File tempOut = File.createTempFile(out.getName(), ".part", out.getParentFile()); File tempOut = File.createTempFile(out.getName(), ".part", out.getParentFile());
BufferedOutputStream bos = null; BufferedOutputStream bos = null;
@ -66,7 +64,7 @@ public class DownloadUtils {
if (tempOut.exists()) { if (tempOut.exists()) {
tempOut.delete(); tempOut.delete();
} }
} catch (Throwable th2) { } catch (IOException th2) {
if (bos != null) { if (bos != null) {
bos.close(); bos.close();
} }
@ -75,7 +73,7 @@ public class DownloadUtils {
} }
throw th2; throw th2;
} }
} catch (Throwable th3) { } catch (IOException th3) {
if (bos != null) { if (bos != null) {
bos.close(); bos.close();