You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alan Moffat (Jira)" <ji...@apache.org> on 2020/09/10 14:57:00 UTC

[jira] [Updated] (IO-686) [v2.8.0] IOUtils.toByteArray(null) does not throw exception

     [ https://issues.apache.org/jira/browse/IO-686?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Alan Moffat updated IO-686:
---------------------------
    Description: 
According to the code in the v2.8.0 release, passing null to the method should throw an exception, however it is producing an empty byte array instead.
{code:java}
/**
 * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * </p>
 *
 * @param input the <code>InputStream</code> to read from
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 */
public static byte[] toByteArray(final InputStream input) throws IOException {
    try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        copy(input, output);
        return output.toByteArray();
    }
} {code}
This can be recreated by the following:
{code:java}
@Test
public void shouldThrowNullPointerException() {
    assertThrows(NullPointerException.class, () -> IOUtils.toByteArray(null))
} {code}

  was:
According to the code in the v2.8.0 release, passing null to the method should throw an exception, however it is producing an empty byte array instead.
{code:java}
/**
 * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * </p>
 *
 * @param input the <code>InputStream</code> to read from
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException          if an I/O error occurs
 */
public static byte[] toByteArray(final InputStream input) throws IOException {
    try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        copy(input, output);
        return output.toByteArray();
    }
} {code}
This can be recreated by the following:
{code:java}
@Test
public void shouldThrowNullPointerException() {
    assertThrows(NullPointerException.class, () -> {
      InputStream inputStream = clazz.getResourceAsStream("non-existant-file.txt");
      return IOUtils.toByteArray(inputStream);
    }
} {code}


> [v2.8.0] IOUtils.toByteArray(null) does not throw exception
> -----------------------------------------------------------
>
>                 Key: IO-686
>                 URL: https://issues.apache.org/jira/browse/IO-686
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>            Reporter: Alan Moffat
>            Priority: Critical
>
> According to the code in the v2.8.0 release, passing null to the method should throw an exception, however it is producing an empty byte array instead.
> {code:java}
> /**
>  * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
>  * <p>
>  * This method buffers the input internally, so there is no need to use a
>  * <code>BufferedInputStream</code>.
>  * </p>
>  *
>  * @param input the <code>InputStream</code> to read from
>  * @return the requested byte array
>  * @throws NullPointerException if the input is null
>  * @throws IOException          if an I/O error occurs
>  */
> public static byte[] toByteArray(final InputStream input) throws IOException {
>     try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
>         copy(input, output);
>         return output.toByteArray();
>     }
> } {code}
> This can be recreated by the following:
> {code:java}
> @Test
> public void shouldThrowNullPointerException() {
>     assertThrows(NullPointerException.class, () -> IOUtils.toByteArray(null))
> } {code}



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