diff --git a/pom.xml b/pom.xml index 862edde..0f65e90 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.indian.util utils - 0.0.4.2 + 0.0.4.2.1 17 diff --git a/src/main/java/me/indian/util/Main.java b/src/main/java/me/indian/util/Main.java index 454d086..d257930 100644 --- a/src/main/java/me/indian/util/Main.java +++ b/src/main/java/me/indian/util/Main.java @@ -97,9 +97,10 @@ public static void downloadFileTest() throws IOException { final DownloadListener downloadListener = new DownloadListener() { @Override - public void onStart(final int definedBuffer, final File outputFile) { + public void onStart(final DownloadBuffer downloadBuffer, final int definedBuffer, final File outputFile) { LOGGER.info("Pobieranie:&a " + outputFile.getName()); LOGGER.info("Ustalony buffer dla naszego pliku to:&a " + DownloadBuffer.defineBuffer(definedBuffer)); + LOGGER.info("Domyślny buffer to:&a " + downloadBuffer); } @Override @@ -113,7 +114,7 @@ public void onProgress(final int progress, final double formatedSpeed, final Str } @Override - public void onTimeout(int timeOutSeconds) { + public void onTimeout(final int timeOutSeconds) { LOGGER.info("TimeOut"); } diff --git a/src/main/java/me/indian/util/download/DownloadBuffer.java b/src/main/java/me/indian/util/download/DownloadBuffer.java index 34d1069..0e61ce6 100644 --- a/src/main/java/me/indian/util/download/DownloadBuffer.java +++ b/src/main/java/me/indian/util/download/DownloadBuffer.java @@ -31,10 +31,19 @@ public int getBuffer() { } public static DownloadBuffer defineBuffer(final int buffer) { - for (final DownloadBuffer downloadBuffer : values()) { - if (downloadBuffer.getBuffer() == buffer) return downloadBuffer; + DownloadBuffer closestBuffer = DownloadBuffer.DYNAMIC; + int minDifference = Integer.MAX_VALUE; + + for (final DownloadBuffer downloadBuffer : DownloadBuffer.values()) { + final int currentBufferSize = downloadBuffer.getBuffer(); + final int difference = Math.abs(currentBufferSize - buffer); + + if (difference < minDifference) { + minDifference = difference; + closestBuffer = downloadBuffer; + } } - return DYNAMIC; + return closestBuffer; } } \ No newline at end of file diff --git a/src/main/java/me/indian/util/download/DownloadListener.java b/src/main/java/me/indian/util/download/DownloadListener.java index f3c0b11..f7391d5 100644 --- a/src/main/java/me/indian/util/download/DownloadListener.java +++ b/src/main/java/me/indian/util/download/DownloadListener.java @@ -4,7 +4,7 @@ public interface DownloadListener { - void onStart(int definedBuffer, File outputFile); + void onStart(DownloadBuffer downloadBuffer, int definedBuffer, File outputFile); void onSecond(int progress, double formatedSpeed, String remainingTimeString); diff --git a/src/main/java/me/indian/util/download/Downloader.java b/src/main/java/me/indian/util/download/Downloader.java index 5b44611..4d8c841 100644 --- a/src/main/java/me/indian/util/download/Downloader.java +++ b/src/main/java/me/indian/util/download/Downloader.java @@ -24,10 +24,10 @@ public static void downloadFile(final InputStream inputStream, final File output long lastActivityTime; try (final FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) { - final int definedBuffer = defineBuffer(downloadBuffer); + final int definedBuffer = defineBuffer(downloadBuffer, fileSize); final byte[] buffer = new byte[definedBuffer]; - if (downloadListener != null) downloadListener.onStart(definedBuffer, outputFile); + if (downloadListener != null) downloadListener.onStart(downloadBuffer, definedBuffer, outputFile); int bytesRead; long totalBytesRead = 0; @@ -79,21 +79,24 @@ public static void downloadFile(final InputStream inputStream, final File output } } - private static int defineBuffer(final DownloadBuffer downloadBuffer) { - if (downloadBuffer == DownloadBuffer.DYNAMIC) return calculateOptimalBufferSize(); + private static int defineBuffer(final DownloadBuffer downloadBuffer, final long fileSize) { + if (downloadBuffer == DownloadBuffer.DYNAMIC) return calculateOptimalBufferSize(fileSize); return downloadBuffer.getBuffer(); } - private static int calculateOptimalBufferSize() { + private static int calculateOptimalBufferSize(final long fileSize) { + final int maxBufferSize = DownloadBuffer.SIXTEEN_MB.getBuffer(); + final long calculatedBufferSize = (long) (fileSize * 0.1); final long bufferPerRequest = ((OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getFreeMemorySize() / 5; + final long bufferSize = Math.min(calculatedBufferSize, bufferPerRequest); - if (bufferPerRequest > DownloadBuffer.SIXTEEN_MB.getBuffer()) { - return DownloadBuffer.SIXTEEN_MB.getBuffer(); - } else if (bufferPerRequest < DownloadBuffer.ONE_MB.getBuffer()) { + if (bufferSize > maxBufferSize) { + return maxBufferSize; + } else if (bufferSize < DownloadBuffer.ONE_MB.getBuffer()) { return DownloadBuffer.ONE_MB.getBuffer(); } - return (int) bufferPerRequest; + return (int) bufferSize; } -} +} \ No newline at end of file