diff --git a/src/main/java/net/fabricmc/loom/util/download/DownloadBuilder.java b/src/main/java/net/fabricmc/loom/util/download/DownloadBuilder.java index 95d46623..15242936 100644 --- a/src/main/java/net/fabricmc/loom/util/download/DownloadBuilder.java +++ b/src/main/java/net/fabricmc/loom/util/download/DownloadBuilder.java @@ -114,22 +114,21 @@ public class DownloadBuilder { } public String downloadString(Path cache) throws DownloadException { - withRetries(() -> { + return withRetries(() -> { build().downloadPath(cache); - return null; - }); - try { - return Files.readString(cache, StandardCharsets.UTF_8); - } catch (IOException e) { try { - Files.delete(cache); - } catch (IOException ex) { - // Ignored - } + return Files.readString(cache, StandardCharsets.UTF_8); + } catch (IOException e) { + try { + Files.deleteIfExists(cache); + } catch (IOException ex) { + // Ignored + } - throw new DownloadException("Failed to download and read string", e); - } + throw new DownloadException("Failed to download and read string", e); + } + }); } private T withRetries(DownloadSupplier supplier) throws DownloadException { diff --git a/src/test/groovy/net/fabricmc/loom/test/unit/download/DownloadStringTest.groovy b/src/test/groovy/net/fabricmc/loom/test/unit/download/DownloadStringTest.groovy index 66f58dd6..d9ccdd3b 100644 --- a/src/test/groovy/net/fabricmc/loom/test/unit/download/DownloadStringTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/unit/download/DownloadStringTest.groovy @@ -95,4 +95,18 @@ class DownloadStringTest extends DownloadTest { then: result == "Hello World 3" } + + def "String: File cache"() { + setup: + server.get("/downloadString2") { + it.result("Hello World!") + } + + when: + def output = new File(File.createTempDir(), "file.txt").toPath() + def result = Download.create("$PATH/downloadString2").downloadString(output) + + then: + result == "Hello World!" + } }