commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alan Moffat (Jira)" <>
Subject [jira] [Created] (IO-686) [v2.8.0] IOUtils.toByteArray(null) does not throw exception
Date Thu, 10 Sep 2020 14:55:00 GMT
Alan Moffat created IO-686:

             Summary: [v2.8.0] IOUtils.toByteArray(null) does not throw exception
                 Key: IO-686
             Project: Commons IO
          Issue Type: Bug
          Components: Utilities
            Reporter: Alan Moffat

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.
 * 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:
public void shouldThrowNullPointerException() {
    assertThrows(NullPointerException.class, () -> {
      InputStream inputStream = clazz.getResourceAsStream("non-existant-file.txt");
      return IOUtils.toByteArray(inputStream);
} {code}

This message was sent by Atlassian Jira

View raw message