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 1b18ca1  Add PathUtils.readString(Path, Charset).
1b18ca1 is described below

commit 1b18ca1f91aaea8f730a335cb49ebe406b996d4f
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Mon Jul 19 10:01:32 2021 -0400

    Add PathUtils.readString(Path, Charset).
---
 src/changes/changes.xml                                 |  3 +++
 src/main/java/org/apache/commons/io/file/PathUtils.java | 16 ++++++++++++++++
 .../java/org/apache/commons/io/file/PathUtilsTest.java  | 17 +++++++++++++++++
 3 files changed, 36 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0f2251e..9066d5c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -105,6 +105,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add NullReader.INSTANCE.
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add PathUtils.readString(Path, Charset).
+      </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/file/PathUtils.java 
b/src/main/java/org/apache/commons/io/file/PathUtils.java
index f8b364b..ca94336 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.UncheckedIOException;
 import java.net.URI;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.nio.file.CopyOption;
 import java.nio.file.DirectoryStream;
 import java.nio.file.FileVisitOption;
@@ -56,6 +57,7 @@ import java.util.stream.Collector;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import org.apache.commons.io.Charsets;
 import org.apache.commons.io.IOExceptionList;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.file.Counters.PathCounters;
@@ -874,6 +876,20 @@ public final class PathUtils {
     }
 
     /**
+     * Reads the given path as a String.
+     *
+     * @param path The source path.
+     * @param charset How to convert bytes to a String.
+     * @return a new String
+     * @throws IOException if an I/O error occurs reading from the stream
+     * @see Files#readAllBytes(Path)
+     * @since 2.12.0
+     */
+    public static Object readString(final Path path, final Charset charset) 
throws IOException {
+        return new String(Files.readAllBytes(path), 
Charsets.toCharset(charset));
+    }
+
+    /**
      * Relativizes all files in the given {@code collection} against a {@code 
parent}.
      *
      * @param collection The collection of paths to relativize.
diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java 
b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
index 70c8346..6924e5d 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.net.URI;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.DirectoryStream;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
@@ -34,6 +35,7 @@ import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.commons.io.filefilter.NameFileFilter;
+import org.apache.commons.lang3.StringUtils;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 
@@ -218,4 +220,19 @@ public class PathUtilsTest extends TestArguments {
         }
     }
 
+    @Test
+    public void testReadStringEmptyFile() throws IOException {
+        final Path path = 
Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
+        assertEquals(StringUtils.EMPTY, PathUtils.readString(path, 
StandardCharsets.UTF_8));
+        assertEquals(StringUtils.EMPTY, PathUtils.readString(path, null));
+    }
+
+    @Test
+    public void testReadStringSimpleUtf8() throws IOException {
+        final Path path = 
Paths.get("src/test/resources/org/apache/commons/io/test-file-simple-utf8.bin");
+        final String expected = "ABC\r\n";
+        assertEquals(expected, PathUtils.readString(path, 
StandardCharsets.UTF_8));
+        assertEquals(expected, PathUtils.readString(path, null));
+    }
+
 }

Reply via email to