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 276bd98  [IO-753] Add IOUtils method to copy output stream to input 
stream (#281)
276bd98 is described below

commit 276bd98be670c7271814023d5306fe4838d49b93
Author: Sebastian Dietrich <sebastian.dietr...@e-movimento.com>
AuthorDate: Tue Oct 19 17:17:24 2021 +0200

    [IO-753] Add IOUtils method to copy output stream to input stream (#281)
    
    * IO-753 - added method to copy ByteArrayOutputStream to InputStream
    
    * IO-753 fixed auto-indentation and organize-import changes
    
    * IO-753 fixed organize-import changes
    
    * IO-753 fixed checkstyle warnings
    
    * IO-753 fixed javadoc errors
    
    * IO-753 fixed PR remarks
---
 src/main/java/org/apache/commons/io/IOUtils.java   | 34 ++++++++++++++++++++++
 .../org/apache/commons/io/IOUtilsCopyTest.java     | 21 +++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/src/main/java/org/apache/commons/io/IOUtils.java 
b/src/main/java/org/apache/commons/io/IOUtils.java
index b095623..24726e2 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -51,6 +51,7 @@ import java.util.Objects;
 import java.util.function.Consumer;
 
 import org.apache.commons.io.function.IOConsumer;
+import org.apache.commons.io.input.QueueInputStream;
 import org.apache.commons.io.output.AppendableWriter;
 import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.commons.io.output.NullOutputStream;
@@ -980,6 +981,39 @@ public class IOUtils {
     }
 
     /**
+     * Copies bytes from a {@link java.io.ByteArrayOutputStream} to a {@code 
QueueInputStream}.
+     * <p>
+     * Unlike using JDK {@link java.io.PipedInputStream} and {@link 
java.io.PipedOutputStream} for this, this
+     * solution works safely in a single thread environment.
+     * </p>
+     * <p>
+     * Example usage:
+     * </p>
+     *
+     * <pre>
+     * ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+     * outputStream.writeBytes("hello world".getBytes(StandardCharsets.UTF_8));
+     *
+     * InputStream inputStream = IOUtils.copy(outputStream);
+     * </pre>
+     *
+     * @param outputStream the {@link java.io.ByteArrayOutputStream} to read.
+     * @return the {@code QueueInputStream} filled with the content of the 
outputStream.
+     * @throws NullPointerException if the {@link 
java.io.ByteArrayOutputStream} is {@code null}.
+     * @throws IOException if an I/O error occurs.
+     * @since 2.12
+     */
+    @SuppressWarnings("resource") // streams are closed by the caller.
+    public static QueueInputStream copy(final java.io.ByteArrayOutputStream 
outputStream) throws IOException {
+        Objects.requireNonNull(outputStream, "outputStream");
+
+        final QueueInputStream in = new QueueInputStream();
+        outputStream.writeTo(in.newQueueOutputStream());
+
+        return in;
+    }
+
+    /**
      * Copies bytes from an {@code InputStream} to an {@code OutputStream} 
using an internal buffer of the
      * given size.
      * <p>
diff --git a/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java 
b/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
index b6d43e3..3da83d0 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
@@ -80,6 +80,27 @@ public class IOUtilsCopyTest {
         assertEquals(inData.length,count);
     }
 
+    @SuppressWarnings("resource") // 'in' is deliberately not closed
+    @Test
+    public void testCopy_byteArrayOutputStreamToInputStream() throws Exception 
{
+        final java.io.ByteArrayOutputStream out = new 
java.io.ByteArrayOutputStream();
+        out.write(inData);
+
+        final InputStream in = IOUtils.copy(out);
+
+        final byte[] inData2 = new byte[FILE_SIZE];
+        final int insize = in.read(inData2);
+
+        assertEquals(0, in.available(), "Not all bytes were read");
+        assertEquals(inData.length, insize, "Sizes differ");
+        assertArrayEquals(inData, inData2, "Content differs");
+    }
+
+    @Test
+    public void testCopy_byteArrayOutputStreamToInputStream_nullOutputStream() 
{
+        assertThrows(NullPointerException.class, () -> IOUtils.copy(null));
+    }
+
     /**
      * Test Copying file > 2GB  - see issue# IO-84
      */

Reply via email to