From 5d2d990f88d89f9409e784b9369c28b0b5d7506c Mon Sep 17 00:00:00 2001 From: Eirik Sletteberg Date: Thu, 26 Oct 2023 00:24:07 +0200 Subject: [PATCH] Download files fully before write to disk --- .../maven/plugins/frontend/lib/FileDownloader.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FileDownloader.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FileDownloader.java index 3530050a0..2d5244a62 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FileDownloader.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/FileDownloader.java @@ -1,14 +1,12 @@ package com.github.eirslett.maven.plugins.frontend.lib; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.http.HttpHost; @@ -71,11 +69,10 @@ public void download(String downloadUrl, String destination, String userName, St if(statusCode != 200){ throw new DownloadException("Got error code "+ statusCode +" from the server."); } + + byte[] data = IOUtils.toByteArray(response.getEntity().getContent()); new File(FilenameUtils.getFullPathNoEndSeparator(destination)).mkdirs(); - ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent()); - FileOutputStream fos = new FileOutputStream(destination); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); + FileUtils.writeByteArrayToFile(new File(destination), data); } } catch (IOException | URISyntaxException e) { throw new DownloadException("Could not download " + fixedDownloadUrl, e);