Skip to content

Commit e53dddc

Browse files
FileUtils#byteCountToDisplaySize supports Zettabyte, Yottabyte, Ronnabyte and Quettabyte (#763)
* FileUtils#byteCountToDisplaySize supports Zettabyte, Yottabyte, Ronnabyte and Quettabyte * Update javadoc to reflect all supported units * Add missing Javadoc since tags * Remove bad import --------- Co-authored-by: Gary Gregory <[email protected]>
1 parent 43ff904 commit e53dddc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,27 @@ public class FileUtils {
194194
/**
195195
* The number of bytes in a zettabyte.
196196
*/
197-
public static final BigInteger ONE_ZB = BigInteger.valueOf(ONE_KB).multiply(BigInteger.valueOf(ONE_EB));
197+
public static final BigInteger ONE_ZB = ONE_KB_BI.multiply(ONE_EB_BI);
198198

199199
/**
200200
* The number of bytes in a yottabyte.
201201
*/
202202
public static final BigInteger ONE_YB = ONE_KB_BI.multiply(ONE_ZB);
203203

204+
/**
205+
* The number of bytes in a ronnabyte.
206+
*
207+
* @since 2.21.0
208+
*/
209+
public static final BigInteger ONE_RB = ONE_KB_BI.multiply(ONE_YB);
210+
211+
/**
212+
* The number of bytes in a quettabyte.
213+
*
214+
* @since 2.21.0
215+
*/
216+
public static final BigInteger ONE_QB = ONE_KB_BI.multiply(ONE_RB);
217+
204218
/**
205219
* An empty array of type {@link File}.
206220
*/
@@ -217,7 +231,7 @@ public class FileUtils {
217231
* </p>
218232
*
219233
* @param size the number of bytes
220-
* @return a human-readable display value (includes units - EB, PB, TB, GB, MB, KB or bytes)
234+
* @return a human-readable display value (includes units - QB, RB, YB, ZB, EB, PB, TB, GB, MB, KB or bytes)
221235
* @throws NullPointerException if the given {@link BigInteger} is {@code null}.
222236
* @see <a href="https://issues.apache.org/jira/browse/IO-226">IO-226 - should the rounding be changed?</a>
223237
* @since 2.4
@@ -227,7 +241,15 @@ public static String byteCountToDisplaySize(final BigInteger size) {
227241
Objects.requireNonNull(size, "size");
228242
final String displaySize;
229243

230-
if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
244+
if (size.divide(ONE_QB).compareTo(BigInteger.ZERO) > 0) {
245+
displaySize = size.divide(ONE_QB) + " QB";
246+
} else if (size.divide(ONE_RB).compareTo(BigInteger.ZERO) > 0) {
247+
displaySize = size.divide(ONE_RB) + " RB";
248+
} else if (size.divide(ONE_YB).compareTo(BigInteger.ZERO) > 0) {
249+
displaySize = size.divide(ONE_YB) + " YB";
250+
} else if (size.divide(ONE_ZB).compareTo(BigInteger.ZERO) > 0) {
251+
displaySize = size.divide(ONE_ZB) + " ZB";
252+
} else if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
231253
displaySize = size.divide(ONE_EB_BI) + " EB";
232254
} else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
233255
displaySize = size.divide(ONE_PB_BI) + " PB";

src/test/java/org/apache/commons/io/FileUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ void testByteCountToDisplaySizeBigInteger() {
370370
final BigInteger TB1 = GB1.multiply(KB1);
371371
final BigInteger PB1 = TB1.multiply(KB1);
372372
final BigInteger EB1 = PB1.multiply(KB1);
373+
final BigInteger ZB1 = EB1.multiply(KB1);
374+
final BigInteger YB1 = ZB1.multiply(KB1);
375+
final BigInteger RB1 = YB1.multiply(KB1);
376+
final BigInteger QB1 = RB1.multiply(KB1);
373377
assertEquals("0 bytes", FileUtils.byteCountToDisplaySize(BigInteger.ZERO));
374378
assertEquals("1 bytes", FileUtils.byteCountToDisplaySize(BigInteger.ONE));
375379
assertEquals("1023 bytes", FileUtils.byteCountToDisplaySize(b1023));
@@ -386,6 +390,10 @@ void testByteCountToDisplaySizeBigInteger() {
386390
assertEquals("1 TB", FileUtils.byteCountToDisplaySize(TB1));
387391
assertEquals("1 PB", FileUtils.byteCountToDisplaySize(PB1));
388392
assertEquals("1 EB", FileUtils.byteCountToDisplaySize(EB1));
393+
assertEquals("1 ZB", FileUtils.byteCountToDisplaySize(ZB1));
394+
assertEquals("1 YB", FileUtils.byteCountToDisplaySize(YB1));
395+
assertEquals("1 RB", FileUtils.byteCountToDisplaySize(RB1));
396+
assertEquals("1 QB", FileUtils.byteCountToDisplaySize(QB1));
389397
assertEquals("7 EB", FileUtils.byteCountToDisplaySize(Long.MAX_VALUE));
390398
// Other MAX_VALUEs
391399
assertEquals("63 KB", FileUtils.byteCountToDisplaySize(BigInteger.valueOf(Character.MAX_VALUE)));

0 commit comments

Comments
 (0)