Stat implements Serializable, Cloneable { public static String sizeToString(long bytes) { if (bytes==0) { return "0"; } int orderOfMagnitude = (int)Math.floor(Math.log(bytes) / Math.log(1024)); double significantSize = (double)bytes / (1L << orderOfMagnitude*10); DecimalFormat sizeFormat = new DecimalFormat("#.#"); return sizeFormat.format(significantSize)+SIZE_UNITS[orderOfMagnitude]; }  int getDev(); void setDev(int dev); int getIno(); void setIno(int ino); int getMode(); void setMode(int mode); int getNlink(); void setNlink(int nlink); int getUid(); void setUid(int owner); int getGid(); void setGid(int group); int getRdev(); void setRdev(int rdev); long getSize(); void setSize(long size); long getATime(); void setATime(long atime); long getMTime(); void setMTime(long mtime); long getCTime(); void setCTime(long ctime); long getFileId(); void setFileid(long fileid); long getGeneration(); void setGeneration(long generation); Type type(); static String modeToString(int mode); static String sizeToString(long bytes); @Override Stat clone(); @Override String toString(); boolean isDefined(StatAttribute attr); void undefine(StatAttribute attr); static final int S_TYPE; static final int S_PERMS; static final int S_IFSOCK; static final int S_IFLNK; static final int S_IFREG; static final int S_IFBLK; static final int S_IFDIR; static final int S_IFCHR; static final int S_IFIFO; }
@Test public void testSizeToString() { assertEquals("0",Stat.sizeToString(0)); assertEquals("1023",Stat.sizeToString(1024-1)); assertEquals("1K",Stat.sizeToString(1024)); assertEquals("1K",Stat.sizeToString(1024+1)); assertEquals("1K",Stat.sizeToString(1024+51)); assertEquals("1.1K",Stat.sizeToString(1024+52)); assertEquals("1024M",Stat.sizeToString(1024*1024*1024-1)); assertEquals("1G",Stat.sizeToString(1024*1024*1024)); assertEquals("8E",Stat.sizeToString(Long.MAX_VALUE)); }