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

FileUtils.copyToFile(InputStream source, File destination) should not close input stream

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.6
    • Fix Version/s: 2.7
    • Component/s: Streams/Writers
    • Labels:
    • Flags:
      Important

      Description

      In 2.6 this method is closing the input stream, while the javadoc states the opposite.
      The correct behavior is to leave the stream open, as stated in the javadoc.

      I assigned a high priority because this incorrect behavior breaks existing code, especially when used in combination with ZipInputStream.

      /**
       * Copies bytes from an {@link InputStream} <code>source</code> to a file
       * <code>destination</code>. The directories up to <code>destination</code>
       * will be created if they don't already exist. <code>destination</code>
       * will be overwritten if it already exists.
       * The {@code source} stream is left open, e.g. for use with {@link java.util.zip.ZipInputStream ZipInputStream}.
       * See {@link #copyInputStreamToFile(InputStream, File)} for a method that closes the input stream.
       *
       * @param source      the <code>InputStream</code> to copy bytes from, must not be {@code null}
       * @param destination the non-directory <code>File</code> to write bytes to
       *                    (possibly overwriting), must not be {@code null}
       * @throws IOException if <code>destination</code> is a directory
       * @throws IOException if <code>destination</code> cannot be written
       * @throws IOException if <code>destination</code> needs creating but can't be
       * @throws IOException if an IO error occurs during copying
       * @since 2.5
       */
      public static void copyToFile(final InputStream source, final File destination) throws IOException {
      	try (InputStream in = source;
      		 OutputStream out = openOutputStream(destination)) {
      		IOUtils.copy(in, out);
      	}
      }
      

      instead it should be:

      public static void copyToFile(final InputStream source, final File destination) throws IOException {
      	try (OutputStream out = openOutputStream(destination)) {
      		IOUtils.copy(source, out);
      	}
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                kinow Bruno P. Kinoshita
                Reporter:
                mmariotti Michele Mariotti
              • Votes:
                0 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: