1
2
3
4
5 package oshi.util;
6
7 import java.math.BigInteger;
8 import java.util.Locale;
9 import java.util.concurrent.TimeUnit;
10
11 import oshi.annotation.concurrent.ThreadSafe;
12
13
14
15
16 @ThreadSafe
17 public final class FormatUtil {
18
19
20
21
22
23
24 private static final long KIBI = 1L << 10;
25 private static final long MEBI = 1L << 20;
26 private static final long GIBI = 1L << 30;
27 private static final long TEBI = 1L << 40;
28 private static final long PEBI = 1L << 50;
29 private static final long EXBI = 1L << 60;
30
31
32
33
34 private static final long KILO = 1_000L;
35 private static final long MEGA = 1_000_000L;
36 private static final long GIGA = 1_000_000_000L;
37 private static final long TERA = 1_000_000_000_000L;
38 private static final long PETA = 1_000_000_000_000_000L;
39 private static final long EXA = 1_000_000_000_000_000_000L;
40
41
42
43
44 private static final BigInteger TWOS_COMPLEMENT_REF = BigInteger.ONE.shiftLeft(64);
45
46
47 public static final String HEX_ERROR = "0x%08X";
48
49 private FormatUtil() {
50 }
51
52
53
54
55
56
57
58
59
60 public static String formatBytes(long bytes) {
61 if (bytes == 1L) {
62 return String.format(Locale.ROOT, "%d byte", bytes);
63 } else if (bytes < KIBI) {
64 return String.format(Locale.ROOT, "%d bytes", bytes);
65 } else if (bytes < MEBI) {
66 return formatUnits(bytes, KIBI, "KiB");
67 } else if (bytes < GIBI) {
68 return formatUnits(bytes, MEBI, "MiB");
69 } else if (bytes < TEBI) {
70 return formatUnits(bytes, GIBI, "GiB");
71 } else if (bytes < PEBI) {
72 return formatUnits(bytes, TEBI, "TiB");
73 } else if (bytes < EXBI) {
74 return formatUnits(bytes, PEBI, "PiB");
75 } else {
76 return formatUnits(bytes, EXBI, "EiB");
77 }
78 }
79
80
81
82
83
84
85
86
87
88 private static String formatUnits(long value, long prefix, String unit) {
89 if (value % prefix == 0) {
90 return String.format(Locale.ROOT, "%d %s", value / prefix, unit);
91 }
92 return String.format(Locale.ROOT, "%.1f %s", (double) value / prefix, unit);
93 }
94
95
96
97
98
99
100
101
102 public static String formatBytesDecimal(long bytes) {
103 if (bytes == 1L) {
104 return String.format(Locale.ROOT, "%d byte", bytes);
105 } else if (bytes < KILO) {
106 return String.format(Locale.ROOT, "%d bytes", bytes);
107 } else {
108 return formatValue(bytes, "B");
109 }
110 }
111
112
113
114
115
116
117
118 public static String formatHertz(long hertz) {
119 return formatValue(hertz, "Hz");
120 }
121
122
123
124
125
126
127
128
129 public static String formatValue(long value, String unit) {
130 if (value < KILO) {
131 return String.format(Locale.ROOT, "%d %s", value, unit).trim();
132 } else if (value < MEGA) {
133 return formatUnits(value, KILO, "K" + unit);
134 } else if (value < GIGA) {
135 return formatUnits(value, MEGA, "M" + unit);
136 } else if (value < TERA) {
137 return formatUnits(value, GIGA, "G" + unit);
138 } else if (value < PETA) {
139 return formatUnits(value, TERA, "T" + unit);
140 } else if (value < EXA) {
141 return formatUnits(value, PETA, "P" + unit);
142 } else {
143 return formatUnits(value, EXA, "E" + unit);
144 }
145 }
146
147
148
149
150
151
152
153 public static String formatElapsedSecs(long secs) {
154 long eTime = secs;
155 final long days = TimeUnit.SECONDS.toDays(eTime);
156 eTime -= TimeUnit.DAYS.toSeconds(days);
157 final long hr = TimeUnit.SECONDS.toHours(eTime);
158 eTime -= TimeUnit.HOURS.toSeconds(hr);
159 final long min = TimeUnit.SECONDS.toMinutes(eTime);
160 eTime -= TimeUnit.MINUTES.toSeconds(min);
161 final long sec = eTime;
162 return String.format(Locale.ROOT, "%d days, %02d:%02d:%02d", days, hr, min, sec);
163 }
164
165
166
167
168
169
170
171 public static long getUnsignedInt(int x) {
172 return x & 0x0000_0000_ffff_ffffL;
173 }
174
175
176
177
178
179
180
181
182
183 public static String toUnsignedString(int i) {
184 if (i >= 0) {
185 return Integer.toString(i);
186 }
187 return Long.toString(getUnsignedInt(i));
188 }
189
190
191
192
193
194
195
196
197
198 public static String toUnsignedString(long l) {
199 if (l >= 0) {
200 return Long.toString(l);
201 }
202 return BigInteger.valueOf(l).add(TWOS_COMPLEMENT_REF).toString();
203 }
204
205
206
207
208
209
210
211 public static String formatError(int errorCode) {
212 return String.format(Locale.ROOT, HEX_ERROR, errorCode);
213 }
214
215
216
217
218
219
220
221 public static int roundToInt(double x) {
222 return (int) Math.round(x);
223 }
224 }