Skip to content

Commit

Permalink
Better dynamic counter
Browse files Browse the repository at this point in the history
  • Loading branch information
IndianBartonka committed Sep 16, 2024
1 parent b58fb18 commit f8dd225
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.indian.util</groupId>
<artifactId>utils</artifactId>
<version>0.0.4.2</version>
<version>0.0.4.2.1</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/me/indian/util/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/me/indian/util/download/DownloadBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
23 changes: 13 additions & 10 deletions src/main/java/me/indian/util/download/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit f8dd225

Please sign in to comment.