Skip to content

Commit

Permalink
Merge pull request #1118 from eirslett/feature/download-file-fully-be…
Browse files Browse the repository at this point in the history
…fore-write

Download files fully before write to disk
  • Loading branch information
eirslett committed Oct 25, 2023
2 parents 49e11d1 + 5d2d990 commit e2d0445
Showing 1 changed file with 4 additions and 7 deletions.
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e2d0445

Please sign in to comment.