[ 
https://issues.apache.org/jira/browse/IO-753?focusedWorklogId=667005&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-667005
 ]

ASF GitHub Bot logged work on IO-753:
-------------------------------------

                Author: ASF GitHub Bot
            Created on: 19/Oct/21 13:55
            Start Date: 19/Oct/21 13:55
    Worklog Time Spent: 10m 
      Work Description: SebastianDietrich commented on a change in pull request 
#281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r731892914



##########
File path: src/test/java/org/apache/commons/io/IOUtilsCopyTest.java
##########
@@ -80,6 +80,22 @@ public void testCopy_inputStreamToOutputStream() throws 
Exception {
         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");
+    }

Review comment:
       done

##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -979,6 +981,40 @@ public static int copy(final InputStream inputStream, 
final OutputStream outputS
         return (int) count;
     }
 
+    /**
+     * 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(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();
+        final QueueOutputStream out = in.newQueueOutputStream();

Review comment:
       done

##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -979,6 +981,40 @@ public static int copy(final InputStream inputStream, 
final OutputStream outputS
         return (int) count;
     }
 
+    /**
+     * 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(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");

Review comment:
       done

##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -979,6 +981,39 @@ public static int copy(final InputStream inputStream, 
final OutputStream outputS
         return (int) count;
     }
 
+    /**
+     * Copies bytes from a {@link java.io.ByteArrayOutputStream} to a {@code 
QueueInputStream}.
+     * <p>
+     * Unlike using JDK {@link PipedInputStream} and {@link PipedOutputStream} 
for this, this solution works safely in a single thread
+     * environment.

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 667005)
    Time Spent: 0.5h  (was: 20m)

> Provide a IOUtils.copy(OutputStream, InputStream)
> -------------------------------------------------
>
>                 Key: IO-753
>                 URL: https://issues.apache.org/jira/browse/IO-753
>             Project: Commons IO
>          Issue Type: New Feature
>          Components: Utilities
>    Affects Versions: 2.11.0
>            Reporter: Sebastian Dietrich
>            Priority: Minor
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> We already have several copy(InputStream, OutputStream) methods, but often 
> the other way round is necessary - i.e. providing an InputStream on data 
> provided by an OutputStream.
> This is e.g. the case when one is using a serialization library (for example, 
> serializing to JSON) and a transport layer (say, Tomcat) which takes an 
> InputStream. So you need to pipe the OutputStream from JSON over an HTTP 
> connection which wants to read from an InputStream.
> My particular use-case is that I want to provide an Excel file (generated via 
> POI) at my web-application. POI can write a workbook to an OutputStream, but 
> I need to provide that Excel as an InputStream to my Web-Framework (Vaadin), 
> so users can download it.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to