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]


Reply via email to