Skip to content

Commit

Permalink
Print converted size instead of percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
vimasig committed Feb 19, 2021
1 parent 5ecf22c commit cea7522
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.vimasig</groupId>
<artifactId>Bozar</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/vimasig/bozar/obfuscator/Bozar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vimasig.bozar.obfuscator.transformer.TransformManager;
import com.vimasig.bozar.obfuscator.utils.StreamUtils;
import com.vimasig.bozar.obfuscator.utils.StringUtils;
import com.vimasig.bozar.obfuscator.utils.model.BozarConfig;
import com.vimasig.bozar.obfuscator.utils.model.BozarMessage;
import com.vimasig.bozar.obfuscator.utils.model.CustomClassWriter;
Expand Down Expand Up @@ -124,12 +125,9 @@ public void run() {
log("Done. Took %ss", timeElapsed);

// File size information
float rate = (float)output.toFile().length() / (float)input.length();
float percentage = rate * 100 - 100;
final String percentageStr = new DecimalFormat("##.##").format(Math.abs(percentage));
if(percentage == 0) log("File size didn't change.");
else if(percentage > 0) log("File size increased by %s%%", percentageStr);
else log("File size decreased by %s%%", percentageStr);
final String oldSize = StringUtils.getConvertedSize(input.length());
final String newSize = StringUtils.getConvertedSize(output.toFile().length());
log("File size changed from %s to %s", oldSize, newSize);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/vimasig/bozar/obfuscator/utils/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.vimasig.bozar.obfuscator.utils;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -12,4 +14,19 @@ public static String getAlphabet() {
.map(String::valueOf)
.collect(Collectors.joining());
}

public static String getConvertedSize(long bytes) {
long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
if (absB < 1024) {
return bytes + " B";
}
long value = absB;
CharacterIterator ci = new StringCharacterIterator("KMGTPE");
for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
value >>= 10;
ci.next();
}
value *= Long.signum(bytes);
return String.format("%.1f %cB", value / 1024.0, ci.current());
}
}

0 comments on commit cea7522

Please sign in to comment.