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 4117033f Refactor using new Supplier API
IOUtils.toString(IOSupplier<InputStream>, Charset[, IOSupplier<String>]).
4117033f is described below
commit 4117033f858a142b4129d9e1d0ab33a265a7558b
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Apr 18 08:35:23 2023 -0400
Refactor using new Supplier API
IOUtils.toString(IOSupplier<InputStream>, Charset[,
IOSupplier<String>]).
Refactor using new Supplier API
org.apache.commons.io.file.PathUtils.copy(IOSupplier<InputStream>, Path,
CopyOption...).
---
src/changes/changes.xml | 6 ++
src/main/java/org/apache/commons/io/FileUtils.java | 12 +--
src/main/java/org/apache/commons/io/IOUtils.java | 117 ++++++++++++++-------
.../org/apache/commons/io/file/FilesUncheck.java | 2 +-
.../java/org/apache/commons/io/file/PathUtils.java | 33 ++++--
.../java/org/apache/commons/io/IOUtilsTest.java | 12 +++
6 files changed, 127 insertions(+), 55 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5d97f7e4..e0ebdafa 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -474,6 +474,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory, maxxedev">
Add PollingQueueInputStream and TakingQueueInputStream to replace
QueueInputStream. See also #447.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory, maxxedev">
+ Refactor using new Supplier API
IOUtils.toString(IOSupplier<InputStream>, Charset[,
IOSupplier<String>]).
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory, maxxedev">
+ Refactor using new Supplier API
org.apache.commons.io.file.PathUtils.copy(IOSupplier<InputStream>, Path,
CopyOption...).
+ </action>
<!-- UPDATE -->
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
Bump actions/cache from 2.1.6 to 3.0.10 #307, #337, #393.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java
b/src/main/java/org/apache/commons/io/FileUtils.java
index 156e5637..b5a80e5a 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -1059,11 +1059,9 @@ public class FileUtils {
* @throws IOException if an IO error occurs during copying
*/
public static void copyURLToFile(final URL source, final File destination)
throws IOException {
- try (InputStream stream = source.openStream()) {
- final Path path = destination.toPath();
- PathUtils.createParentDirectories(path);
- Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
- }
+ final Path path = destination.toPath();
+ PathUtils.createParentDirectories(path);
+ PathUtils.copy(source::openStream, path,
StandardCopyOption.REPLACE_EXISTING);
}
/**
@@ -2597,9 +2595,7 @@ public class FileUtils {
* @since 2.3
*/
public static String readFileToString(final File file, final Charset
charsetName) throws IOException {
- try (InputStream inputStream = Files.newInputStream(file.toPath())) {
- return IOUtils.toString(inputStream,
Charsets.toCharset(charsetName));
- }
+ return IOUtils.toString(() -> Files.newInputStream(file.toPath()),
Charsets.toCharset(charsetName));
}
/**
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java
b/src/main/java/org/apache/commons/io/IOUtils.java
index 9ec94776..a074ed1a 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -56,6 +56,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.function.IOSupplier;
import org.apache.commons.io.input.QueueInputStream;
import org.apache.commons.io.output.AppendableWriter;
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -1089,7 +1090,7 @@ public class IOUtils {
* This method uses {@link InputStreamReader}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param writer the {@link Writer} to write to
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
@@ -1113,7 +1114,7 @@ public class IOUtils {
* This method uses {@link InputStreamReader}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param writer the {@link Writer} to write to
* @param inputCharset the charset to use for the input stream, null means
platform default
* @throws NullPointerException if the input or output is null
@@ -1141,7 +1142,7 @@ public class IOUtils {
* This method uses {@link InputStreamReader}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param writer the {@link Writer} to write to
* @param inputCharsetName the name of the requested charset for the
InputStream, null means platform default
* @throws NullPointerException if the input or
output is null
@@ -1200,7 +1201,7 @@ public class IOUtils {
* use the {@link #copyLarge(Reader, Writer)} method.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param output the {@link Appendable} to write to
* @return the number of characters copied, or -1 if > Integer.MAX_VALUE
* @throws NullPointerException if the input or output is null
@@ -1218,7 +1219,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param output the {@link Appendable} to write to
* @param buffer the buffer to be used for the copy
* @return the number of characters copied
@@ -1253,7 +1254,7 @@ public class IOUtils {
* This method uses {@link OutputStreamWriter}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param output the {@link OutputStream} to write to
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
@@ -1282,7 +1283,7 @@ public class IOUtils {
* This method uses {@link OutputStreamWriter}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param output the {@link OutputStream} to write to
* @param outputCharset the charset to use for the OutputStream, null
means platform default
* @throws NullPointerException if the input or output is null
@@ -1318,7 +1319,7 @@ public class IOUtils {
* This method uses {@link OutputStreamWriter}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param output the {@link OutputStream} to write to
* @param outputCharsetName the name of the requested charset for the
OutputStream, null means platform default
* @throws NullPointerException if the input or
output is null
@@ -1476,7 +1477,7 @@ public class IOUtils {
* </p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param output the {@link OutputStream} to write to
* @param inputOffset : number of bytes to skip from input before copying
* -ve values are ignored
@@ -1504,7 +1505,7 @@ public class IOUtils {
* this is done to guarantee that the correct number of characters are
skipped.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param output the {@link OutputStream} to write to
* @param inputOffset : number of bytes to skip from input before copying
* -ve values are ignored
@@ -1598,7 +1599,7 @@ public class IOUtils {
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param writer the {@link Writer} to write to
* @param inputOffset : number of chars to skip from input before copying
* -ve values are ignored
@@ -1621,7 +1622,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param writer the {@link Writer} to write to
* @param inputOffset : number of chars to skip from input before copying
* -ve values are ignored
@@ -1745,7 +1746,7 @@ public class IOUtils {
* }
* </pre>
*
- * @param input the {@link InputStream} to read from, not null
+ * @param input the {@link InputStream} to read, not null
* @param charset the charset to use, null means platform default
* @return an Iterator of the lines in the reader, never null
* @throws IllegalArgumentException if the input is null
@@ -1780,7 +1781,7 @@ public class IOUtils {
* }
* </pre>
*
- * @param input the {@link InputStream} to read from, not null
+ * @param input the {@link InputStream} to read, not null
* @param charsetName the encoding to use, null means platform default
* @return an Iterator of the lines in the reader, never null
* @throws IllegalArgumentException if the input is
null
@@ -1817,7 +1818,7 @@ public class IOUtils {
* }
* </pre>
*
- * @param reader the {@link Reader} to read from, not null
+ * @param reader the {@link Reader} to read, not null
* @return an Iterator of the lines in the reader, never null
* @throws IllegalArgumentException if the reader is null
* @since 1.2
@@ -2083,7 +2084,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from, not null
+ * @param input the {@link InputStream} to read, not null
* @return the list of Strings, never null
* @throws NullPointerException if the input is null
* @throws UncheckedIOException if an I/O error occurs
@@ -2103,7 +2104,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from, not null
+ * @param input the {@link InputStream} to read, not null
* @param charset the charset to use, null means platform default
* @return the list of Strings, never null
* @throws NullPointerException if the input is null
@@ -2126,7 +2127,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from, not null
+ * @param input the {@link InputStream} to read, not null
* @param charsetName the name of the requested charset, null means
platform default
* @return the list of Strings, never null
* @throws NullPointerException if the input is
null
@@ -2148,7 +2149,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from, not null
+ * @param reader the {@link Reader} to read, not null
* @return the list of Strings, never null
* @throws NullPointerException if the input is null
* @throws UncheckedIOException if an I/O error occurs
@@ -2607,7 +2608,7 @@ public class IOUtils {
* before using {@link IOUtils#toByteArray(InputStream, int)} to read into
the byte array.
* (Arrays can have no more than Integer.MAX_VALUE entries anyway)
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param size the size of {@link InputStream} to read, where 0 <
{@code size} <= min(Integer.MAX_VALUE, length of input stream).
* @return byte [] the requested byte array, of length {@code size}
* @throws IOException if an I/O error occurs or {@link
InputStream} length is less than {@code size}
@@ -2630,7 +2631,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @return the requested byte array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
@@ -2649,7 +2650,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param charset the charset to use, null means platform default
* @return the requested byte array
* @throws NullPointerException if the input is null
@@ -2675,7 +2676,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @param charsetName the name of the requested charset, null means
platform default
* @return the requested byte array
* @throws NullPointerException if the input is
null
@@ -2758,7 +2759,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param inputStream the {@link InputStream} to read from
+ * @param inputStream the {@link InputStream} to read
* @return the requested character array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
@@ -2778,7 +2779,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param inputStream the {@link InputStream} to read from
+ * @param inputStream the {@link InputStream} to read
* @param charset the charset to use, null means platform default
* @return the requested character array
* @throws NullPointerException if the input is null
@@ -2804,7 +2805,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param inputStream the {@link InputStream} to read from
+ * @param inputStream the {@link InputStream} to read
* @param charsetName the name of the requested charset, null means
platform default
* @return the requested character array
* @throws NullPointerException if the input is
null
@@ -2825,7 +2826,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @return the requested character array
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
@@ -2935,7 +2936,7 @@ public class IOUtils {
* Gets the contents of a {@code byte[]} as a String
* using the default character encoding of the platform.
*
- * @param input the byte array to read from
+ * @param input the byte array to read
* @return the requested String
* @throws NullPointerException if the input is null
* @deprecated 2.5 Use {@link String#String(byte[])} instead
@@ -2954,7 +2955,7 @@ public class IOUtils {
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* </p>
*
- * @param input the byte array to read from
+ * @param input the byte array to read
* @param charsetName the name of the requested charset, null means
platform default
* @return the requested String
* @throws NullPointerException if the input is null
@@ -2971,7 +2972,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
@@ -2990,7 +2991,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param charset the charset to use, null means platform default
* @return the requested String
* @throws NullPointerException if the input is null
@@ -3016,7 +3017,7 @@ public class IOUtils {
* {@link BufferedInputStream}.
* </p>
*
- * @param input the {@link InputStream} to read from
+ * @param input the {@link InputStream} to read
* @param charsetName the name of the requested charset, null means
platform default
* @return the requested String
* @throws NullPointerException if the input is
null
@@ -3030,6 +3031,52 @@ public class IOUtils {
return toString(input, Charsets.toCharset(charsetName));
}
+ /**
+ * Gets the contents of an {@link InputStream} from a supplier as a String
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * {@link BufferedInputStream}.
+ * </p>
+ *
+ * @param input supplies the {@link InputStream} to read
+ * @param charset the charset to use, null means platform default
+ * @return the requested String
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.12.0
+ */
+ public static String toString(final IOSupplier<InputStream> input, final
Charset charset) throws IOException {
+ return toString(input, charset, () -> {
+ throw new NullPointerException("input");
+ });
+ }
+
+ /**
+ * Gets the contents of an {@link InputStream} from a supplier as a String
+ * using the specified character encoding.
+ * <p>
+ * This method buffers the input internally, so there is no need to use a
+ * {@link BufferedInputStream}.
+ * </p>
+ *
+ * @param input supplies the {@link InputStream} to read
+ * @param charset the charset to use, null means platform default
+ * @param defaultString the default return value if the supplier or its
value is null.
+ * @return the requested String
+ * @throws NullPointerException if the input is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.12.0
+ */
+ public static String toString(final IOSupplier<InputStream> input, final
Charset charset, final IOSupplier<String> defaultString) throws IOException {
+ if (input == null) {
+ return defaultString.get();
+ }
+ try (InputStream inputStream = input.get()) {
+ return inputStream != null ? toString(inputStream, charset) :
defaultString.get();
+ }
+ }
+
/**
* Gets the contents of a {@link Reader} as a String.
* <p>
@@ -3037,7 +3084,7 @@ public class IOUtils {
* {@link BufferedReader}.
* </p>
*
- * @param reader the {@link Reader} to read from
+ * @param reader the {@link Reader} to read
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs
@@ -3116,9 +3163,7 @@ public class IOUtils {
* @since 2.3
*/
public static String toString(final URL url, final Charset encoding)
throws IOException {
- try (InputStream inputStream = url.openStream()) {
- return toString(inputStream, encoding);
- }
+ return toString(url::openStream, encoding);
}
/**
diff --git a/src/main/java/org/apache/commons/io/file/FilesUncheck.java
b/src/main/java/org/apache/commons/io/file/FilesUncheck.java
index 5de2a4b0..d2b02adc 100644
--- a/src/main/java/org/apache/commons/io/file/FilesUncheck.java
+++ b/src/main/java/org/apache/commons/io/file/FilesUncheck.java
@@ -57,7 +57,7 @@ import org.apache.commons.io.function.Uncheck;
public class FilesUncheck {
/**
- * Delegates to {@link Files#copy(InputStream, Path,CopyOption...)}
throwing {@link UncheckedIOException} instead of
+ * Delegates to {@link Files#copy(InputStream, Path, CopyOption...)}
throwing {@link UncheckedIOException} instead of
* {@link IOException}.
*
* @param in See delegate.
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 fd6cccbf..9acce158 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -76,6 +76,7 @@ import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.io.file.attribute.FileTimes;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.function.IOFunction;
+import org.apache.commons.io.function.IOSupplier;
import org.apache.commons.io.function.Uncheck;
/**
@@ -254,6 +255,22 @@ public final class PathUtils {
return getLastModifiedTime(file, options).compareTo(fileTime);
}
+ /**
+ * Copies the InputStream from the supplier with {@link
Files#copy(InputStream, Path, CopyOption...)}.
+ *
+ * @param in Supplies the InputStream.
+ * @param target See {@link Files#copy(InputStream, Path, CopyOption...)}.
+ * @param copyOptions See {@link Files#copy(InputStream, Path,
CopyOption...)}.
+ * @return See {@link Files#copy(InputStream, Path, CopyOption...)}
+ * @throws IOException See {@link Files#copy(InputStream, Path,
CopyOption...)}
+ * @since 2.12.0
+ */
+ public static long copy(final IOSupplier<InputStream> in, final Path
target, final CopyOption... copyOptions) throws IOException {
+ try (InputStream inputStream = in.get()) {
+ return Files.copy(inputStream, target, copyOptions);
+ }
+ }
+
/**
* Copies a directory to another directory.
*
@@ -280,10 +297,8 @@ public final class PathUtils {
* @see Files#copy(InputStream, Path, CopyOption...)
*/
public static Path copyFile(final URL sourceFile, final Path targetFile,
final CopyOption... copyOptions) throws IOException {
- try (InputStream inputStream = sourceFile.openStream()) {
- Files.copy(inputStream, targetFile, copyOptions);
- return targetFile;
- }
+ copy(sourceFile::openStream, targetFile, copyOptions);
+ return targetFile;
}
/**
@@ -311,11 +326,9 @@ public final class PathUtils {
* @see Files#copy(InputStream, Path, CopyOption...)
*/
public static Path copyFileToDirectory(final URL sourceFile, final Path
targetDirectory, final CopyOption... copyOptions) throws IOException {
- try (InputStream inputStream = sourceFile.openStream()) {
- final Path resolve =
targetDirectory.resolve(FilenameUtils.getName(sourceFile.getFile()));
- Files.copy(inputStream, resolve, copyOptions);
- return resolve;
- }
+ final Path resolve =
targetDirectory.resolve(FilenameUtils.getName(sourceFile.getFile()));
+ copy(sourceFile::openStream, resolve, copyOptions);
+ return resolve;
}
/**
@@ -572,7 +585,7 @@ public final class PathUtils {
* @param path the path to delete.
* @since 3.13.0
*/
- public static void deleteOnExit(Path path) {
+ public static void deleteOnExit(final Path path) {
Objects.requireNonNull(path.toFile()).deleteOnExit();
}
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java
b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 0586551a..4fdc91c3 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -53,6 +54,7 @@ import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.Selector;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -1613,6 +1615,16 @@ public class IOUtilsTest {
}
}
+ @Test
+ public void testToString_InputStreamSupplier() throws Exception {
+ final String out = IOUtils.toString(() ->
Files.newInputStream(testFilePath), Charset.defaultCharset());
+ assertNotNull(out);
+ assertEquals(FILE_SIZE, out.length(), "Wrong output size");
+ assertNull(IOUtils.toString(null, Charset.defaultCharset(), () ->
null));
+ assertNull(IOUtils.toString(() -> null, Charset.defaultCharset(), ()
-> null));
+ assertEquals("A", IOUtils.toString(null, Charset.defaultCharset(), ()
-> "A"));
+ }
+
@Test
public void testToString_Reader() throws Exception {
try (Reader fin = Files.newBufferedReader(testFilePath)) {