Add IOUtils.consume(InputStream).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -121,6 +121,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-672" dev="sebb" type="fix">
Copying a File sets last modified date to 01 January 1970.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOUtils.consume(InputStream).
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Gary Gregory">
Replace FindBugs with SpotBugs.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -298,7 +298,7 @@ public static Checksum checksum(final File file, final Checksum checksum) throws
throw new IllegalArgumentException("Checksums can't be computed on directories");
}
try (InputStream in = new CheckedInputStream(new FileInputStream(file), checksum)) {
- IOUtils.copy(in, NullOutputStream.NULL_OUTPUT_STREAM);
+ IOUtils.consume(in);
}
return checksum;
}

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.output.AppendableWriter;
import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.StringBuilderWriter;
/**
@@ -705,6 +706,23 @@ public static void closeQuietly(final Writer output) {
closeQuietly((Closeable) output);
}
+ /**
+ * Consumes bytes from a <code>InputStream</code> and ignores them.
+ * <p>
+ * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
+ * </p>
+ *
+ * @param input the <code>InputStream</code> to read from
+ * @return the number of bytes copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.8.0
+ */
+ public static long consume(final InputStream input)
+ throws IOException {
+ return copyLarge(input, NullOutputStream.NULL_OUTPUT_STREAM);
+ }
+
/**
* Compares the contents of two Streams to determine if they are equal or
* not.

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
import java.util.List;
import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.input.NullInputStream;
import org.apache.commons.io.output.AppendableWriter;
+import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.StringBuilderWriter;
import org.apache.commons.io.test.TestUtils;
import org.apache.commons.io.test.ThrowOnCloseReader;
@@ -450,6 +452,22 @@ public synchronized void close() throws IOException {
}
}
+ @Test
+ public void testConsume() throws Exception {
+ final long size = (long)Integer.MAX_VALUE + (long)1;
+ final InputStream in = new NullInputStream(size);
+ final OutputStream out = NullOutputStream.NULL_OUTPUT_STREAM;
+
+ // 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 {
{
assertTrue(IOUtils.contentEquals((InputStream) null, null));

MODIFY
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
@@ -76,10 +76,9 @@ public void testLargeFiles_IO84() throws Exception {
final long size = (long) Integer.MAX_VALUE + (long) 1;
final NullInputStream mock = new NullInputStream(size);
final CountingInputStream cis = new CountingInputStream(mock);
- final OutputStream out = NullOutputStream.NULL_OUTPUT_STREAM;
// Test integer methods
- IOUtils.copyLarge(cis, out);
+ IOUtils.consume(cis);
try {
cis.getCount();
fail("Expected getCount() to throw an ArithmeticException");
@@ -96,7 +95,7 @@ public void testLargeFiles_IO84() throws Exception {
mock.close();
// Test long methods
- IOUtils.copyLarge(cis, out);
+ IOUtils.consume(cis);
assertEquals(size, cis.getByteCount(), "getByteCount()");
assertEquals(size, cis.resetByteCount(), "resetByteCount()");
}

MODIFY
src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
@@ -81,8 +81,7 @@ public void testLargeFiles_IO84() throws Exception {
final long size = (long) Integer.MAX_VALUE + (long) 1;
final NullInputStream mock = new NullInputStream(size);
- final OutputStream nos = NullOutputStream.NULL_OUTPUT_STREAM;
- final CountingOutputStream cos = new CountingOutputStream(nos);
+ final CountingOutputStream cos = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM);
// Test integer methods
IOUtils.copyLarge(mock, cos);

Add IOUtils.consume(InputStream).