Author: toad
Date: 2007-02-22 01:10:13 +0000 (Thu, 22 Feb 2007)
New Revision: 11893

Modified:
   trunk/freenet/src/freenet/support/io/FileUtil.java
Log:
Sort-of apply patch from Anonymous at o9_0DTuZniSf_+oDmRsonByWxsI :
Clean up rounding-up-to-next-block functions.
Also minor fix to size calculation - 512 bytes chunks for filenames, at most.

Modified: trunk/freenet/src/freenet/support/io/FileUtil.java
===================================================================
--- trunk/freenet/src/freenet/support/io/FileUtil.java  2007-02-22 00:38:16 UTC 
(rev 11892)
+++ trunk/freenet/src/freenet/support/io/FileUtil.java  2007-02-22 01:10:13 UTC 
(rev 11893)
@@ -5,24 +5,30 @@

 import java.io.File;

-public class FileUtil {
+final public class FileUtil {

+       /** Round up a value to the next multiple of a power of 2 */
+       private static final long roundup_2n (long val, int blocksize) {
+               int mask=blocksize-1;
+               return (val+mask)&~mask;
+       }
+       
        /**
         * Guesstimate real disk usage for a file with a given filename, of a 
given length.
         */
-       public static long estimateUsage(File file, long l) {
+       public static long estimateUsage(File file, long flen) {
                /**
                 * It's possible that none of these assumptions are accurate 
for any filesystem;
                 * this is intended to be a plausible worst case.
                 */
                // Assume 4kB clusters for calculating block usage (NTFS)
-               long blockUsage = ((l / 4096) + (l % 4096 > 0 ? 1 : 0)) * 4096;
+               long blockUsage = roundup_2n(flen, 4096);
                // Assume 512 byte filename entries, with 100 bytes overhead, 
for filename overhead (NTFS)
                String filename = file.getName();
                int nameLength = filename.getBytes().length + 100;
-               long filenameUsage = ((nameLength / 4096) + (nameLength % 4096 
> 0 ? 1 : 0)) * 4096;
+               long filenameUsage = roundup_2n(nameLength, 512);
                // Assume 50 bytes per block tree overhead with 1kB blocks 
(reiser3 worst case)
-               long extra = ((l / 1024) + (l % 1024 > 0 ? 1 : 0) + 1) * 50;
+               long extra = (roundup_2n(flen, 1024) / 1024) * 50;
                return blockUsage + filenameUsage + extra;
        }
 }


Reply via email to