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
The following commit(s) were added to refs/heads/master by this push:
new 82cb5d4 Refactor internals into new methods:
82cb5d4 is described below
commit 82cb5d493c066f8500548996ac78ead9e94d7511
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jul 30 11:57:15 2021 -0400
Refactor internals into new methods:
- Add FileUtils.newOutputStream(File, boolean).
- Add PathUtils.newOutputStream(Path, boolean).
---
src/changes/changes.xml | 6 +++++
src/main/java/org/apache/commons/io/FileUtils.java | 26 +++++++++++++++++-----
.../java/org/apache/commons/io/file/PathUtils.java | 18 +++++++++++++++
.../commons/io/output/FileWriterWithEncoding.java | 7 +-----
4 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 247e902..132f1a2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -114,6 +114,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add PathUtils.readString(Path, Charset).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add FileUtils.newOutputStream(File, boolean).
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add PathUtils.newOutputStream(Path, boolean).
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java
b/src/main/java/org/apache/commons/io/FileUtils.java
index 0e76e3a..6038141 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -2365,6 +2365,20 @@ public class FileUtils {
}
/**
+ * Creates a new OutputStream by opening or creating a file, returning an
output stream that may be used to write bytes
+ * to the file.
+ *
+ * @param append Whether or not to append.
+ * @param file the File.
+ * @return a new OutputStream.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static OutputStream newOutputStream(final File file, final boolean
append) throws IOException {
+ return PathUtils.newOutputStream(file.toPath(), append);
+ }
+
+ /**
* Opens a {@link FileInputStream} for the specified file, providing
better error messages than simply calling
* {@code new FileInputStream(file)}.
* <p>
@@ -2581,6 +2595,7 @@ public class FileUtils {
return readLines(file, Charsets.toCharset(charsetName));
}
+
private static void requireAbsent(final File file, final String name)
throws FileExistsException {
if (file.exists()) {
throw new FileExistsException(
@@ -2588,7 +2603,6 @@ public class FileUtils {
}
}
-
/**
* Throws IllegalArgumentException if the given files' canonical
representations are equal.
*
@@ -3206,6 +3220,8 @@ public class FileUtils {
write(file, data, charset, false);
}
+ // Private method, must be invoked will a directory parameter
+
/**
* Writes a CharSequence to a file creating the file if it does not exist.
*
@@ -3222,8 +3238,6 @@ public class FileUtils {
writeStringToFile(file, Objects.toString(data, null), charset, append);
}
- // Private method, must be invoked will a directory parameter
-
/**
* Writes a CharSequence to a file creating the file if it does not exist.
*
@@ -3256,6 +3270,8 @@ public class FileUtils {
write(file, data, Charsets.toCharset(charsetName), append);
}
+ // Must be called with a directory
+
/**
* Writes a byte array to a file creating the file if it does not exist.
* <p>
@@ -3272,8 +3288,6 @@ public class FileUtils {
writeByteArrayToFile(file, data, false);
}
- // Must be called with a directory
-
/**
* Writes a byte array to a file creating the file if it does not exist.
*
@@ -3357,6 +3371,7 @@ public class FileUtils {
writeLines(file, null, lines, null, append);
}
+
/**
* Writes the {@code toString()} value of each item in a collection to
* the specified {@code File} line by line.
@@ -3373,7 +3388,6 @@ public class FileUtils {
writeLines(file, null, lines, lineEnding, false);
}
-
/**
* Writes the {@code toString()} value of each item in a collection to
* the specified {@code File} line by line.
diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index 5e51cdc..ce14da9 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -20,6 +20,7 @@ package org.apache.commons.io.file;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URL;
@@ -36,6 +37,7 @@ import java.nio.file.NotDirectoryException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
@@ -834,6 +836,22 @@ public final class PathUtils {
}
/**
+ * Creates a new OutputStream by opening or creating a file, returning an
output stream that may be used to write bytes
+ * to the file.
+ * @param path the Path.
+ * @param append Whether or not to append.
+ *
+ * @return a new OutputStream.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.12.0
+ */
+ public static OutputStream newOutputStream(final Path path, final boolean
append) throws IOException {
+ return Files.newOutputStream(path, append ?
+ new OpenOption[] {StandardOpenOption.APPEND} :
+ new OpenOption[] {StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING});
+ }
+
+ /**
* Returns true if the given options contain {@link
StandardDeleteOption#OVERRIDE_READ_ONLY}.
*
* @param deleteOptions the array to test
diff --git
a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
index 5962e61..23b8996 100644
--- a/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
+++ b/src/main/java/org/apache/commons/io/output/FileWriterWithEncoding.java
@@ -23,9 +23,6 @@ import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
-import java.nio.file.Files;
-import java.nio.file.OpenOption;
-import java.nio.file.StandardOpenOption;
import java.util.Objects;
import org.apache.commons.io.Charsets;
@@ -67,9 +64,7 @@ public class FileWriterWithEncoding extends Writer {
OutputStream stream = null;
final boolean fileExistedAlready = file.exists();
try {
- stream = Files.newOutputStream(file.toPath(), append ?
- new OpenOption[] {StandardOpenOption.APPEND} :
- new OpenOption[] {StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING});
+ stream = FileUtils.newOutputStream(file, append);
if (encoding == null || encoding instanceof Charset) {
return new OutputStreamWriter(stream,
Charsets.toCharset((Charset) encoding));
}