X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Futils%2FUrlDownloadClient.java;h=e2fb1b80ee30f363596ffb8abaf30cfb361080de;hb=57738a1f3c19b1c3a00bd3ac5108f8cd0af32f99;hp=cfe080ca80546a3cd0e03445c3f8dc3af93f8bd0;hpb=75b38b407cf19737bcc9013a483e1beee32f5523;p=jalview.git diff --git a/src/jalview/ws/utils/UrlDownloadClient.java b/src/jalview/ws/utils/UrlDownloadClient.java index cfe080c..e2fb1b8 100644 --- a/src/jalview/ws/utils/UrlDownloadClient.java +++ b/src/jalview/ws/utils/UrlDownloadClient.java @@ -21,7 +21,8 @@ package jalview.ws.utils; -import java.io.ByteArrayInputStream; +import jalview.util.Platform; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -35,11 +36,6 @@ import java.nio.file.StandardCopyOption; public class UrlDownloadClient { - public UrlDownloadClient() - { - - } - /** * Download and save a file from a URL * @@ -53,74 +49,71 @@ public class UrlDownloadClient throws IOException { - FileOutputStream fos = null; - ReadableByteChannel rbc = null; - Path temp = null; - try - { - temp = Files.createTempFile(".jalview_", ".tmp"); + FileOutputStream fos = null; + ReadableByteChannel rbc = null; + Path temp = null; + try + { + temp = Files.createTempFile(".jalview_", ".tmp"); - URL url = new URL(urlstring); - rbc = Channels.newChannel(url.openStream()); - fos = new FileOutputStream(temp.toString()); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + URL url = new URL(urlstring); + rbc = Channels.newChannel(url.openStream()); + fos = new FileOutputStream(temp.toString()); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - // copy tempfile to outfile once our download completes - // incase something goes wrong - Files.copy(temp, Paths.get(outfile), - StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) - { - throw e; - } finally + // copy tempfile to outfile once our download completes + // incase something goes wrong + Files.copy(temp, Paths.get(outfile), + StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) + { + throw e; + } finally + { + try { - try - { - if (fos != null) - { - fos.close(); - } - } catch (IOException e) + if (fos != null) { - System.out.println( - "Exception while closing download file output stream: " - + e.getMessage()); + fos.close(); } - try - { - if (rbc != null) - { - rbc.close(); - } - } catch (IOException e) + } catch (IOException e) + { + System.out.println( + "Exception while closing download file output stream: " + + e.getMessage()); + } + try + { + if (rbc != null) { - System.out.println("Exception while closing download channel: " - + e.getMessage()); + rbc.close(); } - try - { - if (temp != null) - { - Files.deleteIfExists(temp); - } - } catch (IOException e) + } catch (IOException e) + { + System.out.println("Exception while closing download channel: " + + e.getMessage()); + } + try + { + if (temp != null) { - System.out.println("Exception while deleting download temp file: " - + e.getMessage()); + Files.deleteIfExists(temp); } + } catch (IOException e) + { + System.out.println("Exception while deleting download temp file: " + + e.getMessage()); } + } } - public static void download(String urlstring, File tempFile) throws IOException + public static void download(String urlstring, File tempFile) + throws IOException { - if (/** @j2sNative 1?false: */true) { + if (!Platform.setFileBytes(tempFile, urlstring)) + { download(urlstring, tempFile.toString()); } - // JavaScript only - - /** @j2sNative - * tempFile._bytes = swingjs.JSUtil.getFileAsBytes(urlstring); - */ } }