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

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/master by this push:
     new eb631d79e3 CAUSEWAY-2297: refactors FileUtils.copyWithCrlf into more 
general purpose utility
eb631d79e3 is described below

commit eb631d79e36596862bd21984f2d1ba81d9af81a3
Author: andi-huber <[email protected]>
AuthorDate: Sat May 20 10:25:34 2023 +0200

    CAUSEWAY-2297: refactors FileUtils.copyWithCrlf into more general
    purpose utility
---
 .../org/apache/causeway/commons/io/FileUtils.java  | 35 +++++++++++++++++-----
 .../value/ValueTypeGenTemplateTest.java            | 10 +++++--
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git 
a/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java 
b/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
index f4f21f84d7..6bf87eb421 100644
--- a/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
+++ b/commons/src/main/java/org/apache/causeway/commons/io/FileUtils.java
@@ -22,22 +22,24 @@ import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileReader;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.NotDirectoryException;
+import java.nio.file.OpenOption;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
 import java.util.LinkedHashSet;
 import java.util.Optional;
 import java.util.Set;
 import java.util.function.Consumer;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.function.UnaryOperator;
 
 import org.springframework.lang.Nullable;
 
@@ -309,15 +311,34 @@ public class FileUtils {
         Files.copy(from.toPath(), to.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
     }
 
+    /**
+     * Copy all lines {@code from} file {@code to} file, using given {@link 
Charset}
+     * and processing each line before writing using {@code lineProcessor}.
+     *
+     * @param from - the file to read lines from
+     * @param to - the file to write the processed lines to
+     * @param openOptions - If no options are present then this method works 
as if the {@link
+     * StandardOpenOption#CREATE CREATE}, {@link
+     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
+     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
+     * opens the file for writing, creating the file if it doesn't exist, or
+     * initially truncating an existing regular-file to
+     * a size of {@code 0} if it exists.
+     */
     @SneakyThrows
-    public void copyWithCrlf(final @NonNull File from, final @NonNull File to) 
{
-        try (final BufferedReader reader = new BufferedReader(new 
FileReader(from));
-             final BufferedWriter writer = new BufferedWriter(new 
FileWriter(to))) {
+    public void copyLines(
+            final @NonNull File from,
+            final @NonNull File to,
+            final @NonNull Charset charset,
+            final @NonNull UnaryOperator<String> lineProcessor,
+            final @NonNull OpenOption... openOptions) {
+
+        try (final BufferedReader reader = 
Files.newBufferedReader(from.toPath(), charset);
+             final BufferedWriter writer = 
Files.newBufferedWriter(to.toPath(), charset, openOptions)) {
 
             String line;
             while ((line = reader.readLine()) != null) {
-                // Append CRLF line endings to each line
-                writer.write(line + "\r\n");
+                writer.write(lineProcessor.apply(line));
             }
         }
     }
diff --git 
a/tooling/metaprog/src/test/java/org/apache/causeway/tooling/metaprog/demoshowcases/value/ValueTypeGenTemplateTest.java
 
b/tooling/metaprog/src/test/java/org/apache/causeway/tooling/metaprog/demoshowcases/value/ValueTypeGenTemplateTest.java
index 9f23c540bb..7c81e39ca8 100644
--- 
a/tooling/metaprog/src/test/java/org/apache/causeway/tooling/metaprog/demoshowcases/value/ValueTypeGenTemplateTest.java
+++ 
b/tooling/metaprog/src/test/java/org/apache/causeway/tooling/metaprog/demoshowcases/value/ValueTypeGenTemplateTest.java
@@ -41,6 +41,7 @@ import 
org.apache.causeway.commons.internal.functions._Predicates;
 import org.apache.causeway.commons.io.FileUtils;
 import org.apache.causeway.commons.io.TextUtils;
 
+import lombok.NonNull;
 import lombok.SneakyThrows;
 import lombok.val;
 import lombok.extern.log4j.Log4j2;
@@ -124,7 +125,7 @@ class ValueTypeGenTemplateTest {
         generatedFiles.forEach(src->{
             val dest = new File(destinationRoot, 
FileUtils.realtiveFileName(sourceRoot, src));
             FileUtils.makeDir(dest.getParentFile());
-            FileUtils.copyWithCrlf(src, dest);
+            copyWithCrlf(src, dest);
 //            FileUtils.copy(src, dest);
         });
     }
@@ -135,7 +136,7 @@ class ValueTypeGenTemplateTest {
             val dest = new File(destinationRoot, 
FileUtils.realtiveFileName(sourceRoot, src));
             if(!dest.exists()) {
                 FileUtils.makeDir(dest.getParentFile());
-                FileUtils.copyWithCrlf(src, dest);
+                copyWithCrlf(src, dest);
 //                FileUtils.copy(src, dest);
             }
         });
@@ -186,4 +187,9 @@ class ValueTypeGenTemplateTest {
         return "";
     }
 
+    private void copyWithCrlf(final @NonNull File from, final @NonNull File 
to) {
+        // Append CRLF line endings to each line
+        FileUtils.copyLines(from, to, StandardCharsets.UTF_8, line->line + 
"\r\n");
+    }
+
 }

Reply via email to