Add IOUtils.consume(Reader).
MODIFY
src/changes/changes.xml
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.

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
@@ -832,7 +832,23 @@ public static void closeQuietly(final Writer writer) {
* @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);
}
/**

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTest.java
src/test/java/org/apache/commons/io/IOUtilsTest.java
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 void testConstants() {
}
@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 void testConsume() throws Exception {
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 {
{

Add IOUtils.consume(Reader).