You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2010/09/28 23:49:24 UTC
svn commit: r1002367 - in /commons/proper/io/trunk/src:
java/org/apache/commons/io/FileUtils.java
test/org/apache/commons/io/FileUtilsTestCase.java
Author: niallp
Date: Tue Sep 28 21:49:24 2010
New Revision: 1002367
URL: http://svn.apache.org/viewvc?rev=1002367&view=rev
Log:
IO-238 Add sizeOf(File) Method to FileUtils - thanks to Michael Wooten for the patch
Modified:
commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=1002367&r1=1002366&r2=1002367&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Tue Sep 28 21:49:24 2010
@@ -1702,6 +1702,39 @@ public class FileUtils {
//-----------------------------------------------------------------------
/**
+ * Returns the size of the specified file or directory. If the provided
+ * {@link File} is a regular file, then the file's length is returned.
+ * If the argument is a directory, then the size of the directory is
+ * calculated recursively. If a directory or subdirectory is security
+ * restricted, its size will not be included.
+ *
+ * @param file the regular file or directory to return the size
+ * of (must not be <code>null</code>).
+ *
+ * @return the length of the file, or recursive size of the directory,
+ * provided (in bytes).
+ *
+ * @throws NullPointerException if the file is <code>null</code>
+ * @throws IllegalArgumentException if the file does not exist.
+ *
+ * @since Commons IO 2.0
+ */
+ public static long sizeOf(File file) {
+
+ if (!file.exists()) {
+ String message = file + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
+
+ if (file.isDirectory()) {
+ return sizeOfDirectory(file);
+ } else {
+ return file.length();
+ }
+
+ }
+
+ /**
* Counts the size of a directory recursively (sum of the length of all files).
*
* @param directory directory to inspect, must not be <code>null</code>
@@ -1726,11 +1759,7 @@ public class FileUtils {
return 0L;
}
for (File file : files) {
- if (file.isDirectory()) {
- size += sizeOfDirectory(file);
- } else {
- size += file.length();
- }
+ size += sizeOf(file);
}
return size;
Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=1002367&r1=1002366&r2=1002367&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Tue Sep 28 21:49:24 2010
@@ -547,6 +547,44 @@ public class FileUtilsTestCase extends F
FileUtils.sizeOfDirectory(file));
}
+ /**
+ * Tests the {@link FileUtils#sizeOf(File)} method.
+ * @throws Exception
+ */
+ public void testSizeOf() throws Exception {
+ File file = new File(getTestDirectory(), getName());
+
+ // Null argument
+ try {
+ FileUtils.sizeOf(null);
+ fail("Exception expected.");
+ } catch (NullPointerException ex) {}
+
+ // Non-existent file
+ try {
+ FileUtils.sizeOf(file);
+ fail("Exception expected.");
+ } catch (IllegalArgumentException ex) {}
+
+ // Creates file
+ file.createNewFile();
+ file.deleteOnExit();
+
+ // New file
+ assertEquals(0, FileUtils.sizeOf(file));
+ file.delete();
+
+ // Existing file
+ assertEquals("Unexpected files size",
+ testFile1Size,
+ FileUtils.sizeOf(testFile1));
+
+ // Existing directory
+ assertEquals("Unexpected directory size",
+ TEST_DIRECTORY_SIZE,
+ FileUtils.sizeOf(getTestDirectory()));
+ }
+
// isFileNewer / isFileOlder
public void testIsFileNewerOlder() throws Exception {
File reference = new File(getTestDirectory(), "FileUtils-reference.txt");