Skip to content

Commit

Permalink
🪲 Fix issue #9 - When the size is over 1GB, don't round down to the n…
Browse files Browse the repository at this point in the history
…earest GB boundary

- Use the International System of Units (SI) to compute file size (1000 instead of 1024)
  • Loading branch information
evrignaud committed Mar 30, 2017
1 parent 8d63426 commit bbf9462
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/main/java/org/fim/internal/hash/HashProgress.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.fim.model.Constants;
import org.fim.model.Context;
import org.fim.util.Logger;

Expand All @@ -36,11 +35,11 @@ public class HashProgress {

private static final List<Pair<Character, Integer>> progressChars = Arrays.asList(
Pair.of('.', 0),
Pair.of('o', Constants._20_MB),
Pair.of('8', Constants._50_MB),
Pair.of('O', Constants._100_MB),
Pair.of('@', Constants._200_MB),
Pair.of('#', Constants._1_GB)
Pair.of('o', 20_000_000),
Pair.of('8', 50_000_000),
Pair.of('O', 100_000_000),
Pair.of('@', 200_000_000),
Pair.of('#', 1000_000_000)
);

private final Context context;
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/fim/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
*/
package org.fim.util;

import org.apache.commons.io.FileUtils;
import org.fim.model.Context;
import org.fim.model.FileState;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.DecimalFormat;

public class FileUtil {
static DecimalFormat decimalFormat = new DecimalFormat("0.#");

public static String getNormalizedFileName(Path file) {
String normalizedFileName = file.toAbsolutePath().normalize().toString();
if (File.separatorChar != '/') {
Expand Down Expand Up @@ -70,10 +72,23 @@ public static String byteCountToDisplaySize(final long size) {
isNegative = true;
}

String displaySize = FileUtils.byteCountToDisplaySize(localSize);
String displaySize = humanReadableByteCount(localSize, true);
if (isNegative) {
displaySize = "-" + displaySize;
}
return displaySize;
}

/**
* Original code comes from:
* http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
*/
private static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " bytes";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
return decimalFormat.format(bytes / Math.pow(unit, exp)) + " " + "KMGTPE".charAt(exp - 1) + (si ? "B" : "bit");
}
}

0 comments on commit bbf9462

Please sign in to comment.