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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]