From e046031dd2de85fd46fab5da0e69bd3c01733150 Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Dec 2018 15:29:13 +0100 Subject: [PATCH] Fix downloading certain files to a byte array. --- .../wiiu/jnus/utils/download/Downloader.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/de/mas/wiiu/jnus/utils/download/Downloader.java b/src/de/mas/wiiu/jnus/utils/download/Downloader.java index 8b6a93f..ae35e96 100644 --- a/src/de/mas/wiiu/jnus/utils/download/Downloader.java +++ b/src/de/mas/wiiu/jnus/utils/download/Downloader.java @@ -16,6 +16,7 @@ ****************************************************************************/ package de.mas.wiiu.jnus.utils.download; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; @@ -33,31 +34,29 @@ public abstract class Downloader { public static byte[] downloadFileToByteArray(String fileURL) throws IOException { int BUFFER_SIZE = 0x800; URL url = new URL(fileURL); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestProperty("User-Agent", Settings.USER_AGENT); int responseCode = httpConn.getResponseCode(); - byte[] file = null; + ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); if (responseCode == HttpURLConnection.HTTP_OK) { - int contentLength = httpConn.getContentLength(); - - file = new byte[contentLength]; InputStream inputStream = httpConn.getInputStream(); int bytesRead = -1; byte[] buffer = new byte[BUFFER_SIZE]; - int filePostion = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { - System.arraycopy(buffer, 0, file, filePostion, bytesRead); - filePostion += bytesRead; + byteArray.write(buffer, 0, bytesRead); } inputStream.close(); } else { - log.info("File not found: " + fileURL); + log.fine("File not found: " + fileURL); } httpConn.disconnect(); - return file; + byte[] result = byteArray.toByteArray(); + byteArray.close(); + return result; } }