From: BobHanson Date: Fri, 5 Jun 2020 21:46:09 +0000 (-0500) Subject: JAL-3446 UrlDownloadClient and Test X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=45dbf681b7894072a9e3f5b6d55bf21f4e02599e;p=jalview.git JAL-3446 UrlDownloadClient and Test - moving download to Platform - trivializing JavaScript method - expanding SwingJS to allow for nio methods Note: SwingJS preserves byte[] in the File, Path, or FileOutputStream objects as well as in J2S._javaFileCache. Because of that, one thing you have to look out for is stale references. In this case, we have: temp = new Path(...); fos = new FileOutputStream(temp.toString); now two independent pointers both refer to the tmp file. rbc = new ReadableByteChannel(url.openStream()); fos.getChannel().transferFrom(rbc); Now the bytes are in fos's channel, but they are not associated yet with "tmp". Files.copy(tmp, outfile); This wasn't working in JavaScript, because Path tmp was out of date, and so no bytes were being copied. Working now, but still far to complicated for the simple operation of transferring bytes from an InputStream to a File in JavaScript. --- diff --git a/src/jalview/util/Platform.java b/src/jalview/util/Platform.java index c89f1e7..0a6a5c9 100644 --- a/src/jalview/util/Platform.java +++ b/src/jalview/util/Platform.java @@ -33,8 +33,12 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.util.Date; import java.util.Locale; @@ -950,7 +954,54 @@ public class Platform */ public static Locale getLocaleOrNone(Locale loc) { - return (isJS && loc.getCountry() == "en" ? new Locale("") : loc); + return (isJS && loc.getLanguage() == "en" ? new Locale("") : loc); + } + + /** + * From UrlDownloadClient; trivial in JavaScript; painful in Java. + * + * @param urlstring + * @param outfile + * @throws IOException + */ + public static void download(String urlstring, String outfile) + throws IOException + { + Path temp = null; + try (InputStream is = new URL(urlstring).openStream()) + { + if (isJS) + { // so much easier! + streamToFile(is, new File(outfile)); + return; + } + temp = Files.createTempFile(".jalview_", ".tmp"); + try (FileOutputStream fos = new FileOutputStream(temp.toString()); + ReadableByteChannel rbc = Channels.newChannel(is)) + { + 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 + { + try + { + if (temp != null) + { + Files.deleteIfExists(temp); + } + } catch (IOException e) + { + System.out.println("Exception while deleting download temp file: " + + e.getMessage()); + } + } } } diff --git a/src/jalview/ws/utils/UrlDownloadClient.java b/src/jalview/ws/utils/UrlDownloadClient.java index 58632f2..227fe23 100644 --- a/src/jalview/ws/utils/UrlDownloadClient.java +++ b/src/jalview/ws/utils/UrlDownloadClient.java @@ -53,64 +53,7 @@ public class UrlDownloadClient 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 - { - try - { - if (fos != null) - { - fos.close(); - } - } catch (IOException e) - { - System.out.println( - "Exception while closing download file output stream: " - + e.getMessage()); - } - try - { - if (rbc != null) - { - rbc.close(); - } - } catch (IOException e) - { - 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()); - } - } - + Platform.download(urlstring, outfile); } public static void download(String urlstring, File tempFile) throws IOException diff --git a/test/jalview/ws/utils/UrlDownloadClientTest.java b/test/jalview/ws/utils/UrlDownloadClientTest.java index 2bd8dd0..fe70a98 100644 --- a/test/jalview/ws/utils/UrlDownloadClientTest.java +++ b/test/jalview/ws/utils/UrlDownloadClientTest.java @@ -35,7 +35,8 @@ public class UrlDownloadClientTest { public void UrlDownloadTest() { UrlDownloadClient client = new UrlDownloadClient(); - String urlstring = "http://identifiers.org/rest/collections/"; + String urlstring = "http://www.jalview.org/services/identifiers"; + // was "http://identifiers.org/rest/collections/"; String outfile = "testfile.tmp"; try @@ -58,12 +59,14 @@ public class UrlDownloadClientTest { // download file has a believable size // identifiers.org file typically at least 250K - Assert.assertTrue(f.length() > 250000); - + long n = f.length(); if (f.exists()) { f.delete(); } + // 74589 + Assert.assertTrue(n > 70000); + }