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 8e63832a16457ca919cff15b140039d8e9543a69 Author: Gary Gregory <[email protected]> AuthorDate: Sun Jul 24 08:06:10 2022 -0400 Add IOUtils.consume(Reader). --- src/changes/changes.xml | 3 +++ src/main/java/org/apache/commons/io/IOUtils.java | 18 +++++++++++++++++- src/test/java/org/apache/commons/io/IOUtilsTest.java | 19 ++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9ff69f60..50c957b5 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -412,6 +412,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="add" due-to="Gary Gregory"> Add IOBiFunction, IOTriFunction, IOQuadFunction. </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add IOUtils.consume(Reader). + </action> <!-- UPDATE --> <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory"> Bump actions/cache from 2.1.6 to 3.0.5 #307, #337. diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index a25f818a..0c9f824a 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -832,7 +832,23 @@ public class IOUtils { * @since 2.8.0 */ public static long consume(final InputStream input) throws IOException { - return copyLarge(input, NullOutputStream.INSTANCE, getByteArray()); + return copyLarge(input, NullOutputStream.INSTANCE); + } + + /** + * Consumes characters from a {@link Reader} and ignores them. + * <p> + * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. + * </p> + * + * @param input the {@link Reader} to read. + * @return the number of bytes copied. or {@code 0} if {@code input is null}. + * @throws NullPointerException if the Reader is {@code null}. + * @throws IOException if an I/O error occurs. + * @since 2.12.0 + */ + public static long consume(final Reader input) throws IOException { + return copyLarge(input, NullWriter.INSTANCE); } /** diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java index 1a4f39fc..de8a0d49 100644 --- a/src/test/java/org/apache/commons/io/IOUtilsTest.java +++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java @@ -63,6 +63,7 @@ import org.apache.commons.io.function.IOConsumer; import org.apache.commons.io.input.BrokenInputStream; import org.apache.commons.io.input.CircularInputStream; import org.apache.commons.io.input.NullInputStream; +import org.apache.commons.io.input.NullReader; import org.apache.commons.io.input.StringInputStream; import org.apache.commons.io.output.AppendableWriter; import org.apache.commons.io.output.BrokenOutputStream; @@ -458,7 +459,7 @@ public class IOUtilsTest { } @Test - public void testConsume() throws Exception { + public void testConsumeInputStream() throws Exception { final long size = (long) Integer.MAX_VALUE + (long) 1; final InputStream in = new NullInputStream(size); final OutputStream out = NullOutputStream.INSTANCE; @@ -473,6 +474,22 @@ public class IOUtilsTest { assertEquals(size, IOUtils.consume(in), "consume()"); } + @Test + public void testConsumeReader() throws Exception { + final long size = (long) Integer.MAX_VALUE + (long) 1; + final Reader in = new NullReader(size); + final Writer out = NullWriter.INSTANCE; + + // Test copy() method + assertEquals(-1, IOUtils.copy(in, out)); + + // reset the input + in.close(); + + // Test consume() method + assertEquals(size, IOUtils.consume(in), "consume()"); + } + @Test public void testContentEquals_InputStream_InputStream() throws Exception { {
