View Javadoc
1   package edu.jiangxin.apktoolbox.utils;
2   
3   import org.apache.logging.log4j.LogManager;
4   import org.apache.logging.log4j.Logger;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.math.BigDecimal;
9   import java.math.BigInteger;
10  
11  public class FileUtils {
12  
13      private static final Logger LOGGER = LogManager.getLogger(FileUtils.class.getSimpleName());
14  
15      public static final long ONE_KB = 1024;
16  
17      public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB);
18  
19      public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI);
20  
21      public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);
22  
23      public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI);
24  
25      public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI);
26  
27      public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI);
28  
29      public static String sizeOfInHumanFormat(final File file) {
30          BigInteger size = BigInteger.valueOf(file.length());
31          String displaySize;
32  
33          if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
34              displaySize = divide(size, ONE_EB_BI) + " EB";
35          } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
36              displaySize = divide(size, ONE_PB_BI) + " PB";
37          } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
38              displaySize = divide(size, ONE_TB_BI) + " TB";
39          } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
40              displaySize = divide(size, ONE_GB_BI) + " GB";
41          } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
42              displaySize = divide(size, ONE_MB_BI) + " MB";
43          } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
44              displaySize = divide(size, ONE_KB_BI) + " KB";
45          } else {
46              displaySize = size + " bytes";
47          }
48          return displaySize;
49      }
50  
51      public static String lastModifiedInHumanFormat(File file) {
52          long lastModified = file.lastModified();
53          return DateUtils.millisecondToHumanFormat(lastModified);
54      }
55  
56      public static double divide(BigInteger size, BigInteger one_bi) {
57          BigDecimal decimalSize = BigDecimal.valueOf(size.doubleValue())
58                  .divide(BigDecimal.valueOf(one_bi.doubleValue()), 2, BigDecimal.ROUND_HALF_UP);
59          return decimalSize.doubleValue();
60      }
61  
62      public static String getCanonicalPathQuiet(File file) {
63          if (file == null) {
64              LOGGER.warn("getCanonicalPathQuiet failed: file is null");
65              return null;
66          }
67          try {
68              return file.getCanonicalPath();
69          } catch (IOException e) {
70              LOGGER.error("getCanonicalPathQuiet failed: IOException");
71              return null;
72          }
73      }
74  }
75  
76