diff options
| author | Roman / Nea <roman.graef@gmail.com> | 2021-12-18 12:27:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-18 12:27:12 +0100 |
| commit | 4e06a3b0ff9389ef75ee4e407ce4262e1b050ffc (patch) | |
| tree | 8fad6665dbba933a75de2e1e81e3fdb36172a465 /src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java | |
| parent | aca006bb4d379b6afe79357f24957f035184636e (diff) | |
| download | notenoughupdates-4e06a3b0ff9389ef75ee4e407ce4262e1b050ffc.tar.gz notenoughupdates-4e06a3b0ff9389ef75ee4e407ce4262e1b050ffc.tar.bz2 notenoughupdates-4e06a3b0ff9389ef75ee4e407ce4262e1b050ffc.zip | |
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.
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java | 49 |
1 files changed, 26 insertions, 23 deletions
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<File> 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; + }); } /** |
