You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/08/20 18:19:56 UTC

[commons-io] branch master updated: Add IOUtils.consume(InputStream).

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 c1692f4  Add IOUtils.consume(InputStream).
c1692f4 is described below

commit c1692f43ef5a41328b54bbbcd06285120aff52c3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Aug 20 14:19:48 2020 -0400

    Add IOUtils.consume(InputStream).
---
 src/changes/changes.xml                                |  3 +++
 src/main/java/org/apache/commons/io/FileUtils.java     |  2 +-
 src/main/java/org/apache/commons/io/IOUtils.java       | 18 ++++++++++++++++++
 .../java/org/apache/commons/io/IOUtilsTestCase.java    | 18 ++++++++++++++++++
 .../commons/io/input/CountingInputStreamTest.java      |  9 ++++-----
 .../commons/io/output/CountingOutputStreamTest.java    |  7 +++----
 6 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a169da3..d8fe476 100644
--- a/src/changes/changes.xml
+++ b/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.
diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java
index d23ac5c..b63d054 100644
--- a/src/main/java/org/apache/commons/io/FileUtils.java
+++ b/src/main/java/org/apache/commons/io/FileUtils.java
@@ -298,7 +298,7 @@ public class FileUtils {
             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;
     }
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java
index 13f31ab..158537b 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -53,6 +53,7 @@ import java.util.function.Consumer;
 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;
 
 /**
@@ -706,6 +707,23 @@ public class IOUtils {
     }
 
     /**
+     * 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.
      * <p>
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
index 256912d..0c2b732 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
@@ -62,7 +62,9 @@ import java.util.Arrays;
 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 class IOUtilsTestCase {
         }
     }
 
+    @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));
diff --git a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
index c47e3e9..b710a38 100644
--- a/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
@@ -73,13 +73,12 @@ public class CountingInputStreamTest {
      */
     @Test
     public void testLargeFiles_IO84() throws Exception {
-        final long size = (long)Integer.MAX_VALUE + (long)1;
-        final NullInputStream mock    = new NullInputStream(size);
+        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 class CountingInputStreamTest {
         mock.close();
 
         // Test long methods
-        IOUtils.copyLarge(cis, out);
+        IOUtils.consume(cis);
         assertEquals(size, cis.getByteCount(), "getByteCount()");
         assertEquals(size, cis.resetByteCount(), "resetByteCount()");
     }
diff --git a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
index 6885bf9..5ce521a 100644
--- a/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
@@ -78,11 +78,10 @@ public class CountingOutputStreamTest {
      */
     @Test
     public void testLargeFiles_IO84() throws Exception {
-        final long size = (long)Integer.MAX_VALUE + (long)1;
+        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 NullInputStream mock = new NullInputStream(size);
+        final CountingOutputStream cos = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM);
 
         // Test integer methods
         IOUtils.copyLarge(mock, cos);