|
25 | 25 | package com.oracle.svm.hosted; |
26 | 26 |
|
27 | 27 | public class ByteFormattingUtil { |
28 | | - private static final double BYTES_TO_KB = 1000d; |
29 | | - private static final double BYTES_TO_MB = 1000d * 1000d; |
30 | | - private static final double BYTES_TO_GB = 1000d * 1000d * 1000d; |
| 28 | + private static final long KiB_TO_BYTES = 1024L; |
| 29 | + private static final long MiB_TO_BYTES = 1024L * KiB_TO_BYTES; |
| 30 | + private static final long GiB_TO_BYTES = 1024L * MiB_TO_BYTES; |
31 | 31 |
|
32 | 32 | public static String bytesToHuman(long bytes) { |
33 | 33 | assert bytes >= 0; |
34 | | - if (bytes < BYTES_TO_KB) { |
35 | | - return plainBytes(bytes, "B"); |
36 | | - } else if (bytes < BYTES_TO_MB) { |
37 | | - return toHuman(bytes / BYTES_TO_KB, "kB"); |
38 | | - } else if (bytes < BYTES_TO_GB) { |
39 | | - return toHuman(bytes / BYTES_TO_MB, "MB"); |
| 34 | + if (bytes < KiB_TO_BYTES) { |
| 35 | + return bytes + "B"; |
| 36 | + } else if (bytes < MiB_TO_BYTES) { |
| 37 | + return toHuman((double) bytes / KiB_TO_BYTES, "KiB"); |
| 38 | + } else if (bytes < GiB_TO_BYTES) { |
| 39 | + return toHuman((double) bytes / MiB_TO_BYTES, "MiB"); |
40 | 40 | } else { |
41 | 41 | return bytesToHumanGB(bytes); |
42 | 42 | } |
43 | 43 | } |
44 | 44 |
|
45 | 45 | public static String bytesToHumanGB(long bytes) { |
46 | | - return toHuman(bytes / BYTES_TO_GB, "GB"); |
| 46 | + return toHuman((double) bytes / GiB_TO_BYTES, "GiB"); |
47 | 47 | } |
48 | 48 |
|
49 | 49 | private static String toHuman(double value, String unit) { |
50 | 50 | return "%.2f%s".formatted(value, unit); |
51 | 51 | } |
52 | 52 |
|
53 | | - private static String plainBytes(long value, String unit) { |
54 | | - assert 0 <= value && value < BYTES_TO_KB; |
55 | | - return "%d%s".formatted(value, unit); |
56 | | - } |
57 | 53 | } |
0 commit comments