From 4e06a3b0ff9389ef75ee4e407ce4262e1b050ffc Mon Sep 17 00:00:00 2001 From: Roman / Nea Date: Sat, 18 Dec 2021 12:27:12 +0100 Subject: Load wiki pages asynchronously. (#30) Previous to this commit, wiki pages would be downloaded on the main rendering thread, preventing all rendering and user interaction while the download was occuring. This has now been shifted to the common forkpool provided by the JVM. --- .../moulberry/notenoughupdates/NEUManager.java | 49 ++++++++++++---------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java index 01e97c75..b129f133 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java @@ -29,6 +29,7 @@ import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.zip.ZipEntry; @@ -1024,32 +1025,34 @@ public class NEUManager { /** * Downloads a web file, appending some HTML attributes that makes wikia give us the raw wiki syntax. */ - public File getWebFile(String url) { - File f = new File(configLocation, "tmp/" + Base64.getEncoder().encodeToString(url.getBytes()) + ".html"); - if (f.exists()) { - return f; - } + public CompletableFuture getWebFile(String url) { + return CompletableFuture.supplyAsync(() -> { + File f = new File(configLocation, "tmp/" + Base64.getEncoder().encodeToString(url.getBytes()) + ".html"); + if (f.exists()) { + return f; + } - try { - f.getParentFile().mkdirs(); - f.createNewFile(); - f.deleteOnExit(); - } catch (IOException e) { - return null; - } - try (BufferedInputStream inStream = new BufferedInputStream(new URL(url + "?action=raw&templates=expand").openStream()); - FileOutputStream fileOutputStream = new FileOutputStream(f)) { - byte[] dataBuffer = new byte[1024]; - int bytesRead; - while ((bytesRead = inStream.read(dataBuffer, 0, 1024)) != -1) { - fileOutputStream.write(dataBuffer, 0, bytesRead); + try { + f.getParentFile().mkdirs(); + f.createNewFile(); + f.deleteOnExit(); + } catch (IOException e) { + return null; + } + try (BufferedInputStream inStream = new BufferedInputStream(new URL(url + "?action=raw&templates=expand").openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(f)) { + byte[] dataBuffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inStream.read(dataBuffer, 0, 1024)) != -1) { + fileOutputStream.write(dataBuffer, 0, bytesRead); + } + } catch (IOException e) { + e.printStackTrace(); + return null; } - } catch (IOException e) { - e.printStackTrace(); - return null; - } - return f; + return f; + }); } /** -- cgit