aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2021-06-28 10:10:21 -0500
committersonartech <sonartech@sonarsource.com>2021-06-30 20:03:13 +0000
commit551db520fa153e6654e9d39bde8404aeae14f9d3 (patch)
tree80e870a75ec74fc490eebcdcbed32ca7f2839cd2 /sonar-core
parent2b1e236fa390e079bb4277b93b2dc9be25a77c5b (diff)
downloadsonarqube-551db520fa153e6654e9d39bde8404aeae14f9d3.tar.gz
sonarqube-551db520fa153e6654e9d39bde8404aeae14f9d3.zip
SONAR-14917 Misleading memory amounts displayed in Administration > System
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/FileUtils.java22
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java25
2 files changed, 47 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/FileUtils.java b/sonar-core/src/main/java/org/sonar/core/util/FileUtils.java
index 109e6769ce9..ad058a6f45b 100644
--- a/sonar-core/src/main/java/org/sonar/core/util/FileUtils.java
+++ b/sonar-core/src/main/java/org/sonar/core/util/FileUtils.java
@@ -27,6 +27,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
import java.util.EnumSet;
import javax.annotation.Nullable;
@@ -162,6 +164,26 @@ public final class FileUtils {
}
/**
+ * Replaces the use of Apache Commons FileUtils #byteCountToDisplaySize (see SONAR-14917)
+ * @param bytes number of bytes
+ * @return human readable byte count, with 1 decimal place
+ */
+ public static String humanReadableByteCountSI(long bytes) {
+ if (-1000 < bytes && bytes < 1000) {
+ if (bytes == 1) {
+ return bytes + " byte";
+ }
+ return bytes + " bytes";
+ }
+ CharacterIterator ci = new StringCharacterIterator("kMGTPE");
+ while (bytes <= -999_950 || bytes >= 999_950) {
+ bytes /= 1000;
+ ci.next();
+ }
+ return String.format("%.1f %cB", bytes / 1000.0, ci.current());
+ }
+
+ /**
* This visitor is intended to be used to visit direct children of directory <strong>or a symLink to a directory</strong>,
* so, with a max depth of {@link #VISIT_MAX_DEPTH 1}. Each direct children will either be directly deleted (if file)
* or recursively deleted (if directory).
diff --git a/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
index 89eefb5b4a9..de333333ccb 100644
--- a/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/util/FileUtilsTest.java
@@ -160,6 +160,31 @@ public class FileUtilsTest {
}
@Test
+ public void humanReadableByteCountSI_returns_bytes() {
+ assertThat(FileUtils.humanReadableByteCountSI(123)).isEqualTo("123 bytes");
+ assertThat(FileUtils.humanReadableByteCountSI(0)).isEqualTo("0 bytes");
+ assertThat(FileUtils.humanReadableByteCountSI(1)).isEqualTo("1 byte");
+ }
+
+ @Test
+ public void humanReadableByteCountSI_returns_kbs() {
+ assertThat(FileUtils.humanReadableByteCountSI(1_234)).isEqualTo("1.2 kB");
+ assertThat(FileUtils.humanReadableByteCountSI(1_000)).isEqualTo("1.0 kB");
+ assertThat(FileUtils.humanReadableByteCountSI(9_999)).isEqualTo("10.0 kB");
+ assertThat(FileUtils.humanReadableByteCountSI(999_949)).isEqualTo("999.9 kB");
+ }
+
+ @Test
+ public void humanReadableByteCountSI_returns_tbs() {
+ assertThat(FileUtils.humanReadableByteCountSI(1_234_000_000_000L)).isEqualTo("1.2 TB");
+ }
+
+ @Test
+ public void humanReadableByteCountSI_returns_mbs() {
+ assertThat(FileUtils.humanReadableByteCountSI(1234567)).isEqualTo("1.2 MB");
+ }
+
+ @Test
public void deleteQuietly_deletes_symbolicLink() throws IOException {
assumeTrue(SystemUtils.IS_OS_UNIX);
Path folder = temporaryFolder.newFolder().toPath();