You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/10/10 08:06:26 UTC

[GitHub] [commons-io] SebastianDietrich opened a new pull request #281: Io 753/copy output stream to input stream

SebastianDietrich opened a new pull request #281:
URL: https://github.com/apache/commons-io/pull/281


   this works at least for java.io.ByteArrayOutputStream
   
   see https://issues.apache.org/jira/projects/IO/issues/IO-753


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] SebastianDietrich commented on a change in pull request #281: Io 753/copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
SebastianDietrich commented on a change in pull request #281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r730660676



##########
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 {

Review comment:
       That is not possible, since org.apache.commons.io.output.ByteArrayOutputStream is imported and used throughout IOUtils




-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] garydgregory commented on a change in pull request #281: Io 753/copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r726992168



##########
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:
       The p HTML tag is still not closed.




-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] SebastianDietrich commented on a change in pull request #281: [IO-753] Add IOUtils method to copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
SebastianDietrich commented on a change in pull request #281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r731892749



##########
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));

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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] garydgregory commented on a change in pull request #281: [IO-753] Add IOUtils method to copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r731842874



##########
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();

Review comment:
       Gotcha, please see the other comments.




-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] SebastianDietrich commented on a change in pull request #281: [IO-753] Add IOUtils method to copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] garydgregory merged pull request #281: [IO-753] Add IOUtils method to copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
garydgregory merged pull request #281:
URL: https://github.com/apache/commons-io/pull/281


   


-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [commons-io] garydgregory commented on a change in pull request #281: Io 753/copy output stream to input stream

Posted by GitBox <gi...@apache.org>.
garydgregory commented on a change in pull request #281:
URL: https://github.com/apache/commons-io/pull/281#discussion_r730294920



##########
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));

Review comment:
       Undefined constant: in example `UTF_8` -> `StandardCharsets.UTF_8`.

##########
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();

Review comment:
       Here, using the FQCN is required since this test class imports `org.apache.commons.io.output.ByteArrayOutputStream`.

##########
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:
       See above, missing test for null input using `assertThrows()`.
   

##########
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:
       This PR should test this path with an `assertThrows()`.

##########
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 {

Review comment:
       Import ByteArrayOutputStream instead of using a FQCN.

##########
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:
       The `out` variable is not needed IMO.




-- 
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: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org