X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Futils%2FUrlDownloadClient.java;h=235d271c68b21c5478f4ddaf8c000fb40df8b5bf;hb=7d92d3994908aae709e7c85cc5e1a1c4775907ed;hp=86e3f76b5c3a4550f14a7ba59bd651bd99df3eae;hpb=a1bf90058be8be92554ecbc5868982dcdbed5a80;p=jalview.git diff --git a/src/jalview/ws/utils/UrlDownloadClient.java b/src/jalview/ws/utils/UrlDownloadClient.java index 86e3f76..235d271 100644 --- a/src/jalview/ws/utils/UrlDownloadClient.java +++ b/src/jalview/ws/utils/UrlDownloadClient.java @@ -21,6 +21,9 @@ package jalview.ws.utils; +import jalview.util.Platform; + +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; @@ -33,11 +36,6 @@ import java.nio.file.StandardCopyOption; public class UrlDownloadClient { - public UrlDownloadClient() - { - - } - /** * Download and save a file from a URL * @@ -47,63 +45,74 @@ public class UrlDownloadClient * the name of file to save the URLs to * @throws IOException */ - public void download(String urlstring, String outfile) throws IOException + public static void download(String urlstring, String outfile) + throws IOException { - 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); - - // 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 - { + FileOutputStream fos = null; + ReadableByteChannel rbc = null; + Path temp = null; try { - if (fos != null) - { - fos.close(); - } + 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); + + // copy tempfile to outfile once our download completes + // incase something goes wrong + Files.copy(temp, Paths.get(outfile), + StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - System.out - .println("Exception while closing download file output stream: " - + e.getMessage()); - } - try + throw e; + } finally { - if (rbc != null) + try + { + if (fos != null) + { + fos.close(); + } + } catch (IOException e) { - rbc.close(); + System.out.println( + "Exception while closing download file output stream: " + + e.getMessage()); } - } catch (IOException e) - { - System.out.println("Exception while closing download channel: " - + e.getMessage()); - } - try - { - if (temp != null) + try + { + if (rbc != null) + { + rbc.close(); + } + } catch (IOException e) { - Files.deleteIfExists(temp); + System.out.println("Exception while closing download channel: " + + e.getMessage()); + } + try + { + if (temp != null) + { + Files.deleteIfExists(temp); + } + } catch (IOException e) + { + System.out.println("Exception while deleting download temp file: " + + e.getMessage()); } - } catch (IOException e) - { - System.out.println("Exception while deleting download temp file: " - + e.getMessage()); } + + } + + public static void download(String urlstring, File tempFile) throws IOException + { + if (!Platform.setFileBytes(tempFile, urlstring)) + { + download(urlstring, tempFile.toString()); } } }