]> source.dussan.org Git - jgit.git/commitdiff
File utility for creating a new empty file 05/2105/5
authorMatthias Sohn <matthias.sohn@sap.com>
Thu, 30 Dec 2010 22:10:55 +0000 (23:10 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Fri, 14 Jan 2011 16:28:14 +0000 (17:28 +0100)
The java.io.File.createNewFile() method for creating new empty files
reports failure by returning false. To ease proper checking of return
values provide a utility method wrapping createNewFile() throwing
IOException on failure.

Change-Id: I42a3dc9d8ff70af62e84de396e6a740050afa896
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FileUtilTest.java
org.eclipse.jgit/resources/org/eclipse/jgit/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/JGitText.java
org.eclipse.jgit/src/org/eclipse/jgit/util/FileUtils.java

index 4ad4234868de56cdaeb2b7b765408a99a791b5ca..d81e686c14d1c42fc239500f9fdc680ec6f34804 100644 (file)
@@ -175,4 +175,19 @@ public class FileUtilTest {
                assertTrue(f.delete());
        }
 
+       public void testCreateNewFile() throws IOException {
+               File f = new File(trash, "x");
+               FileUtils.createNewFile(f);
+               assertTrue(f.exists());
+
+               try {
+                       FileUtils.createNewFile(f);
+                       fail("creation of already existing file must fail");
+               } catch (IOException e) {
+                       // expected
+               }
+
+               FileUtils.delete(f);
+       }
+
 }
index c8f5920977d48e9141e5b1adf0153820178979f4..3049ae7d09acf1061e9eb6c1ffbc34f62c221134 100644 (file)
@@ -130,6 +130,7 @@ couldNotWriteFile=Could not write file {0}
 countingObjects=Counting objects
 createBranchFailedUnknownReason=Create branch failed for unknown reason
 createBranchUnexpectedResult=Create branch returned unexpected result {0}
+createNewFileFailed=Could not create new file {0}
 credentialPassword=Password
 credentialUsername=Username
 daemonAlreadyRunning=Daemon already running
index 083abe5f02436bc96ec2a1bef43508cd1fe19862..9718e6294520bb3a2235465c5940b4fe66b91fbb 100644 (file)
@@ -190,6 +190,7 @@ public class JGitText extends TranslationBundle {
        /***/ public String countingObjects;
        /***/ public String createBranchFailedUnknownReason;
        /***/ public String createBranchUnexpectedResult;
+       /***/ public String createNewFileFailed;
        /***/ public String credentialPassword;
        /***/ public String credentialUsername;
        /***/ public String daemonAlreadyRunning;
index 568f2882ba8c56e48da3ac830e46e98b6f8e948e..56d20d4ff044a0724930293ab805695d188304ac 100644 (file)
@@ -47,6 +47,7 @@ package org.eclipse.jgit.util;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.channels.FileLock;
 import java.text.MessageFormat;
 
 import org.eclipse.jgit.JGitText;
@@ -219,4 +220,26 @@ public class FileUtils {
                                        JGitText.get().mkDirsFailed, d.getAbsolutePath()));
                }
        }
+
+       /**
+        * Atomically creates a new, empty file named by this abstract pathname if
+        * and only if a file with this name does not yet exist. The check for the
+        * existence of the file and the creation of the file if it does not exist
+        * are a single operation that is atomic with respect to all other
+        * filesystem activities that might affect the file.
+        * <p>
+        * Note: this method should not be used for file-locking, as the resulting
+        * protocol cannot be made to work reliably. The {@link FileLock} facility
+        * should be used instead.
+        *
+        * @param f
+        *            the file to be created
+        * @throws IOException
+        *             if the named file already exists or if an I/O error occurred
+        */
+       public static void createNewFile(File f) throws IOException {
+               if (!f.createNewFile())
+                       throw new IOException(MessageFormat.format(
+                                       JGitText.get().createNewFileFailed, f));
+       }
 }