You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/01/08 17:18:29 UTC

[commons-io] branch master updated (65b1302 -> 4c5fb3b)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git.


    from 65b1302  Update com.github.siom79.japicmp:japicmp-maven-plugin 0.15.1 -> 0.15.2.
     new be64c16  Add FileUtils.delete(File), refactor, reuse.
     new 4c5fb3b  Javadoc. Use the same param names as the JDK to avoid confusion.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  3 ++
 .../org/apache/commons/io/FileDeleteStrategy.java  |  9 ++--
 src/main/java/org/apache/commons/io/FileUtils.java | 20 ++++++--
 .../commons/io/output/LockableFileWriter.java      | 60 +++++++++++-----------
 .../org/apache/commons/io/FileUtilsTestCase.java   |  6 +++
 5 files changed, 61 insertions(+), 37 deletions(-)


[commons-io] 02/02: Javadoc. Use the same param names as the JDK to avoid confusion.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 4c5fb3befaf6639daccd9c77f8bb68744ed2da08
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 8 12:18:23 2021 -0500

    Javadoc. Use the same param names as the JDK to avoid confusion.
---
 .../commons/io/output/LockableFileWriter.java      | 60 +++++++++++-----------
 1 file changed, 30 insertions(+), 30 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/output/LockableFileWriter.java b/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
index 16c87a7..db85e42 100644
--- a/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
+++ b/src/main/java/org/apache/commons/io/output/LockableFileWriter.java
@@ -32,18 +32,20 @@ import org.apache.commons.io.FileUtils;
  * <p>
  * This class provides a simple alternative to <code>FileWriter</code>
  * that will use a lock file to prevent duplicate writes.
+ * </p>
  * <p>
- * <b>N.B.</b> the lock file is deleted when {@link #close()} is called
+ * <b>Note:</b> The lock file is deleted when {@link #close()} is called
  * - or if the main file cannot be opened initially.
- * In the (unlikely) event that the lockfile cannot be deleted,
+ * In the (unlikely) event that the lock file cannot be deleted,
  * this is not reported, and subsequent requests using
- * the same lockfile will fail.
+ * the same lock file will fail.
+ * </p>
  * <p>
  * By default, the file will be overwritten, but this may be changed to append.
  * The lock directory may be specified, but defaults to the system property
  * <code>java.io.tmpdir</code>.
  * The encoding may also be specified, and defaults to the platform default.
- *
+ * </p>
  */
 public class LockableFileWriter extends Writer {
     // Cannot extend ProxyWriter, as requires writer to be
@@ -54,6 +56,7 @@ public class LockableFileWriter extends Writer {
 
     /** The writer to decorate. */
     private final Writer out;
+
     /** The lock file. */
     private final File lockFile;
 
@@ -217,7 +220,6 @@ public class LockableFileWriter extends Writer {
         this(file, Charsets.toCharset(charsetName), append, lockDir);
     }
 
-    //-----------------------------------------------------------------------
     /**
      * Tests that we can write to the lock directory.
      *
@@ -252,7 +254,7 @@ public class LockableFileWriter extends Writer {
     }
 
     /**
-     * Initialize the wrapped file writer.
+     * Initializes the wrapped file writer.
      * Ensure that a cleanup occurs if the writer creation fails.
      *
      * @param file  the file to be accessed
@@ -276,9 +278,8 @@ public class LockableFileWriter extends Writer {
         }
     }
 
-    //-----------------------------------------------------------------------
     /**
-     * Closes the file writer and deletes the lockfile (if possible).
+     * Closes the file writer and deletes the lock file (if possible).
      *
      * @throws IOException if an I/O error occurs
      */
@@ -291,41 +292,40 @@ public class LockableFileWriter extends Writer {
         }
     }
 
-    //-----------------------------------------------------------------------
     /**
-     * Write a character.
-     * @param idx the character to write
+     * Writes a character.
+     * @param c the character to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(final int idx) throws IOException {
-        out.write(idx);
+    public void write(final int c) throws IOException {
+        out.write(c);
     }
 
     /**
-     * Write the characters from an array.
-     * @param chr the characters to write
+     * Writes the characters from an array.
+     * @param cbuf the characters to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(final char[] chr) throws IOException {
-        out.write(chr);
+    public void write(final char[] cbuf) throws IOException {
+        out.write(cbuf);
     }
 
     /**
-     * Write the specified characters from an array.
-     * @param chr the characters to write
-     * @param st The start offset
-     * @param end The number of characters to write
+     * Writes the specified characters from an array.
+     * @param cbuf the characters to write
+     * @param off The start offset
+     * @param len The number of characters to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(final char[] chr, final int st, final int end) throws IOException {
-        out.write(chr, st, end);
+    public void write(final char[] cbuf, final int off, final int len) throws IOException {
+        out.write(cbuf, off, len);
     }
 
     /**
-     * Write the characters from a string.
+     * Writes the characters from a string.
      * @param str the string to write
      * @throws IOException if an I/O error occurs
      */
@@ -335,19 +335,19 @@ public class LockableFileWriter extends Writer {
     }
 
     /**
-     * Write the specified characters from a string.
+     * Writes the specified characters from a string.
      * @param str the string to write
-     * @param st The start offset
-     * @param end The number of characters to write
+     * @param off The start offset
+     * @param len The number of characters to write
      * @throws IOException if an I/O error occurs
      */
     @Override
-    public void write(final String str, final int st, final int end) throws IOException {
-        out.write(str, st, end);
+    public void write(final String str, final int off, final int len) throws IOException {
+        out.write(str, off, len);
     }
 
     /**
-     * Flush the stream.
+     * Flushes the stream.
      * @throws IOException if an I/O error occurs
      */
     @Override


[commons-io] 01/02: Add FileUtils.delete(File), refactor, reuse.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit be64c1642913dbd2d2840a5a2bafab363349c43e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jan 8 12:17:54 2021 -0500

    Add FileUtils.delete(File), refactor, reuse.
    
    Javadoc.
---
 src/changes/changes.xml                              |  3 +++
 .../org/apache/commons/io/FileDeleteStrategy.java    |  9 +++++----
 src/main/java/org/apache/commons/io/FileUtils.java   | 20 +++++++++++++++++---
 .../org/apache/commons/io/FileUtilsTestCase.java     |  6 ++++++
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2f1601c..2396442 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -115,6 +115,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileSystem#supportsDriveLetter().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add FileUtils.delete(File).
+      </action>
       <!-- UPDATES -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Update junit-jupiter from 5.6.2 to 5.7.0 #153.
diff --git a/src/main/java/org/apache/commons/io/FileDeleteStrategy.java b/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
index 244c53d..31fbdf2 100644
--- a/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
+++ b/src/main/java/org/apache/commons/io/FileDeleteStrategy.java
@@ -135,16 +135,17 @@ public class FileDeleteStrategy {
      * A check has been made to ensure that the file will exist.
      * </p>
      * <p>
-     * This implementation uses {@link File#delete()}.
+     * This implementation uses {@link FileUtils#delete(File)}.
      * </p>
      *
-     * @param fileToDelete  the file to delete, exists, not null
+     * @param file  the file to delete, exists, not null
      * @return true if the file was deleted
      * @throws NullPointerException if the file is null
      * @throws IOException if an error occurs during file deletion
      */
-    protected boolean doDelete(final File fileToDelete) throws IOException {
-        return fileToDelete.delete();
+    protected boolean doDelete(final File file) throws IOException {
+        FileUtils.delete(file);
+        return true;
     }
 
     /**
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index 49d87d0..4a64f2d 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1109,6 +1109,22 @@ public class FileUtils {
     }
 
     /**
+     * Deletes the given File but throws IOException if it cannot, unlike {@link File#delete()}.
+     *
+     * @param file The file to delete.
+     * @return the given file.
+     * @throws IOException if the file cannot be deleted.
+     * @see File#delete()
+     * @since 2.9.0
+     */
+    public static File delete(final File file) throws IOException {
+        if (!file.delete()) {
+            throw new IOException("Unable to delete " + file);
+        }
+        return file;
+    }
+
+    /**
      * Deletes a directory recursively.
      *
      * @param directory directory to delete
@@ -1124,9 +1140,7 @@ public class FileUtils {
             cleanDirectory(directory);
         }
 
-        if (!directory.delete()) {
-            throw new IOException("Unable to delete directory " + directory + ".");
-        }
+        delete(directory);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
index e6aa9c4..b8a3798 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -1332,6 +1332,12 @@ public class FileUtilsTestCase {
     }
 
     @Test
+    public void testDelete() throws Exception {
+        assertEquals(testFile1, FileUtils.delete(testFile1));
+        assertThrows(IOException.class, () -> FileUtils.delete(new File("does not exist.nope")));
+    }
+
+    @Test
     public void testDeleteQuietlyDir() throws IOException {
         final File testDirectory = new File(temporaryFolder, "testDeleteQuietlyDir");
         final File testFile = new File(testDirectory, "testDeleteQuietlyFile");