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;
*/
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());
+ }
+ }
}
}
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
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
// 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);
+
}