Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-686

IOUtils.toByteArray(InputStream) Javadoc does not match code

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.8.0
    • Fix Version/s: 2.9.0
    • Component/s: Utilities
    • Labels:
      None

      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.

      /**
       * 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();
          }
      } 

      This can be recreated by the following:

      @Test
      public void shouldThrowNullPointerException() {
          assertThrows(NullPointerException.class, () -> IOUtils.toByteArray((InputStream) null))
      } 

      On v2.7 the test passes, on v2.8.0 it fails.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                themoffster Alan Moffat
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: